Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 40
1
Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40
    1
    Netbeans IDE
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Roel,
    thank you for your answer, which is totally clear, as always.

    pagano monello
     
    passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
    free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic