Public static void main (string args [] ) { int a =1 , b=2; system.out.println (--b - ++a + ++b - --a); system.out.println ("" +a+ ""+b); }
Answers
Question :-
int a = 1 , b = 2;
System.out.println(--b - ++a + ++b - --a);
System.out.println(""+a+""+b);
Program Statement :-
import java.util.*;
public class a
{
public static void main (String args[])
{
int a = 1, b = 2;
System.out.println(--b - ++a + ++b - --a);
System.out.println(""+a+""+b);
}
}
Output :-
0
12
Reason :-
--b : This is a prefix decrement. The rule of prefix decrement is to first decrement, then use.
So, the value of b is 2 and after decrement the value of b will be 1.
++a : This is a prefix increment. The rule of prefix increment is to first increment, then use.
So, the value of a is 1 and after increment the value of a will be 2.
++b : This is a prefix increment. The rule of prefix increment is to first increment, then use.
So, the value of b is 1 and after increment the value of b will be 2.
--a : This is a prefix decrement. The rule of prefix decrement is to first decrement, then use.
So, the value of a is 2 and after increment the value of a will be 1.
Mathematically :-
--b - ++a + ++b - --a
1 - 2 + 2 - 1 (Replace the values of a and b)
= 0