Java fundamentals | Selenium Forum
M
Posted on 28/06/2016
Hi
In the following code in the system.out.println after t1.j and t2.i though you have incerement and decrement operator the output is printing as 5 and 5.Doesnt the increment and decrement operator throw error messages or compiling errors
Thanks
Hari.

public class Test {
int i;
int j;

public static void main(String[] args) {

Test t1 = new Test();
Test t2 = new Test();

t1.j=t2.i=5;
t1.i=t2.j=6;

System.out.print(t1.j++ + " " + t2.i--);

}
}

M
Replied on 28/06/2016

i'm confused is that code throwing an error or not?


M
Replied on 28/06/2016

Output is correct

++a increments and then uses the variable.
a++ uses and then increments the variable.

so here in t1.j is printed and later incremented
so here in t2.i is printed and later decremented

If you want it to increment and decrement then do below
System.out.print(++t1.j + " " + --t2.i);

Console:
6 4


M
Replied on 03/07/2016

[quote="ahkbly@gmail.com":1x99kjvv]Output is correct

++a increments and then uses the variable.
a++ uses and then increments the variable.

so here in t1.j is printed and later incremented
so here in t2.i is printed and later decremented

If you want it to increment and decrement then do below
System.out.print(++t1.j + " " + --t2.i);

Console:
6 4[/quote:1x99kjvv]


Wow Thanks for that