• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rules of casting and converting primitives

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please elaborate on the rules for casting and converting between primitives? I'm always confused about this issue, and would hope that someone could post some rules or some tricks to help me answer these questions for the exam. thanks a lot.
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also can anyone explain when to use an explicit cast and when a cast is not needed as well. Thanks again
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch "acha114". Please change your displayed name to conform with our JavaRanch Naming Policy. We need a <first name> <family name> pair. You can change it via the My Profile link.

Thanks
-Barry

(NR) 1st time requested.
[ June 06, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect! Thanks.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is never necessary and, in my opinion, rather poor practice to cast where there is a widening conversion, eg int to long. But you often must cast whenever you need to do a narrowing conversion, eg long to int.

There are times when you do not need to cast as the compiler will apply the necessary conversion. You will need to check the JLS to ascertain when this happens. A typical example is binary operators, when if one of the operands is double the other operand is converted to a double.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to cast when the new type is not wider than or the same as the old type.

short is wider than byte
int is wider than byte, short, and char
long is wider than int
float is wider than long
double is wider than float

Note that "not wider than" isn't the same as "narrower than". Neither char nor short is wider than the other. Nor not contrariwise neither, as they say in Javaranch country: neither char nor short is narrower tan the other.

Pitfall alert: "wider" and "narrower" don't refer to bit width. 32-bit float is wider than 64-bit long. Width refers to the range of possible values the type can have. The range of byte completely fits with the range of float, so no cast is required: every possible byte can be reasonably represented by a float. But when you go from a float to a byte, there are possible values for the float that can't be represented by the byte. So the language requires you to explicitly tell the compiler that you really don't mind the risk, by using a cast.

Back when I taught Java, I told people that casting is like "The Odd Couple". Remember that show? Felix was fussy and full of rules ... just like the Java compiler. Oscar wanted to do whatever he wanted to do, regardless of the consequences ... just like us programmers. When they got into conflict, Oscar would say, "Come on, Felix, pleeeeeease!" Felix would wag his finger and say "Very well, Oscar, but let this be on your head."

Look at line 3 below:


When this fails to compile, it's Felix saying that Oscar can't smoke cigars in the apartment when the Pigeon sisters come over for their dinner date. When we change line 3 to , we're Oscar saying, "Oh come on, pleeeease!". When line 3 now compiles, Felix is saying "Very well, but let this be on your head!"

It's on Oscar's head because at runtime, Felix the compiler isn't around. If the value in d is larger than the max int, Oscar (that's us) will just have to live with the consequences.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic