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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Casting Integer to byte" Watch "Casting Integer to byte" New topic
Author

Casting Integer to byte

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hi all,


I am studying casting of Primitive types , but i cannot understand how can we cast an integer whose range is from 0-65535 into a byte whose rannge is 0-256 . We will never be able to cast integer values greater than 256 into bytes . However the book says we will only get a loss of precision . Can any one explain .
Ill elaborate my doubt.

example:-
int a=10;
int b=65534
byte c=(byte)(a+b);

Even when we apply an external cast we will not be able to cast it into byte the value of integer sum is greater than the byte range . how can we ever get the result ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

The type int is a 32-bit signed integer, so the range is from -2^31 to 2^31 - 1 = -2147483648 to 2147483647. Not from 0 to 65535 as you said.

And the type byte is an 8-bit signed integer, which has a range from -2^7 to 2^7 - 1 = -128 to 127. Not from 0 to 256 as you said.

When you cast an int to a byte, only the lower 8 bits of the int are taken. The higher 24 bits are thrown away. So you lose information when you cast an int to a byte.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

integer whose range is from 0-65535

No, integers are signed four-byte values. The maximum value is 0x7fffffff (2147483647).

Even when we apply an external cast we will not be able to cast it into byte the value of integer sum is greater than the byte range . how can we ever get the result ?

This is called a narrowing primitive conversion, and is explained in details here.

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.


[My Blog]
All roads lead to JavaRanch
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Have a look at this thread
http://www.coderanch.com/t/384557/java/java/Casting-int-value-Byte

It would be nice if you can tell how it prints 8 based on your understanding.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Too difficult to be a "beginning" topic. Moving thread.
 
Consider Paul's rocket mass heater.
 
subject: Casting Integer to byte