This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Primitive Converstions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Primitive Converstions" Watch "Primitive Converstions" New topic
Author

Primitive Converstions

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
this is about a conversion from int to byte and int to double.
Why does iftest1a work and iftest1b doesn’t?
What must be done to get iftest1b to work properly?

Compiles and runs with result: „i < a“
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ July 24, 2002: Message edited by: Dirk Schreckmann ]
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Primitives can be automatically converted if the new primitive is 'wider'. So the following is possible:
int (32 bits) -> double (64 bits)
However, you cannot automatically convert to a narrower primitive. This is not possible:
int (32 bits) -> byte (8 bits)
To do this, you must use an explicit cast expression:
int i = 3;
byte b = (byte)i;
Cheers, Neil
[ July 24, 2002: Message edited by: Neil Laurance ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primitive Converstions
 
Similar Threads
Casting int to long
Barry Boone #29
narrowing primitive casts
Static initaializer block & forward referencing -Question from SCJP 6 book by Khalid Mughal
byte[] to int