• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

i++i+++;

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pls checjk this code,
public class lost
{
public static void main (String [] args)
{
int [] array = new int[10];
int i = 5;
i+++i++;//produces a compile time error
System.out.println(i);
array[i++] = i++ + i++;//works fine here
System.out.println(i);
System.out.println(array[5] + " "+array[6] + " "+i);
}
}
this is some code i found in a tutorial (i dont rembember exactly but in i think Amit Tyagis Site) plss kan some 1 plss explain me in detail wats happennin here and y the i++i+++; wrks in the second case.
Also in the same tutorial
import java.io.*;
class Test
{
public static void method1() throws Exception
{
try
{
System.out.println("nothing");
}
catch (Exception e)
{
System.out.println("from catch(method1)");
}
}
public static void method2()
{
method1();
}
System.out.println("method2");

}
public static void main(String args[])
{
method2();
System.out.println("bye from main");
}
}
This dosent compile unless the throws clause is not removed from method1() wat is the other way ??
puttin the same throws clause in method2() dosent seem to work
AMIT ROY
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
That's too simple!
You haven't assigned this to an integer.
i++ + i++ is an expression which returns an integer. If you write the same code by assigning this value to an integer, then it would work perfectly fine!!
public class lost
{
public static void main (String [] args)
{
int [] array = new int[10];
int i = 5;
int j=i+++i++;//doesn't produce a compile time error
System.out.println(i);
array[i++] = i++ + i++;//works fine here bcoz'u have assigned it to an int
System.out.println(i);
System.out.println(array[5] + " "+array[6] + " "+i);
}
}
Hope I am clear.
Aparna
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Amit,
Aparna had cleared your first one...
For the second one.... you have to put a try-catch block in method2 becaz caller method should always catch the exception in the called method(which is specified by a throws statement) otherwise compiler won't accept..
Try the program again by just rewriting the method2 as
public static void method2(){
try{
method1();
}catch(Exception e){
System.out.println("anything");
}
}
Hope this would help you..
Ramesh
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Aparna,
Why do this code prints 6 even though I have not assigned i to any variable after incrementing it.
public class IntegerTest
{
public static void main (String args[])
{
int i =5;
i++;
System.out.println(i); //prints 6
}
}
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajiv, i is now 5 and when you increment it, it becomes 6!!! Get it? 5+1 = 6.
Shashi
 
Rajiv Ranjan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sasikant, but I already knew that.If you reffer back to Aparna's reply
int i = 5;
i+++i++;//produces a compile time error

but

int i = 5;
int j=i+++i++;//doesn't produce a compile time error
Aparna said
"i++ + i++ is an expression which returns an integer. If you write the same code by assigning this value to an integer, then it would work perfectly fine!!"

However I didn't assign the value to any interger after incrementing it.
Did you get my question now ?
Thanks
 
Amit Roy
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with u Rajiv Ranjan But kan some 1 pls explain me as to how i+++i++ work ie
i = 5;
i = i+++i++
then i becomes 11 HOW??
and how far kan we use it can we write i++++i++i++++i++i--.......
Also 1 more thing
byte fff = 4;
fff = fff++;
System.out.println("fff is " +fff);
out put is amazingly 4
and
byte fff = 4;
fff++;
System.out.println("fff is " +fff);
produces 4
kan some i explain this also
AMIT
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Are u sure about the second case
byte fff = 4;
fff++;
System.out.println("fff is " +fff);
it must give 5.


I agree with u Rajiv Ranjan But kan some 1 pls explain me as to how i+++i++ work ie
i = 5;
i = i+++i++
then i becomes 11 HOW??
and how far kan we use it can we write i++++i++i++++i++i--.......
Also 1 more thing
byte fff = 4;
fff = fff++;
System.out.println("fff is " +fff);
out put is amazingly 4
and
byte fff = 4;
fff++;
System.out.println("fff is " +fff);
produces 4
kan some i explain this also
AMIT

 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i+++i++
