Author Topic:   who can tell me why?
peter_gong01
greenhorn
posted May 13, 2000 05:24 PM             
int k = 1;
int i = ++k + k++ + +k;

what is the i result and why?

[This message has been edited by peter_gong01 (edited May 13, 2000).]

Java Nut
unregistered
posted May 13, 2000 08:17 PM           
What happened when you compiled and ran it?

peter_gong01
greenhorn
posted May 13, 2000 08:40 PM             
compile no problem. the i is 7, but why?

Jyoti Bhayana
unregistered
posted May 13, 2000 11:44 PM           
Hi,
int k = 1;
int i = ++k + k++ + +k;
According to me in the first ++k, k value has become 2
so the expression is 2 + k++ + +k
in the second k++ the value used in expession will be 2 but after
k++ the k value has become 3
so the expression is 2 + 2 +3 =7
This is the concept of prefix and postfix operators
in prefix the value is changed before using in expression
and in postfix value is changed after using in the expression
Thanks

rahul_mkar
greenhorn
posted May 14, 2000 12:35 AM             
hi
actually an additional plus is the cause of most confusion put in braces below. even if this plus is removed it will still give the same result of 7

int k = 1;
int i = ++k + k++ + (+)k;

[This message has been edited by rahul_mkar (edited May 14, 2000).]

Suresh
greenhorn
posted May 14, 2000 05:55 AM             
Additionally,
if all the spaces in between are removed, I got a compiler error saying invalid type expression.

peter_gong01
greenhorn
posted May 14, 2000 07:37 AM             
hi:
i think i understand the concept of prefix and postfix. i have another question for this
code below.
int k = 1;
k = ++k + k++ + k ;
System.out.println(k);

when i run the code in visual age for java 3.0, the result is 5.
when i run the code using jdk1.2 complier. i got result 7.
that means same code run under different virture machine will result different result.
is that a virture machine bug?
can anyone tell me?

[This message has been edited by peter_gong01 (edited May 14, 2000).]

Raghubir Mutum
greenhorn
posted May 14, 2000 10:57 AM             
Hi Everyone !

The first rule of thumb is (I am sure you all are aware of) -

"Operands are evaluated first from left to right, and then
associativity is performed from right to left."

The second rule of thumb is -

"Compilers have differences in interpreting
complicated/abusive codes (no offense)".

Before I get to Peter's question, lets try this simple example.

int[] a = {2,2};
int i = 1;

a[i] = i = 0;

As a result of the third statement, the array will be now {2,0},
and not {0,2}.

Applying the same rule to Peter's code:
int k = 1;
int i = ++k + k++ + +k;

we get
i = (++k) + k + (++(+k))
or, i = 2 + 2 + 3
or, i = 7

The confusion is about how the compiler interpretes the
operators between the second k and the third k. We'd think
the second k should get a post-fix incrementer and the
third k should get only a unary plus. This would have been
true if the two operands are different.

x++ + +y is interpreted as (x++) + (+y)

but,

x++ + +x is interpreted as (x) + (++(+x)).

Also, we have to remember that when you have even number
of operators between two operands, the second operand gets
a unary operator, and it needs a whitespace before it.
(x++++x) will not compile, but (x+++ +x) and (x+++x) will. Again, some compilers may choose to differ.

Anyways, these codes stretch the compiler to the limit,
and I hope we won't see any code like these in the real
business world.

But, I've to say, Peter, its a good one. If you are upto
the challenge, try this ....

a = b ? c : d ? e : f ? g : h ? i : j;

Thank you, all.


quote:
Originally posted by peter_gong01:
int k = 1;
int i = ++k + k++ + +k;

what is the i result and why?


[This message has been edited by peter_gong01 (edited May 13, 2000).]


satya5
ranch hand
posted May 14, 2000 04:00 PM             

Just wanted to bring your attn to a similar (almost exact)
discussion. In this post Maha explains an easy way to
understand such cases .... enjoy

Regds.

- satya

sean zang
greenhorn
posted May 14, 2000 04:50 PM             
Dear all;
int i = ++k + k++ + +k;
i=? who know?
Answer: It depends OS Platform.
It test it in different OS, and get different result. so Does C++

maha anna
bartender
posted May 14, 2000 05:44 PM             
sean zang,
All primitive types in Java are platform independent. All operations on primitives (both interal and floating point) will give SAME result irrespective of the platform. This is the beauty of Java. There are some sections which are JVM dependent (garbage collection algorithm /thread scheduling etc) but NOT size of primitives.

Can you be little bit elaborate on what you are trying to say?

regds
maha anna

Ajay Kumar
greenhorn
posted May 14, 2000 06:37 PM             
Hi Peter;
quote:

int k = 1;
k = ++k + k++ + k ;
System.out.println(k);
when i run the code in visual age for java 3.0, the result is 5.
when i run the code using jdk1.2 complier. i got result 7.

When I executed the the above expression in VA for Java 3.0, I got the answer as 7. though I was clear on why it was 7, the above remark by U thet U are getting different results prompted me to test in VA for Java.
But for the purpose of the Certification we should assume that we have just JDK 1.2 and nothing else I THINK.

Thnx
Ajay Kumar

maha anna
bartender
posted May 14, 2000 07:24 PM             
Yes. The Sun's SCJP2 Exam is based ob Sun's jdk and JLS.
regds
maha anna

|