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; [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. [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 The second rule of thumb is - "Compilers have differences in interpreting Before I get to Peter's question, lets try this simple example. int[] a = {2,2}; a[i] = i = 0; As a result of the third statement, the array will be now {2,0}, Applying the same rule to Peter's code: we get The confusion is about how the compiler interpretes the 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 Anyways, these codes stretch the compiler to the limit, But, I've to say, Peter, its a good one. If you are upto a = b ? c : d ? e : f ? g : h ? i : j; Thank you, all.
quote:
|
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
|
Ajay Kumar greenhorn |
posted May 14, 2000 06:37 PM
Hi Peter; quote: 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. Thnx
|
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
|
| | |