The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Help: "--" and "="

Posted by YJ on September 10, 2000 at 12:04 PM


I am a beginner in Java and have a question regarding "=" and "++".
Listed below are a short program I tried to run and its execution result.
Could anyone please be kind enough to let me know
why the outputs following "6:" and "14:" are what they are?
It seems that "--" doesn't have any effect in those two
statements: "n1.i = n2.i --;" and "a = a --;".
(Please do not question why I put "++" and "=" in the same
statement. I was just curious.)
I'll appreciate your answer very much.


// Why1.java

class Number {
int i;
}

public class Why1 {
public static void main (String[] args) {
Number n1 ;
Number n2 = new Number();
n1 = n2;
n2.i = 40;
System.out.println( "4: n1.i=" + n1.i + " n2.i=" + n2.i);
n1.i = -- n2.i;
System.out.println( "5: n1.i=" + n1.i + " n2.i=" + n2.i);
n1.i = n2.i --;
System.out.println( "6: n1.i=" + n1.i + " n2.i=" + n2.i);

int a ;
int b = 10;
a = -- b;
System.out.println( "11: a=" + a + " b=" + b);
a = b --;
System.out.println( "12: a=" + a + " b=" + b);
a = -- a;
System.out.println( "13: a=" + a + " b=" + b);
a = a --;
System.out.println( "14: a=" + a + " b=" + b);
}
}

// execution result

4: n1.i=40 n2.i=40
5: n1.i=39 n2.i=39
6: n1.i=39 n2.i=39
11: a=9 b=9
12: a=9 b=8
13: a=8 b=8
14: a=8 b=8





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us