Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type casting

 
ankit maini
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankit maini
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks 2 all.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new at this site ... so i don't know about this.
i will keep in mind ..
thanks
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic