File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Integer literals implicit casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer literals implicit casting" Watch "Integer literals implicit casting" New topic
Author

Integer literals implicit casting

MrKamal Joshi
Greenhorn

Joined: Jul 31, 2011
Posts: 24

byte a=100;// works fine
//but
byte b=a+a;//don't why??
vibhor sharma
Greenhorn

Joined: Dec 10, 2010
Posts: 19

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

Joined: Jul 31, 2011
Posts: 24

byte a = 0;
byte b=a+a;
//this also doesn't work
vibhor sharma
Greenhorn

Joined: Dec 10, 2010
Posts: 19

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

Joined: Jul 31, 2011
Posts: 24

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

Joined: Oct 27, 2005
Posts: 19719
    
  20

As Vibhor said, the result of a byte + a byte is an int. You'll need to cast the results:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vibhor sharma
Greenhorn

Joined: Dec 10, 2010
Posts: 19

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

Joined: Jul 31, 2011
Posts: 24

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

Joined: Aug 02, 2011
Posts: 4
mrkamal joshi wrote:byte a=100;// works fine
//but
byte b=a+a;//don't why??

See This..


-- Vinay [The Excavator of Mind]
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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

Joined: Jul 31, 2011
Posts: 24

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

Joined: Feb 03, 2011
Posts: 247
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

The compound statements +=, -=, etc also perform a cast. So a += 0 is actually the same as a = (byte)(a + 0).
MrKamal Joshi
Greenhorn

Joined: Jul 31, 2011
Posts: 24

Thank you Rameshwar Soni
Maarten Bodewes
Greenhorn

Joined: Aug 04, 2011
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer literals implicit casting