In this circumstance what is the value of variable y after the first two statements? I'm assuming it's Integer 7 but my book says automatic unboxing of objects only occurs with relational operators < >". I'm a little confused how variable Integer y gets it's value. Does any unboxing occur in newInteger(x)?
Integer x = 7;
Integer y = new Integer(x);
println( "x == y" + " is " + (x == y))
Unboxing happens when the compiler is certain that you wish to compare values. Using == can compare the Objects and therefore give false because == is a valid operation between objects. With < and > there is no concept of Object < OtherObject so it is certain that you mean numerically.
public void test() {
Integer x = 7;
Integer y = new Integer(x) + 1;
System.out.println("x == y" + " is " + (x == y));
System.out.println("x.intValue() == y.intValue()" + " is " + (x.intValue() == y.intValue()));
System.out.println("x < y" + " is " + (x < y));
System.out.println("x.intValue() < y.intValue()" + " is " + (x.intValue() < y.intValue()));
}
x == y is false
x.intValue() == y.intValue() is true
x < y is true
x.intValue() < y.intValue() is true
In this circumstance what is the value of variable y after the first two statements?
The value of the variable y is a reference to an Integer object containing the value 7.
Integer x = 7;
In this case, the int literal 7 is automatically boxed into the Integer variable x.
Integer y = new Integer(x);
This involves an automatic unboxing of the Integer variable x into an int (temporary) variable, which is passed to the Integer constructor. In other words, it is equivalent to:
Integer y = new Integer(x.intValue());
After this statement, y points to a new object that is different than x but containing the same int wrapped value.
Related
Reading Java Headfirst found this example pls if someone can help me understand.
class Scratch {
public static void main(String[] args) {
int x = 0;
int y = 0;
while ( x < 2 ) {
y = y + x;
System.out.print(x + "" + y + " ");
x = x + 1;
}
}
}
I can't wrap my head around whats the function of quotes here in print statement the results vary a lot if you remove them.
The second one adds "Space" but the first somehow adds another integer?!
This is a common Java idiom to convert a number to a string:
int x = 1;
System.out.println(x + "" + x); // prints 11
In Java the + operator is overloaded to mean either addition or string concatenation, depending on the operand types.
What happens here is that x + "" is interpreted as a string concatenation rather than an addition.
x + "" -> 1 + "" -> "1"
"1" + x -> "1" + 1 -> "11"
Now there are some people who say that x + "" should be written as String.valueOf(x). They say that the latter is more efficient. In reality, it depends on how good the JIT compiler is at optimizing string concatenation expressions, and that varies with the Java version.
Running this code will return 11 while I was expecting 20. Why is that so?
int x = 1;
int y = x + (x = 10);
System.out.println(y);
Evaluation is from left to right. So
int y = x + (x = 10);
is (with x initially 1):
int y = 1 + 10;
Putting the assignment in () doesn't make it come first. It just ensures that it's a valid expression, since y = x + x = 10 would be y = (x + x) = 10 which would require assigning to something (x + x) that wasn't a variable.
If you want 20, put the assignment first:
int y = (x = 10) + x;
Of course, the vast majority of the time, it's best to avoid these kinds of side-effects and assign x a value outside the expression, breaking the expression up if necessary. Assignment-within-expression can be useful sometimes (particularly while ((blah = getNextBlah()) != null) sort of things), but only in limited situations.
I read a few post regarding unary operator:
What is the Difference between postfix and unary and additive in java
"C - C++" joke about postfix/prefix operation ordering
And a few more.
However, I still don't understand exactly when the value is changed.
For example:
int x = 1;
x = x++;
System.out.print("x = x++ ==> ");
System.out.print(" x = " + x);
System.out.println();
int x = 1;
x = x++ + x++;
System.out.print("x = x++ + x++ ==> ");
System.out.print(" x = " + x);
System.out.println();
The output is:
x = x++ ==> x = 1
x = x++ + x++ ==> x = 3
So in the first block x is assigned to x and afterwards incremented, but the value is never used, otherwise the output would have been x = 2.
In the second block, if I understand correctly, the first x++ is evaluated before the assignment and the second x++ is evaluated afterwards but is never used.
If in the second block both x++ would have been evaluated after the assignment but never used, the output would have been x = 2. If both have been used, the output would have been x = 4.
My IDE also indicated that the first x++ is used, but the second is not used:
So to conclude - I'm still confused about when and how exactly the increment is done.
At the line
x = x++ + x++;
Assuming x = 1, the first x++ returns "1" as the value, and then it increments x to 2. So basically, it's assigning the old value back to x.
The second x++ does the same; it returns the value of x, which is now 2, and only then increments its value to 3 - that value, is not used.
Your code is equivalent to:
tmp = x;
x = x + 1;
tmp2 = x;
x = x + 1; // not used
x = tmp + tmp2;
Links that may help you:
JSL - 15.14.2. Postfix Increment Operator ++
JLS - 15.15.1. Prefix Increment Operator ++
What is x after “x = x++”?
This question already has answers here:
Ternary operator, syntax error when using assignment
(4 answers)
Closed 8 years ago.
I have the following piece of code. This is how I understand it.
In the first case, the ternary operator returns the value of y because x=4 and the print statement prints 5, as expected.
In the 2nd case, the ternary operator first assigns the value of y to x and then returns that value. Again, it prints 5, as expected.
In the 3rd case, the ternary operator has x=y to the left of the : and x=z to the right of the :. I would expect this to behave much like the 2nd case. However, this statement does not even compile.
Any help in understanding this will be much appreciated.
public class Test {
public static void main(String[] args) {
int x = 4;
int y = 5;
int z = -1;
x = (x == 4) ? y : z; // compiles and runs fine
System.out.println(x + " " + y + " " + z);
x = (x == 4) ? x = y : z; // compiles and runs fine
System.out.println(x + " " + y + " " + z);
x = (x == 4) ? x = y : x = z; // Does not compile
System.out.println(x + " " + y + " " + z);
}
}
Assignment has lower precedence than a ternary expression, so this expression:
(x==4)?x=y:x = z;
can be thought of as:
((x==4)?x=y:x) = z;
Which obviously won't compile because you can't assign a value to something that isn't a variable.
Add parenthesis to control the order of evaluation
x = (x == 4) ? (x = y) : (x = z); // Does compile.
Note the above is equivalent to
if (x == 4) {
x = (x = y);
} else {
x = (x = z);
}
Which will (as a side effect) of assigning a value to x assign the value assigned to x to x. In other words, your ternary is equivalent to
x = (x == 4) ? y : z;
or
if (x == 4) {
x = y;
} else {
x = z;
}
The ternary is specified in JLS-15.25. Conditional Operator ? :.
Can anyone tell me the reason for the change in output.
public class Demo {
public void demo()
{
Integer y = 567;
Integer x = y;
System.out.println(x + " " + y);
System.out.println(y == x);
y++;
System.out.println(x + " " + y);
System.out.println(y == x);
y--;
System.out.println(x + " " + y);
System.out.println(y == x);
}
public static void main(String args[])
{
Demo obj = new Demo();
obj.demo();
}
}
OUTPUT:
567 567
true
567 568
false
567 567
False
Here why i'm getting the final false.
You are using Integer which is an immutable object.
Basically your code is
y = new Integer(y.intValue() + 1);
and
y = new Integer(y.intValue() - 1);
Therefore you're creating two new Integer objects that are not the same (==) as the previous objects.
This behaviour is called autoboxing in Java.
Change your
Integer y = 567;
Integer x = y;
to
int y = 567;
int x = y;
and the suprprising behavior will be gone. My guess is that you have stumbled upon Java's implicit autoboxing of primitive values into wrapper objects, and are lead to believe that you are directly manipulating the numbers.
This is because the compiler does this internally:
y--
means:
int _y = y.intValue();
_y--;
y = Integer.valueOf(_y);
Therefore, y is has a new Integer instance. You are doing Object reference check (when using ==) and not value equality check.
Use equals() method to evaluate 2 values.
Integer y = 567; // y=567
Integer x = y; // x is equal to y object
System.out.println(x + " " + y); // out put x and y so obviously x and y are 567
System.out.println(y == x); // here x and y are in same reference. so this x==y is true and out put is true.
y++; // increment y by 1. then y=568
System.out.println(x + " " + y); // now x= 567 and y= 568
System.out.println(y == x);// now x not equals to y then false will print
y--; // again decrement y value
System.out.println(x + " " + y); // again x and y are same 567
System.out.println(y == x);// here y.value == x.value but x and y object wise not equal since object x and y are referring deference points
Because x and y are referring to 2 different objects.
y--;
This first unboxes y to int than decrements it and than boxes it to Integer. And this new boxed integer is referring to a different memory location.
Ideally, on wrapper classes, its better to call equals method on wrapper object rather than == operator.
y == x checks for content-equality, that is, whether they pointed to the same object, not whether the object they pointed to contains the same int. Especially when x, y >= 128.
Use
y.equals(x);
or
(int) y == (int) x
or declare x and y as int instead.
Note that auto-unboxing doesn't happen in Integer == Integer or Integer != Integer.
When you use primitive int instead of the Integer. The final output would be true.
However when you use Integer class, these are Objects. If you use the equals method the final output would be true.
Even, if you create
Integer a = new Integer(1000);
Integer b = new Integer(1000);
a==b - false
but for
Integer a = new Integer(1);
Integer b = new Integer(1);
a==b - is true
In java there is a cache of small integers: -127 to 128. All other integers are newly created objects and they can not be equals.