This is similar to writing..
a+b;
where a and b are 2 integer variables,
This is bcoz ++ has higher precedence than +
its similar to (i++)+(i++).
So why do u expect it to work without a substitution?
if i = 5, it gives 5 + 6 = 11.
U can always try like this..
System.out.println(i+++i++);
as we try
System.out.println(a+b);
[This message has been edited by lakshmi nair (edited November 09, 2000).]
 
Amit Roy
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Laxmi t ried the code which i haven written it indeed gives the answer i hav written
byte fff = 4;
fff++;
System.out.println("fff is " +fff);
the output is 4 not 5
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: although this post of mine was a good attempt at the time, it turns out to be long and wrong Read it for background, but continue to end to get the correct answer.
Paul R
I sincerely hope that this post does not sound like it's talking down to anyone. I also realize that this is rephrasing a few of the earlier posts.
First, let's agree that...
i+++i++ and i++ + i++
...are the same thing to the compiler. (And if you disagree, say so, because this is important.)
Second, if you write
int i = 5;
i++;
you have made an assignment to i (whether you wanted to or not)
i.e i++; and i = i + 1; are equivalent
So if you write
i+++i++; the compiler sees: i++ + i++; and gets confused because you cannot perform an addition without assigning it.
Wheras j = i+++i++; is just fine because now the addition can be assigned.
Everybody with me so far?
Now remember that in Java and, if you put the ++ after the variable (i++), it gets incremented *after* the assignment is made. If you put the ++ before the variable (++i) the assignment is made *before* the assignment is made.
So...
int fff = 4; //line 1
int ggg = 0; //line 2
ggg = fff++; //line 3
System.out.println("ggg is " +ggg); //line 4
In order
line 1: fff is assigned 4
line 2: ggg is assigned 0
line 3: ggg is assigned 4, then fff is incremented and assigned 5
line 4: Prints out: ggg is 4
But...
int fff = 4; //line 1
int ggg = 0; //line 2
ggg = ++fff; //line 3
System.out.println("ggg is " +ggg); //line 4
In order
line 1: fff is assigned 4
line 2: ggg is assigned 0
line 3: fff is incremented and assigned 5, then ggg is assigned 5
line 4: Prints out: ggg is 5
Now for the really confusing one...
int i = 5; //line 1
//i+++i++; Without comment, compiler error -
// nothing was given for the addition to be assigned to
int j = i+++i++; //line 2
System.out.println("j is " +j); //line 3
line 1: i is assigned 5
line 2:
First operation: int j = *i++*+i++
The complier will increment the first i, but waits until the second operation is complete.
Second operation: int j = i+++*i++*
The compiler will increment the second i, but waits until the third operation is complete.
Now that the second operation is complete, the first i is incremented to 6 (and the second i is still 5)
Third operation: int j = i++*+*i++
The compiler performs the addition (6+5) and j is assigned 11.
line 3 (finally!): Prints out j = 11
You can change the j to i and you will still get the same result.
Now how on earth can i be equal to two different things (seemingly) at the same time?!? Beats me. However, this is what it seems to do.
(Sigh) This post was too dang long, but I was trying to clear.

[This message has been edited by Paul Ralph (edited November 10, 2000).]
 
Amit Roy
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That surely took some of ur time Paul thanks but i am still throughly confused
i quote from your post:
Second operation: int j = i+++*i++*
The compiler will increment the second i, but waits until the third operation is complete.
//here after the second operation (ie i++ i should become 6 then y do u say in the next line that (second is still 5) also after the first step ie *i++* value of i is 6 so now in the second step shouldnt the value of i become 7 as i is now 6 so the final answer should b 6+7=13??
Now that the second operation is complete, the first i is incremented to 6 (and the second i is still 5)
Thanks for ur fff assingment explanation I understood that.
 
Paul Ralph
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's difficult to explain and understand. I just hope *I* understand it correctly. And...it turns out I don't understand it as well as I hoped.
<code>
import java.util.*;
public class T
{
public static void main(String args[])
{
int i = 5;
i = i+++i++; // same as i = i++ + i++
System.out.println("i = "+i);
int a = 5;
int b = 5;
int c = a+++b++; // same as c = a++ + b++
System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("c = "+c);
}
}
</code>
I was hoping that the output would be
a=11
b=6
c=6
d=11
This would have proven that the i's were treated like two separate variable, but instead the output is
a=11
b=6
c=6
d=10
So I'm afraid my theory was incorrect.
Well here's an explanation of what I *was* going for.
Two keys: First, that when the ++ comes after the variable in an assignment, such as i++, the increment comes after the assignment.
int i = 3;
int j = i++;
result j = 3, i = 4
That above part I am sure about.
This is the dubious (read: wrong) part
Second, somehow in a statment like this
j = i++ + i++
the two i's are treated as separate variables. The first i gets incremented, but the second one doesn't have time.
I'm sorry. Ever think of something really cool, but it just doesn't work? :{ Maybe someone else will be able to answer this.
Good luck
Paul
 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public static void main(String args[])
{
int i = 5;
i = i+++i++; // same as i = i++ + i++
System.out.println("i = "+i);
int a = 5;
int b = 5;
int c = a+++b++; // same as c = a++ + b++
System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("c = "+c);
}
}


This is not exactly the case as above.
In i++ + i++ we have the same variable i for the two ++ operations. thus 5 will result in 5 + 6. After the first i++, i will be incremented to 6 and the second ++ won't have time to operate, as u say it.
But with a++ + b++ there are two different variables a and b. One being incremented wont affect the others value.
Thus if a&b being 5, it is 5 + 5. After a++ a becomes 6 and after second b becomes 6.
Hope this helps
lakshmi
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value of fff should be 5 and NOT 4. I have tried it on my machine. r u sure u r using java?
raj.

i quote :
Yes Laxmi t ried the code which i haven written it indeed gives the answer i hav written
byte fff = 4;
fff++;
System.out.println("fff is " +fff);
the output is 4 not 5
 
Amit Roy
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Raj let me put this down finally and clearly:
byte b = 5;
b = b++;
output b = 4;
********************
byte b = 5;
b = ++b;
output b = 5;
********************
byte b = 4;
b++;
output is = 5
********************
byte b = 5;
++b
output b = 5
********************
I was wrong previously
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am quoting RHE on this:


These operators modify value of an expression by adding or sutracting by 1.
So for example in an <code>int</code> variable <code>x</code> contains 10, <code>--x</code> gives value of 9. Since in this case the expression --x itself describes the storage(the value of variable x) the resulting value is stored in x.
.....
.....
Let us look more closely at how the position of these operators affects their behaviour. If the operator preceds the expression,then the value of expression is modified before it takes part in the the rest of the calculation.this is called pre-increment or pre-decrement. Conversely, if the operator is positioned to the right of an expression, then the value that is used in the rest of the calculation is the original value of that expression and the increment or decrement only occcurs after the exression has been calulated.


Paul, I just did a little variation to your code, and got some very surprising results. Here goes ....
<code>
public class lost1
{
public static void main (String [] args){
int i = 5;
int j = 5;
int k = j+++j++;
System.out.println("i++"+ i++ +"i++"+ i++);
System.out.println("k="+k );
System.out.println("i="+i);
System.out.println("j="+j);
}
}
</code>
The output is:
<code>
i++5i++6
k=11
i=7
j=7
</code>
any comments anyone?
Shubhangi

[This message has been edited by Shubhangi A. Patkar (edited November 10, 2000).]
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt on the same topic
JLS 15.14.1 says
the result of the postfix expression must be a variable of a numeric type, or a compile time error occurs.
JLS 15.15.1 says

