Results 1 to 9 of 9
- 06-23-2010, 12:45 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
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);
}
}
- 06-23-2010, 12:57 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Which output don't you understand?
- 06-23-2010, 01:46 PM #3
Read up on arithmetic operators and integer arithmetic.
- 06-23-2010, 03:31 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 06-24-2010, 08:13 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
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
- 06-24-2010, 08:35 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
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.
- 06-24-2010, 08:40 AM #7
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 encyclopediaTeaching myself java so that i can eventually join the industry! Started in June 2010
- 06-24-2010, 09:12 AM #8
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
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
- 06-24-2010, 09:59 AM #9
Similar Threads
-
Can somebody explain me this plz
By ccie007 in forum New To JavaReplies: 4Last Post: 05-20-2010, 08:47 PM -
Help explain the FOR lines please
By hydride in forum New To JavaReplies: 2Last Post: 01-20-2010, 12:52 AM -
Can someone explain why...
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-19-2009, 07:59 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-26-2009, 12:44 AM -
[SOLVED] Can anyone explain this pgm's output
By haoberoi in forum New To JavaReplies: 13Last Post: 11-10-2008, 02:30 PM
Bookmarks