pagano monello wrote:1. Is there any reason why short values doesn't have a suffix neither a prefix? I think that suffixes and prefixes make more sense to distinguish between the different numeration systems (octal, hexadecimal...), rather than between the different data types (int, long...); however long values can have prefix L or l. It sounds weird to me. Also: is there any difference in omitting or not the suffix for long values?
Try to compile this one without using the long suffix and see what happens
And try the same with this little code snippet
I think both examples will definitely show there is a difference between omitting the suffix or not. And it also answers the question why these suffixes are needed.
pagano monello wrote:2. I read that casting is allowed only between compatible types. Does it mean that it is possible to cast only from a numeric type to another numeric type (and char)? In other worlds, does it mean that it's not allowed to cast just numeric types (and char) with boolean? Actually I am not sure to have clearly understood when it is allowed to cast from a type to another, and when it isn't.
You can indeed only cast compatible types. So these statements will not compile
The rules for casting primitive types are pretty easy. You can cast all numeric types and
char to any numeric type or
char without compiler errors. You might get odd looking results if some number is outside the range of the value you are casting to. Some examples
The rules for casting reference variables are much harder and have a few intricacies. These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls):
Why will this throw Class Cast Exception?CastingNot sure why my answer on overloading was incorrectSome doubts about castinginstanceof operator with an interface versus class
(Note: casting and the
instanceof operator are closely related, so the rules are exactly the same)
pagano monello wrote:3. I have noticed that I can use the hexadecimal notation, even if I am defining an byte variable; this thing doesn't make a lot of sense. I hope I mustn't expect tricky questions with hexadecimal values stored in binary variables in OCA SE 7 Exam; I would make a mistake for sure.
Why doesn't it make sense to assign a hexadecimal to a byte? It would be much weirder ad be less conistent if it would be possible for all integral types, except
byte. Here's a little example
Hope it helps!
Kind regards,
Roel