the result of prefix increment expression is not a variable, but a value.

what exacly does it signify. I thought that it would be possible to do something like x++ = y ( because result of x++ is a variable) but it doesnt work like that.
As pointed out by Jim, this was a case of careless reading on my part. The postfix expression is different from postfix increment expression. in x++ , x is the postfix expression and x++ is the postfix increment expression. so x is a variable but x++ results in a value. simple.
[This message has been edited by mohit joshi (edited November 12, 2000).]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul & Amit, I think Shubhangi's code solves a lot of problem for us.
----------------------
public class lost1
{
public static void main (String [] args){
int i = 5;
int j = 5;
int k = j+++j++;
System.out.println("i++"+ i++ +"i++"+ i++);
System.out.println("k="+k );
System.out.println("i="+i);
System.out.println("j="+j);
}
}
The output is:
i++5i++6
k=11
i=7
j=7
// What we have been thinking when Paul wrote
-------------------------------------------------------------
Now for the really confusing one...
int i = 5; //line 1
//i+++i++; Without comment, compiler error -
// nothing was given for the addition to be assigned to
int j = i+++i++; //line 2
System.out.println("j is " +j); //line 3
line 1: i is assigned 5
line 2:
First operation: int j = *i++*+i++
The complier will increment the first i, but waits until the second operation is complete.
Second operation: int j = i+++*i++*
The compiler will increment the second i, but waits until the third operation is complete.
Now that the second operation is complete, the first i is incremented to 6 (and the second i is still 5)
Third operation: int j = i++*+*i++
The compiler performs the addition (6+5) and j is assigned 11.
line 3 (finally!): Prints out j = 11
You can change the j to i and you will still get the same result.
----------------------------------------------------------
// that as per Paul's code
int i = 5;
int j = i+++i++;
// j = 11;
// i = 7;
/* the java compiler before doing any addition increments the first i and keeps the second copy of the i at post incremental value. i.e. in (i++)+(i++) the first i becomes 6 and the second i remains with the value 5.
But now after going through Shubhangi's code, it seems that the java compiler actually keeps the value of first i at 5 for before performing any increment on it, gives the second i the expected incremented value of i i.e. 6 and goes for the addition operation.
Thus we can get from
System.out.println("i++"+ i++ +"i++"+ i++);
The output is:
i++5i++6
After the addition is performed, it increments first i from 5 to 6 and the second i from 6 to 7;
Thus
int j = i+++i++;
The output is:
i = 7;
Note: If the value of first i was incremented in the first chance to 6 and the second i was kept at 5 then the last increment would have kept the value of i finally at 6 not 7.
*/
Anyway I may be wrong but trying to guess... Correct me if I'm off the track
Thanx
Rajib
 
Shubhangi A. Patkar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajib,
You hv managed to put the things much better than i hoped to put accross.(My post was getting so long, i decided to let the code do talking. )
Actually what you said makes a lot of sense if you are able to grasp the concept put forward by RHE.
RHE says:


Conversely, if the operator is positioned to the right of an expression, then the value that is used in the rest of the calculation is the original value of that expression and the increment or decrement only occcurs after the exression has been calulated.


Thus according to the steps you wrote...
First operation: int k = *j++*+j++
The complier will increment j, but uses the original value of j in the expression, which of course is 5. The postscript expression has been now been calculated therefore value of j is now 6 for everyone accessing it.
Second operation: int k = j+++*j++*
The value of j is now 6. The compiler will increment the i again. The value of j is now 7, but uses the (new) original value of j in the expression, this is 6.The postscript expression has been now been calculated therefore value of j is now 7 for everyone accessing it.
Third operation: int k = j++*+*j++
The compiler performs the addition (5 + 6) and k is assigned 11.
line 3 (finally!): Prints out k = 11
-----------------------------------------------------------------
The value of j has already been incremented to 7, no extra step is required anywhere.
I hopr I hv managed to put this in a simple manner.
Mohit,
when you say:
JLS 15.14.1 says
the result of the postfix expression must be a variable of a numeric type, or a compile time error occurs.
In next few lines itself it also says that


