Results 1 to 9 of 9
  1. #1
    moonlanka is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Smile Could you plese explain somebody how come output of these codes.

    class prog5
    {
    public static void main(String[]args)
    {
    char a='N';
    byte b = 125;
    short c=32767;

    System.out.println("a="+a);
    --a;
    System.out.println("a="+a);
    System.out.println("b="+b);
    ++b;
    System.out.println("b="+b);
    System.out.println("c="+c);
    ++c;
    System.out.println("c="+c);
    ++b;
    System.out.println("b="+b);
    ++b;
    System.out.println("b="+b);
    System.out.println("result1="+b%c);
    System.out.println("result2="+b/c);

    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Which output don't you understand?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    Read up on arithmetic operators and integer arithmetic.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by moonlanka View Post
    class prog5
    {
    public static void main(String[]args)
    {
    char a='N';
    byte b = 125;
    short c=32767;

    System.out.println("a="+a);
    --a;
    System.out.println("a="+a);
    System.out.println("b="+b);
    ++b;
    System.out.println("b="+b);
    System.out.println("c="+c);
    ++c;
    System.out.println("c="+c);
    ++b;
    System.out.println("b="+b);
    ++b;
    System.out.println("b="+b);
    System.out.println("result1="+b%c);
    System.out.println("result2="+b/c);

    }
    }
    The output comes from all those System.out.println( ... ) calls.

    kind regards,

    Jos

  5. #5
    moonlanka is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Output for this program :
    a=N
    a=M
    b=125
    b=126
    c=32767
    c=-32768
    b=127
    b=-128
    result1=-128
    result2=0



    So want to know , How come result c=-32768 and result1 and result2

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Ask yourself what the maximum value of a short it. Then ask yourself what happens when this value is exceeded. Read your text book if you can't answer any of those questions.

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by moonlanka View Post
    Output for this program :
    a=N
    a=M
    b=125
    b=126
    c=32767
    c=-32768
    b=127
    b=-128
    result1=-128
    result2=0



    So want to know , How come result c=-32768 and result1 and result2
    heres the answer

    its to do with how the info is stored in binary. once you go above the higest amount, the most siognificant bit becomes a positive thus the overall value now gets represented in minus format.
    Also if you were to add a large enough value to it, then the value you'd expect wouldnt fit in the allocated memory space, and the data would overflow into the next byte alongside it

    Anways the system is called twos complement, read up on it here

    Two's complement - Wikipedia, the free encyclopedia
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  8. #8
    moonlanka is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks all experts, still I have problem in result1 and result2 in my program. I know the symbol % is remain and / division.

    c=-32768
    b=-128

    System.out.println("result1="+b%c);
    System.out.println("result2="+b/c);

    How is comes like,
    result1=-128
    result2=0

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by moonlanka View Post
    System.out.println("result1="+b%c);
    System.out.println("result2="+b/c);

    as you can see the result2 of b/c is 0, so the remain of result1 must be -128.

Similar Threads

  1. Can somebody explain me this plz
    By ccie007 in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 07:47 PM
  2. Help explain the FOR lines please
    By hydride in forum New To Java
    Replies: 2
    Last Post: 01-19-2010, 11:52 PM
  3. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 06:59 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  5. [SOLVED] Can anyone explain this pgm's output
    By haoberoi in forum New To Java
    Replies: 13
    Last Post: 11-10-2008, 01:30 PM

Tags for this Thread

Posting Permissions

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