aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in implict casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in implict casting" Watch "doubt in implict casting" New topic
Author

doubt in implict casting

Karthick Ravi
Greenhorn

Joined: Feb 27, 2008
Posts: 13

Guys,

What is the difference between

byte testMethod(){ return 42;}

and

byte testMethod(){ int i = 42; return i;}


The first one compiles but the second one doesn't. My understanding was, java takes in any number literal as an integer, in which case both shouldn't compile.. Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

In the first one you're returning a constant (namely 42). In the second one you're returning an int variable (namely i). Do you see why that is a significant difference?
Karthick Ravi
Greenhorn

Joined: Feb 27, 2008
Posts: 13

Yes Paul, but shouldn't "42" be read as an integer? How can it get converted to byte without an explicit cast?
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

Yes..in the second one you are returning int variable..that will cause the type mis match


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

If you return a constant, the compiler is smart enough to see that it fits into a byte and will cast it implicitly for you. If you would have used a value that would not fit in a byte, for example:

Then you would have gotten a compiler error.

This special handling doesn't work when you're trying it with a variable instead of a constant, because the compiler in principle can't be sure, by only looking at the return statement, if the value of the variable will fit into a byte.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Praveen mourya Kumar
Greenhorn

Joined: Oct 14, 2008
Posts: 16
Karthick Ravi wrote:
Guys,

What is the difference between

byte testMethod(){ return 42;}

and

byte testMethod(){ int i = 42; return i;}


The first one compiles but the second one doesn't. My understanding was, java takes in any number literal as an integer, in which case both shouldn't compile.. Thanks.


There is huge difference in returning call.
"return 42;", here compiler wants a byte to be return. So Compiler will implicitly type converted to byte.similar to
byte b+=2;
if you see the second example,"int i=42; return i;".It is obvious, you are returning an int value, which need explicit type conversion. Complier will flag a error. It is similar to below statement :
byte b=12;
b=b+13;// here compiler will raise an error.Why?
Please remember the following sequence for implicit conversion
byte --> short --> int -->long -->float-->double

or
char-->int-->long-->float-->double

if you try to go in reverse, then you need to do explicit type casting. Even you have to do explicit type casting in the case of short( or byte) to char primitive types.


SCJP 1.6..
belakumari das
Ranch Hand

Joined: Mar 31, 2009
Posts: 31


byte testMethod(){ int i = 42; return i;}

will not compile because int is heigher precedence than byte

But
below method will compile
int testMethod(){ byte i = 42; return i;}


you can cast byte to int but you can't int to byte

precedence from low to high
byte
int
long





java j2ee 10+ years Experience.

Praveen mourya Kumar
Greenhorn

Joined: Oct 14, 2008
Posts: 16
belakumari das wrote:

byte testMethod(){ int i = 42; return i;}

will not compile because int is heigher precedence than byte

But
below method will compile
int testMethod(){ byte i = 42; return i;}


you can cast byte to int but you can't int to byte

precedence from low to high
byte
int
long






Correct!!!
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
It is the same case as if you declare a byte as

byte b = 100; /*Compiler knows very well the range of a byte....*/


Thanks
Vijay Saraf.
Karthick Ravi
Greenhorn

Joined: Feb 27, 2008
Posts: 13

Thanks a lot guys. I understand it completely now..

What made me confused was, since every number literal is taken as an int in java, i thought there shouldn't be a difference between those two. But looks like there is a difference.
Praveen mourya Kumar
Greenhorn

Joined: Oct 14, 2008
Posts: 16
Yes.. It is as simple. But remember Java is not completly OO due to its primitive data types.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in implict casting