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

Integer literals implicit casting

 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte a=100;// works fine
//but
byte b=a+a;//don't why??
 
vibhor sharma
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The range of a byte is up to 127 , so first statement works fine .

in second statement you are adding two bytes , 200 , in this case and storing it again on byte type. what else would you expect ??

Can you figure out the solution ??
int b = a+a ; should work with out truncating any bits.
 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte a = 0;
byte b=a+a;
//this also doesn't work
 
vibhor sharma
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We tried to assign the sum of two bytes to a byte variable, the result of which
is small enough to fit into a byte, How ever , we are adding something here , which means we are involving expressions .
But the compiler knows taht anything int -or -smaller expressions are always result an int (implicitly)..

it wont get complied unless explicit cast.

Hope this helps ...
 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vibhor sharma wrote:We tried to assign the sum of two bytes to a byte variable, the result of which
is small enough to fit into a byte, How ever , we are adding something here , which means we are involving expressions .
But the compiler knows taht anything int -or -smaller expressions are always result an int (implicitly)..

it wont get complied unless explicit cast.

Hope this helps ...

Hmm you are right bro...but 100 will be by default interger and a+a will also result in an interger so why explict cast is required in second case??and also byte a=100 works fine but float a=30.9 don't why??help!!, i am confused
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Vibhor said, the result of a byte + a byte is an int. You'll need to cast the results:
 
vibhor sharma
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)the second case

byte b = some byte + some byte , which will results in..(as compiler thinks ) an int type... and we are storing the result into byte type ...

Think like this .. how can a smaller cup (byte) would able to handle a larger cup (int)....excessive water gets out in the form of truncating bits..


I suggest you to read some casting basics from Kathy sierra book .. you wont regret .
 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i have read the topic from the same book but i am confused by following results
result1:
byte a=0;//which works fine
0 is also a interger so why we don't require a cast here...it is in byte range,ok! but a+a will result in a interger and also will be in byte range so why cast is required here??
result2:
byte a=0;//which works fine
but
float a=0.0 don't,why?
result3:
byte a=0;
a=a+a;requires an explicit cast
but
a+=a;works fine
 
Vxyz eom
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mrkamal joshi wrote:byte a=100;// works fine
//but
byte b=a+a;//don't why??

See This..
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 2 types of initialization you can do to a variable
(1) when you are doing then you are storing 100 which is by default of type integer into an variable of type byte.........Now
here Java will do an automatic conversion(implicit conversion) from int to byte i.e. changing 100 of type int to 100 of type byte................Now the question arises why this conversion happened? It happened because 100 was within the range of byte (range of byte is -127 to 128)............ So when you store Here 200 is again of integer but here an conversion will not happen since 200 is out-of-range from that of byte.............
(2) DYNAMIC INITIALIZATION : Now see when you will write So what you are doing here is adding to 2 numbers again by default of type integer to a variable of type byte.....This is Dynamic Initialization(i.e storing an expression into a variable........expression can include addition, subtraction, multiplication, division etc....... ) Here the compiler will not do the automatic conversion because the compiler has no idea what 5+4 will return as an answer....The compiler thinks that adding 2 numbers of type integer may return a value which cannot be stored in an byte variable(But in above example it can v=be stored in byte since the answer of 5+4 is 9......but the compiler doesn't know this and that's why the name is dynamic initialization i.i run-time).......So to be on safe side it thinks the answer is int..........And that is why we have to explicitly cast it and therefore to avoid error we write
.
.
.
You also asked about error................ See by default floating point numbers are of type double.....The way 5 is an int the same way 3.4 is double ......And above you are storing a double value i.e 3.4 into a float and that is why it is an error.......So you have to append an F or f at the end of the value .So to avoid an error we write
 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote:There are 2 types of initialization you can do to a variable
(1) when you are doing then you are storing 100 which is by default of type integer into an variable of type byte.........Now
here Java will do an automatic conversion(implicit conversion) from int to byte i.e. changing 100 of type int to 100 of type byte................Now the question arises why this conversion happened? It happened because 100 was within the range of byte (range of byte is -127 to 128)............ So when you store Here 200 is again of integer but here an conversion will not happen since 200 is out-of-range from that of byte.............
(2) DYNAMIC INITIALIZATION : Now see when you will write So what you are doing here is adding to 2 numbers again by default of type integer to a variable of type byte.....This is Dynamic Initialization(i.e storing an expression into a variable........expression can include addition, subtraction, multiplication, division etc....... ) Here the compiler will not do the automatic conversion because the compiler has no idea what 5+4 will return as an answer....The compiler thinks that adding 2 numbers of type integer may return a value which cannot be stored in an byte variable(But in above example it can v=be stored in byte since the answer of 5+4 is 9......but the compiler doesn't know this and that's why the name is dynamic initialization i.i run-time).......So to be on safe side it thinks the answer is int..........And that is why we have to explicitly cast it and therefore to avoid error we write
.
.
.
You also asked about error................ See by default floating point numbers are of type double.....The way 5 is an int the same way 3.4 is double ......And above you are storing a double value i.e 3.4 into a float and that is why it is an error.......So you have to append an F or f at the end of the value .So to avoid an error we write

Thanks for the concept of dynamic initilization,but if we write
byte a=0;
a+=0; this works fine..how?
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is good that you are trying all sort of possibilities...........Great
+= is known as addition assignment operator and is a shorthand operator..........i.e. is equivalent to
As the name implies shorthand therefore += will automatically handle conversion and everything for you.........Therefore when using += there is no need of cast this is what i think........it is always better to use shorthand operators
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compound statements +=, -=, etc also perform a cast. So a += 0 is actually the same as a = (byte)(a + 0).
 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rameshwar Soni
 
Maarten Bodewes
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This stuff is very clearly specified in the Java language specs. Try them out, they're pretty readable regarding the base types, and you might learn things about the language you would not otherwise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic