What is ++int in Java? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
explain working of post and pre increment operator in Java
What is the difference between int++ and ++int?
In Java, what is ++int? What does it do? What does it mean?
(Sorry, but I didn't ask the question correctly last time.)

a = 5; b = ++a; // a = 6, b = 6
a = 5; b = a++; // a = 6, b = 5

int a=1;
System.out.println(a++);
prints "1"
int a=1;
System.out.println(++a);
prints "2"
Or maybe i don't understand your question.

++int increments int by 1 before and int++ increments by one after

Related

java pre increment and post increment and self-assignment in loop [duplicate]

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Java increment and assignment operator [duplicate]
(6 answers)
Closed 4 years ago.
I expected post-increment and pre-increment of a variable and assignment of the result to itself to work a bit differently. But while the latter works as expected, the former runs as infinite while loop. Could someone please point out what i am missing here?
int y = 0;
int z = 4;
while(y<z)
{
System.out.println(y);
y =y++;//this prints 0 infinite times, shouldn't why be assigned values 0,1,2,3 with each pass?
//y =++y;//this works as expected
}
Thank you
As described in this StackOverflow answer, post increment works by storing a copy of y, adding 1 and returning the copy. This means that the return value of y++ is not y+1, but still only y. Since you are overwriting y with y++, you are essentially just saying y = y.

Java println() of integers? [duplicate]

This question already has answers here:
How to concatenate int values in java?
(22 answers)
Closed 7 years ago.
I want to print the two integer variables divided.
int a = 1, b = 2;
System.out.println(a + b);
Obviously println() function processes them as integers and calculates the sum.
Instead I would like the output becomes like this "12". Any ideas?
Insert some blank Strings to induce this:
System.out.println(a +""+ b);
Store the integers as strings:
String a = "1";
String b = "2";
String c = a + b;
System.out.println(c);

Java equals plus operator [duplicate]

This question already has answers here:
What is the difference between a += b and a =+ b , also a++ and ++a?
(9 answers)
Closed 8 years ago.
I need to explain this strange operator =+ (equal plus)
Example #1:
Double a = new Double(5);
Double b = new Double(10);
a += b
result:
a=15.0
b=10.0
Example #2:
Double a = new Double(5);
Double b = new Double(10);
a =+ b
result:
a=10.0
b=10.0
I understand the first example, but please explain me what this =+ operator did in example no.2.
And another interesting fact is, that these operators are valid and compilable:
+=, -=, *=, /=
but any of these two won't compile:
=*, =/
=+ is the assignment operation and the unary + afterwards. It's perfectly valid and what happens is:
a = (+b);
It's pretty much the same when you want to assign the negative value of a variable to another variable:
a = (-b); //a will be assigned with -10
Also, =* doesn't compile, because there's no * unary operator.

Swapping two variables in Java without using a third variable or an API [duplicate]

This question already has answers here:
Swapping two variable value without using third variable
(31 answers)
Closed 8 years ago.
I recently went to an interview and I was asked to write some code to swap the values of two variables without using third variable or any API.
I could not work out how to do this, can you help me on this?
For example I have two variable a=10 and b=20 the output should be b=10 and a=20.
Tricky but not that hard to figure out:
a = 10
b = 20
a = a + b; //a = 30, b = 20
b = a - b; //a = 30, b = 10
a = a - b; //a = 20, b = 10

i++ still working for immutable Integer in Java? [duplicate]

This question already has answers here:
Is Integer Immutable
(12 answers)
Closed 9 years ago.
I know Integer is immutable in Java. But I tried this:
Integer i = 4;
i++;
System.out.println(i); // output is 5
Why the self-increment is still working? Did Java create a new Integer object?
i++;
is equivalent to:
i = i + 1;
which i now refers to a different Integer object (5).

Categories

Resources