# on unary operators

• December 28th, 2013, 05:38 AM
tappetivamsi
on unary operators
DIFFERENCE BETWEEN THESE TWO PROGRAMS PLEASE EXPLAIN ME IN DETAIL HOW IT WORKS

public class a
{
public static void main(String[] args)
{
int i=0;
int j=i++ +i++ + i + i;
System.out.println(i);
System.out.println(j);
}
}

AND

public class B
{
public static void main(String[] args)
{
int i=0;
int j=i++ + i + i + i++;
System.out.println(i);
System.out.println(j);
}
}
• December 28th, 2013, 05:52 AM
GregBrannon
Re: on unary operators
Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers..

These two lines:

int j=i++ +i++ + i + i;

int j=i++ + i + i + i++;

are different. What about the differences don't you understand or about the results caused by them?
• December 28th, 2013, 06:00 AM
tappetivamsi
Re: on unary operators
am getting different outputs and i ve doubt how i value is passing pls clear my doubt
• December 28th, 2013, 07:06 AM
GregBrannon
Re: on unary operators
Write down:

(first line without unary operator)

j = i + i + i + i

substitute 0 for i, calculate j. Shat does j equal?

write down (or modify the line in the program) to:

j = i++ + i + i + i

substitute 0 for i. what does that equal?

write down (or modify the line in the program) to:

j = i++ + i++ + i + i

Keep going until YOU figure it out. You don't need anyone's help with adding together numbers less than 5.
• December 28th, 2013, 07:12 AM
Norm
Re: on unary operators
Another way to see what each expression evaluates to and when it returns its value is to use println statements:
Code :

``` int i = 1; // set initial value System.out.println("i++="+i++); // show value returned by i++ System.out.println("i="+i); // show value AFTER i++```