Results 1 to 4 of 4
  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default int and byte and casting

    public class A
    {
    public static void main(String args[])
    {
    byte a=5,b=2,c;
    c=(byte)(a*b);
    System.out.println("c is="+c);//c=10

    byte d=-0x05,e=0x7f,f=0xffffff81,g=(byte)0x81,h=0x81;

    System.out.println("d is="+d);//d=-5
    System.out.println("e is="+e);//127
    System.out.println("f is="+f);//-127
    System.out.println("g is="+g);//-127
    System.out.println("h is="+h);//Error!
    }
    }


    Hello
    f is a kind of byte and its value is 0xffffff81.
    f is -127.
    f has 6 f in its definition. why?

    if i write byte g=0x81 then i will get an error that g is not byte. why?
    but with casting i will have -127.why?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java uses two's complements to represent negative numbers. So 0xffffff81 is actually the int literal -127, is within bytes range, and won't require an explicit cast to byte.

    I can't really explain two's complements as well as these articles below. Please have a look:
    two’s complement arithmetic : Java Glossary
    Negative numbers and binary representation in Java?
    Two's complement - Wikipedia, the free encyclopedia

  3. #3
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I do not underestant yet.
    the second link does not open and disply:"The page cannot be displayed"
    the third link seemes Intelligible and simple, but i do not underestand why 0xffffff81 is -127 in java.:confused:

    please help me.

  4. #4
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello

    I studied cached page of second link, not exact link because exact link dos not open.

    I saw an example for -7:

    Let us take a negative number for example, -7.
    int a = -7; //declaration
    The bit representation will be in 2ís complement form.

    Steps to find 2ís Complement.

    Take binary representation of positive value (in this case, it is 7)

    0000 0000 0000 0000 0000 0000 0000 0111"
    but, 0000 0000 0000 0000 0000 0000 0000 0111 is not a byte, it is an integer.ok?

    I confuse and mixed!

Similar Threads

  1. Object casting
    By spiderweb in forum Advanced Java
    Replies: 5
    Last Post: 08-20-2009, 06:43 PM
  2. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 08:59 PM
  3. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 05:31 PM
  4. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •