aspose file tools*
The moose likes Beginning Java and the fly likes Type casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Type casting" Watch "Type casting" New topic
Author

Type casting

ankit maini
Greenhorn

Joined: Feb 08, 2011
Posts: 25

class Test3
{
public static void main(String[] args)
{

byte f = (byte)3249;

System.out.println (f);

}
}

Output is -79

How to solve these types of problems ?
How to identify the sign?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3969
    
  17

ankit maini wrote:class Test3
{
public static void main(String[] args)
{

byte f = (byte)3249;

System.out.println (f);

}
}

Output is -79

How to solve these types of problems ?

You can avoid the overflow by checking if the value fits inside the range of bytes before making the cast:



How to identify the sign?

I don't think I understand the question here. Can you restate it?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A literal numeric value, like 3249, is an int.

If you need to assign a literal numeric value to a narrower type, and the value is outside that type's range, then you (the programmer) must supply an explicit cast. This is required because you are losing information, which might give undexpected results. By supplying the explict cast, you (the programmer) are saying, "Yes, I understand this could be a problem, but trust me, this is what I want."

Edit: I thought you were asking how to solve the "problem" of getting an unexpected result (which would be to avoid the narrowing cast). But maybe you're asking instead how this result is calculated...? For that, see my follow-up below.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

ankit maini wrote:...
How to solve these types of problems ?
How to identify the sign?

In Java, the integral primitives (byte, short, int, and long) are stored as signed two's complements. See Cornell - Two's Complement.

In your example, the int 3249 is represented as 32-bits, 0000 0000 0000 0000 0000 0000 1011 0001.

When you narrow this to an 8-bit byte, it becomes 1011 0001.

In two's complement, the most significant bit (the one farthest to the left) is the "sign bit." If that's a 1, then the value is negative. So the byte 1011 0001 is negative. As explained by the link above, the absolute value of this negative number is determined by inverting the bits and adding one.

Inverting 1011 0001 results in 0100 1110, and adding 1 results in 0100 1111, which is 79 in decimal. So it's -79.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Marc explains it in detail; you have to know what the bits of the number look like in two's complement to figure out what the result will be exactly.

If you're doing this because you are learning for a certification exam, then you will not get questions like these on the exam. Nobody will expect you to be able to see in your head what the two's complement of an arbitrary integer will look like... but it's important to understand the principle.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ankit maini
Greenhorn

Joined: Feb 08, 2011
Posts: 25

thanks 2 all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
ankit maini wrote:thanks 2 all.
I am sure all who responded were pleased to help, but it's not 2, please.
ankit maini
Greenhorn

Joined: Feb 08, 2011
Posts: 25

I am new at this site ... so i don't know about this.
i will keep in mind ..
thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Apology accepted
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type casting
 
Similar Threads
shifting and casting
Shift Operator
how to convert Byte to the String
Casting Difference of Float.POSITIVE_INFINITY
narrowing primitive casts