Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in implict casting

 
Karthick Ravi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1874
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes..in the second one you are returning int variable..that will cause the type mis match
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Praveen mourya Kumar
Greenhorn
Posts: 16
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
belakumari das
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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




 
Praveen mourya Kumar
Greenhorn
Posts: 16
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the same case as if you declare a byte as

byte b = 100; /*Compiler knows very well the range of a byte....*/
 
Karthick Ravi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 16
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.. It is as simple. But remember Java is not completly OO due to its primitive data types.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic