This 'if' evaluation in Java with three simultaneous expressions [duplicate] - java

This question already has answers here:
How to make loop infinite with "x <= y && x >= y && x != y"?
(4 answers)
Closed 7 years ago.
I had this question in my Java test where I had to assign values to a and b so this expression evaluates to true:
(a<=b && b<=a && a!=b)
Sadly, I had no idea what the answer was.

There's a simple trick here.
You cannot think this through with boolean logic only. Using that, this combination...
a is less than or equal to b, and
b is less than or equal to a, and
a is not equal to b
...would never return true.
However, the != operator compares references if its operands are objects.
So, the following will return true:
Integer a = 1;
Integer b = new Integer(1);
System.out.println(a<=b && b<=a && a!=b);
What happens here is: a as an object reference is not equal to b as an object reference, although of course they hold equal integer values.

Related

Why does comparing Integer values using == and equals() give different results? [duplicate]

This question already has answers here:
Java: Integer equals vs. ==
(7 answers)
How can I properly compare two Integers in Java?
(10 answers)
Closed 4 years ago.
Consider:
Integer i = 11;
Integer j = 11;
Integer h = 10000;
Integer k = 10000;
System.out.println((i==j));
System.out.println((i.equals(j)));
System.out.println((h==k));
System.out.println((h.equals(k)));
The output is:
true
true
false
true
Actually for h==k, it should also give true. What is the explanation?
Because == checks object references while equals checks actual values. They are not guaranteed to yield the same result.
In some cases they do give the same result, but that's because your JVM is interning some of the Integer objects. Meaning, it maintains a cache of integer objects. So for example if you ask for an Integer value of 10, it might return the same object instance. But there are no guarantees as to which values would be interned. So it is always advisable to use equals instead of relying on ==.

Will this if statement throw an exception or ignore it? [duplicate]

This question already has answers here:
Java logical operator short-circuiting
(10 answers)
Closed 7 years ago.
Will this code not evaluate the divide by zero portion of the if statement since the first part evaluates to false? If so, is this true for all cases in all Java IDEs? Or will certain compilers throw the exception?
int n = 0;
int x = 5;
if (n != 0 && x / n > 100) {
System.out.println(" s1");
} else {
System.out.println("s2");
}
From JLS §15.23:
The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.
So no, you will not get an exception.
Obviously this assumes that you have a single-threaded or thread safe environment - if you introduce visibility problems into the mix then it's anyone's guess.
Assuming we include the import for SoP, and that thoses lines are written inside a static main, there will be no exception thrown, and s2 will be printed.
X/0 will never be evaluated in your code because && check left expression first, thus, it will never throw anything.

In java equal and == behaviour [duplicate]

This question already has answers here:
Java: Integer equals vs. ==
(7 answers)
Closed 8 years ago.
Please can you explain the below behaviour.
public class EqAndRef {
public static void main(String[] args) {
Integer i = 10;
Integer j = 10;
Double a = 10D;
Double b = 10D;
System.out.println(i.equals(j));
System.out.println(i == j);
System.out.println(a.equals(b));
System.out.println(a == b);
}
}
Output on jdk 6
true
true
true
false
why a==b is false and i==j not false?
The Integers i and j are constructed (via auto boxing) from integer literals from the range –128 to 127 and thus are guaranteed to be pooled by the JVM so the same object (see flyweight pattern) is used for them. Hence, they compare identical by object references.
For the Doubles a and b on the other hand, no such pooling guarantee exists and, in your case, you got two different objects that did not compare identical.
Using == to compare objects if you don't mean to check identity is to be considered suspect and should be avoided. The equals methods of both types are overridden to compare the boxed values (as opposed to object identity) which is why they return true in both cases (and should be used).
Initialize the Integers the following way then you will get the difference as #5gon12eder said
The Integer s i and j are constructed (via auto boxing) from integer literals from the range –128 to 127 which are guaranteed to be pooled by the JVM so the same object (see flyweight pattern ) is used for them. Hence, they compare equal by object references.
try this code to initialize your integers
Integer i = new Integer(10);
Integer j = new Integer(10);

Integer comparison with == [duplicate]

This question already has answers here:
Integer wrapper objects share the same instances only within the value 127? [duplicate]
(5 answers)
Closed 8 years ago.
I was writing some test code and found one strange thing, and still confused how this is happening?
Integer i1 = 220;
Integer i2 = 220;
System.out.println(i1 == i2);
prints false as expected. But
Integer i1 = 20;
Integer i2 = 20;
System.out.println(i1 == i2);
prints true, but both are different references referring to different objects (I assume that).
How come second snippet prints true?
The == operator only works for Integer values between -128 and 127. That is why it doesn't work for 220 but does for 20. In general it is best to always use .equals() when comparing Integers and you should never rely on the == operator.
More information can be found here: https://www.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching

How can "a <= b && b <= a && a != b" be true? [duplicate]

This question already has answers here:
How to make loop infinite with "x <= y && x >= y && x != y"?
(4 answers)
How can i define variables to make an infinity while loop with these conditions? [closed]
(4 answers)
Closed 9 years ago.
Here is the code i have to figure it out how is it possible. I have a clue but i do not know how to do it. I think it is about negative and positive numbers and maybe the variable modifiers as well. I am a beginner i looked the solution everywhere but i could not find anything usable.
the question is that: You need to declare and initialize the two variables. The if condition must be true.
the code:
if( a <= b && b <= a && a!=b){
System.out.println("anything...");
}
I appreciate you taking the time.
This is not possible with primitive types. You can achieve it with boxed Integers:
Integer a = new Integer(1);
Integer b = new Integer(1);
The <= and >= comparisons will use the unboxed value 1, while the != will compare the references and will succeed since they are different objects.
This works too:
Integer a = 128, b = 128;
This doesn't:
Integer a = 127, b = 127;
Auto-boxing an int is syntactic sugar for a call to Integer.valueOf(int). This function uses a cache for values less than 128. Thus, the assignment of 128 doesn't have a cache hit; it creates a new Integer instance with each auto-boxing operation, and a != b (reference comparison) is true.
The assignment of 127 has a cache hit, and the resulting Integer objects are really the same instance from the cache. So, the reference comparison a != b is false.
Another rare case for class-variables may be that another thread could change the values of a and b while the comparison is executing.

Categories

Resources