Different output for b = + 1 in java

I have executed the following program

    int b = 0;
    b=+1;
    System.out.println(b);
    b=+1;
    System.out.println(b);
    b=+1;
    System.out.println(b);

      

and got the output as 1 always. Why is b incremented in the first increment, and why isn't it incremented in the second and third increments of the operation?

+3


source to share


3 answers


Cancel the symbols =

and +

. Unary is+

not what you want.

b+=1;

      

or

b++;

      



or

++b;

      

Unary plus - b = (+1);

or just b = 1

.

+4


source


you make assignment here with value +1

int

literal allows leading +

and -

sign



Do you want to

b += 1

      

+4


source


b=+1

means b = +1

here +

is an operator unary

, and you just point sign to digit (which indicates a positive value), while you want to add an add operator and bind b += 1

means b = b +1

to increase the value.

+2


source







All Articles