The result of the postfix increment expression is not a variable, but a value.


The result of x++ is a variable maybe in the sense that it can vary at runtime depending on the value of x. or may be in the sense that you can carry out any further operation on it. It could also mean that you can not use a numeric literal there.I am not very sure. But the x++ itself is an expression, so the result of it will be a value. Therefore you cannot put it on LHS of an equation.
I hope I hv managed to answer your question.
Anybody has any vomments on the "variable" part of answer to Mohits Q? I am not very sure what exactly it means, I just put some of the possiblities that occurred to me.
Shubhangi.
[This message has been edited by Shubhangi A. Patkar (edited November 10, 2000).]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I can clarify all of this stuff.
The important thing that you need to know is that Java does arithmetic by first evaluating all of the operands from left to right, THEN doing the arithmetic on the results of those evaluations. In the case of the increment operator, it's all a little trickier, but it still makes sense.
First, the increment operator.
It does two things: increments the variable, then returns (evaluates to) a value. That value is controlled by whether you put the ++ before or after the variable.
i++ means increment i to 6, but evaluate as i BEFORE incrementing, so it returns 5.
++i means increment i to 6, and evaluate as i AFTER incrementing, so it returns 6.
BOTH of these examples increment i to 6.

Let's look at the examples again.

byte fff = 5;
fff = fff++;
System.err.println( "fff is " + fff );

Here's what happens:
1. fff is 5
2. the operand is evaluated: fff is incremented to 6, but returns 5.
3. the assignment is done. fff (which was 6, very briefly) gets assigned back to 5.
4. 5 gets printed out.

Now, look at the original example:

i = 5;
i = i++ + i++; // I'm putting in the spaces for readability
System.err.println( "i is " + i );

Here's what happens:
First, Java evaluates the operands, from left to right.
1. the first i++ increments i to 6, and returns 5.
2. the second i++ increments i again, from 6 to 7, and returns 6.
Java now does the arithmetic using the evaluated operands. In the first steps, it basically translated

i++ + i++

into

5 + 6

...hence the last steps.
3. adds 5 + 6
4. assigns 11 to i
5. prints out 11

As for the latest example:

int i = 5;
int j = 5;
int k = j+++j++;
System.out.println("i++"+ i++ +"i++"+ i++);
System.out.println("k="+k );
System.out.println("i="+i);
System.out.println("j="+j);

Here's what happens:
1. on line 3, the operands of j+++j++ are evaluated into 5 + 6, leaving j equal to 7 (it was incremented twice)
2. k is assigned 11 (result of 5 + 6)
3. in the first println, i is incremented twice (up to 7), evaluating to 5 then 6
4. 5 and 6 are converted to strings, concatenated, etc., and "i++5i++6" gets printed out
5. the current values of i, then j get printed out with no further modification

As was seen the output is:
i++5i++6
k=11
i=7
j=7

[This message has been edited by Rob Whelan (edited November 10, 2000).]
 
Amit Roy
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys;
lots and lots of answers tanx a ton but i am sorry as i kuldnt respond i was a bit bzy ill get bak vry soon ice i read all the stuff
Amit
 
Amit Roy
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooffff++!! +finally got it++!!
Thanks a lot Shubhangi, Rob, Rajib, Mohit, Raj, Laxmi , Paul, Rajiv, Sasikant, Ramesh, and Aparna hope didnt miss ne 1 That surely took some dissucssion but finally it was solved ,But isnt it strange in the part of the compiler to increment a value and still return the original value ??(though this is wat happens) .
Thanks ++ Again++
Amit Roy
I am having 1 more of such a question which ill b putin up vry soon so watch out!
 
Rajib D
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All of U,
After solving Amit's problem (thou i joined late) together, I've got another. First for the code.
public class lost_again{

public static void main(String args[]){
int x, y;
boolean b1, b2;
b1 = b2 = true;
x = y = 0;
x = (b1 | b2)?x++:--x ;
y = (b1 | b2)?++y:--y ;
System.out.println(" x = "+x);
// the output is
// x = 0;

System.out.println(" y = "+y);
// the output is
// y = 1;
}
}
In both the case the value of the expression evaluates to be true, the value of x before increment, being 0, is returned and then x is incremented while in case of y, the value of y is first incremented and then returned, i.e. 1 is returned.
No doubts about what kind of value is returned by x and y respectively. But the query I've is the value shown by
System.out.println(" x = "+x);
It is understood that from x = (b1 | b2)?x++:--x ; x is assigned the value of 0, but my question is that after returning value, x does get incremented to 1.
SO WHERE DOES THIS VALUE GO ???
Why System.out.println(" x = "+x); should show the assigned value of x NOT the incremented value of x.
Hmmm!!! Goes above my head.....
Can anybody help???
Thanx in advance.
Rajib
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result seems to be correct.
In your statement
x = (b1 | b2)?x++:--x ;
since (b1 | b2) value is true, x++ stmt
will execute and returns 0 and x value becomes 1, BUT immediately you are assaigning the returned value(0 here) to x AGAIN..!!
That's why you are still seeing x value as zero.
If you try this
int z = (b1 | b2)?x++:--x ;
System.out.println("x :" + x); // value 1 would be printed
System.out.println("z :" + z); // value 0 would be printed
Enjoy...
 
Shubhangi A. Patkar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajib Here goes answer for your code...
public class lost_again{

public static void main(String args[]){
int x, y;
boolean b1, b2;
b1 = b2 = true;
x = y = 0;
x = (b1 | b2)?x++:--x ;
y = (b1 | b2)?++y:--y ;
System.out.println(" x = "+x);
// the output is
// x = 0;

System.out.println(" y = "+y);
// the output is
// y = 1;
}
}
Your Question was:


In both the case the value of the expression evaluates to be true, the value of x before increment, being 0, is returned and then x is incremented while in case of y, the value of y is first incremented and then returned, i.e. 1 is returned.
No doubts about what kind of value is returned by x and y respectively. But the query I've is the value shown by
System.out.println(" x = "+x);
It is understood that from x = (b1 | b2)?x++:--x ; x is assigned the value of 0, but my question is that after returning value, x does get incremented to 1.
SO WHERE DOES THIS VALUE GO ???
Why System.out.println(" x = "+x); should show the assigned value of x NOT the incremented value of x.


You see the ++ operator has a higher precedence. So the expression gets evaluated first, and then the assignment takes place. OK So far?
So what happens is:
1. x++ is evaluated, but because the ++ is postfix, the original value of x is used in the equation. the new value of x is already 1.
2. x is now assigned the original value of x i.e. 0. thus the increment operation is effectively lost.
If you had done
j= (b1 | b2)?x++:--x ;
then
the value of j would have been 0 and x would have stayed 1 as there is no assignment of a new value to variable x.
got it?
Shubhangi.
[This message has been edited by Shubhangi A. Patkar (edited November 13, 2000).]
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vasu n, may I ask you to register with proper name?
You can read this post for more details. We are glad to see you here, just a little formality�
 
Rajib D
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Shubhangi & Vasu,
I had no doubt about what kind of value is returned by x in the equation
x = (b1 | b2)?x++:--x ;
but I was not sure of what was happening to the incremented value of x after it is returning the original postfix value. But I think it is now clear, as Shubhangi you have written
"2. x is now assigned the original value of x i.e. 0. thus the increment operation is effectively lost. "
the incremented value is lost. It would not have been if it was assigned to a different variable.
Thanx again

Rajib
 
reply
    Bookmark Topic Watch Topic
  • New Topic