What does this print?

Found this Java interview question on another site, but it doesn't give the answer.

public class test {

public static void main(String[] args)

{

int i=5;

i=i++;

System.out.println("result::"+i);

}

}

I would guess it print 6 but i'm not too sure.

[308 byte] By [Jahvaha] at [2007-11-27 11:03:20]
# 1

FFS it would take you a fraction of the time it took to post this, just to compile and run it!

georgemca at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 2

You're right i should have tested it first, but i was hoping for an explanation not just the answer, and i knew many here would know the answer without testing it.

There was user comments on the website and most of them thought the answer was 5 , so i was kinda assuming i was wrong about it being 6.

I just tested it and got 5. I thought the prefix increment happened AFTER the line: "i=i++;" was executed, therefore "System.out.println("result::"+i);" would print 6.

Jahvaha at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 3

1. It's postfix not prefix, this is prefix++i

2. The postfix operator is applied after the evaluation of the expressioni++; // result of expression is value of i before increment

3. So i gets assigned the value of evaluating the expression i++, meaning that i gets assigned the value of i before the increment.

It's basically like thisi = 5

temp = evaluate the expression i++ // temp is now 5

increment i // i is now 6

i = temp // i is now 5

dwga at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 4

int i=0;

System.out.println(i++ +" "+i+" "+ ++i +" "+ i-- +" "+ --i);

If you can make a good guess before you compile, You got them better!

:-)

passion_for_javaa at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 5

Hey guys,

just try that example with a C-compiler. The result will be 6, so maybe there is something wrong in the Java world ?

dietmar.paulusa at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 6

> Hey guys,

>

> just try that example with a C-compiler. The result

> will be 6, so maybe there is something wrong in the

> Java world ?

Why would you say that? If you expect Java to do everything exactly the same as C then why would we need it?

Anyway, the result of this code is most likely undefined in C; however, it is well defined in Java. Either way, this type of coding is considered a VeryBadThing(TM).

jbisha at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 7

> so maybe there is something wrong in the

> Java world ?

No, nothing wrong in the Java world

The behaviour of this idiotic construct was never defined for C, so you get whatever the specific compiler gives you.

The Java behaviour, on the other hand, is clearly defined, and you consistently get the expected result.

roger.bagnalla at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 8

Guys you are right.

BTW: look at http://skeletoncoder.blogspot.com/2006/09/java-tutorials-i-i.html

dietmar.paulusa at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 9

> Guys you are right.

Yeah, we know. ;o)

It's not the first (or seemingly thousandth) time this has been discussed around here. Notice the similarity between the blog post you mention (September, 2006) and the explanations given previous to that post:

http://forum.java.sun.com/thread.jspa?threadID=725106&messageID=4179124

http://forum.java.sun.com/thread.jspa?threadID=755675&messageID=4320434

http://forum.java.sun.com/thread.jspa?threadID=770353&messageID=4389965

~

yawmarka at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 10

> Hey guys,

>

> just try that example with a C-compiler.

Not necessarily. In C, it's undefined, so some might be 5 and some might be 6.

> The result

> will be 6, so maybe there is something wrong in the

> Java world ?

That makes no sense. You're saying C is always "correct" and anything in Java that's different is "wrong"?

Java is behaving exactly as it's defined to, as explained above. C doesn't define what happens here.

jverda at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...
# 11

> 1. It's postfix not prefix, this is

> prefix

post-increment/pre-increment.

jverda at 2007-7-29 12:48:56 > top of Java-index,Java Essentials,Java Programming...