Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive data types: default type and prefixes / suffixes - casting - notations

 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I have three question about primitive data types:

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?

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 (or char)? In other worlds, does it mean that it's not allowed to cast just numeric types (or 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.

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, according to me. I hope I mustn't expect tricky questions with hexadecimal values stored in binary variables in OCA SE 7 Exam; it would be a mistake guaranteed.

Thank you in advance.
pagano monello
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 happensAnd try the same with this little code snippetI 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 compileThe 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 examplesThe 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?
  • Casting
  • Not sure why my answer on overloading was incorrect
  • Some doubts about casting
  • instanceof 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
     
    pagano monello
    Ranch Hand
    Posts: 38
    1
    Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel,
    thank you for your answer, which is totally clear, as always.

    pagano monello
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic