• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type casting

 
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks 2 all.
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apology accepted
 
reply
    Bookmark Topic Watch Topic
  • New Topic