This question already has answers here:
Why does (i<=j && j<=i && i!=j) evaluate to TRUE?
(10 answers)
Closed 2 years ago.
I saw this exercise on a Java test, which finally led me to ask for ideas because there are some solutions I tried, but obviously won't solve the problem, due to Math conditions.
So the exercise:
Define S and T variables as it results an endless while loop below!
while(s <= t && s >= t && s != t)
{
}
think about OOP, in fact 0!=0 is false, but new Integer(0) != new Integer(0) is true because it's not the same reference.
so Integer S= new Integer(0); Integer T = new Integer(0);
should resolve your problem
here is answer :
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.
you can check farther details in this link How can "a <= b && b <= a && a != b" be true?
Related
This question already has answers here:
Why does this code have a missing return statement error?
(6 answers)
Closed 6 years ago.
Suppose I write the function
public static Object create(int num) {
if (num < 0 || num > 0) return null;
if (num == 0) return new Object();
}
I have specified a return value (either null or a new object) for all possible integers (the num < 0 case, the num > 0 case, and the num == 0 case).
Why, then, is my IDE telling me the function lacks a return value?
IDE does not have to know that you have covered all integers. So as a compiler.
What it does know is that you have used constructions in your method that, by its definition, are not obligatory to be executed.
Integer i = new Integer(0);
Integer j = new Integer(0);
while(i <= j && j <= i && i !=j ){
System.out.println(i);
}
Why does this while loop execute?
I understand that i != j. But separately, both i <= j and j <= i returns true. Why? And doesn't that mean that i == j? In that case the while loop shouldn't execute even once. But it goes for an infinite loop. Why is this so?
While == can be performed between two references (and is, when both sides are Integer), <= can't - so the compiler unboxes. So, your code is equivalent to:
while (i.intValue() <= j.intValue() &&
j.intValue() <= i.intValue() &&
i != j)
Now when i and j refer to different Integer objects with the same value, all of those conditions will be met - hence the loop keeps executing.
Try:
while(!i.equals(j))
You are comparing two integer objects with == rather than two primitive int values. That is to say, "Integer" is an object while int is a primitive value. Doing != on an object will return true for Integer(0) and Integer(0) because they are indeed not equal -> each has a separate location in memory.
The "new" operator allocates dynamic memory on the heap.
For == to work, you could change the type to int, as in:
int i = 0;
int j = 0;
This will compare the values directly.
If you use "Integer," another alternative is to use the .compareTo method, which will compare the value of the objects
== for objects, like a capital-I Integer, compares references. <= and >= will unbox the Integer objects and compare the values numerically, which is completely different.
Think of == and != in this case as completely unrelated to the values of the Integers.
"<=" is comparing the values while "!=" the references hence here all the cases are true hence it is going in infinite loop.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
This is classical, but writing to Google did not give me hits.
My code:
Integer i = ..; // Sth is put from database.
if (i != 1) {
// do sth
} else {
// do not.
}
Case:
I know that this comparison is not correct java and I should compare:
if (i.intValue != 1) {}
or
if(!i.equals(1)) {}
but my code had the first one and I seem to get the true from somewhere, where the Integer is not 1 and when it is 1 there comes false.
Question:
What is happening there around?
but my code had the first one and I seem to get the true from somewhere, where the Integer is not 1 and when it is 1 there comes false.
If I understand your issue correctly, the following might explain the behavior:
Integer i = 1;
Integer j = new Integer(1);
Integer k = Integer.valueOf(1);
System.out.println(i == j); // false
System.out.println(i == k); // true
In other words, you can get both true or false when comparing with 1 depending on how the Integer was constructed. Integer.valueOf will reuse objects while new Integer will not.
If you indeed did the comparison with an integer literal (or with an int) then any Integer should be automatically unboxed by the compiler, and you should never get any surprises.
The compiler changes :
if (i != 1)
to
if(i.intValue()!=1)
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
See this link
if (i != 1) , if (i.intValue != 1) and if(!i.equals(1)) are all equal and will return the same value. The compiler will automatically unbox i, turning it into a primitive int.
This question already has answers here:
Integer wrapper class and == operator - where is behavior specified? [duplicate]
(2 answers)
Closed 8 years ago.
I have been playing with == in wrapper classes. There's some weird thing that I encountered.
Integer i1 = 3;
Integer i2 = 3;
if(i1 != i2)
{
System.out.println("i1 and i2 are not equal"); // if condition returns false in this case
}
which is working perfectly fine as far as my knowledge is concerned.
whereas, If I instantiate wrapper with
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2)
{
System.out.println("i1 and i2 are not equal"); // if condition returns true in here
}
Then I am getting confused with the dual behavior of JVM with respect to the different values assigned to wrapper.
Why is this happening so? Please share your thought on this.
Thanks
You need to use !i1.equals(i2)
I have written a piece of Java code which is running in an infinite loop.
Below is the code:
public class TestProgram {
public static void main(String[] args){
Integer i = new Integer(0);
Integer j = new Integer(0);
while(i<=j && j<=i && i!=j){
System.out.println(i);
}
}
}
In the code above, while seeing the condition in the while loop, at first it looks like that program will not go inside the while loop. But actually it is an infinite loop and keeps printing the value.
What is happening here?
i <= j is evaluated to true, because auto unboxing happens for int
comparisons and then both i and j hold the default value, 0.
j <= i is evaluated to true because of the above reason.
i != j is evaluated to true, because both i and j are
different objects. And while comparing objects, there isn't any need of
auto unboxing.
All the conditions are true, and you are not changing i and j in loop, so it is running infinitely.
Because you are comparing
0 < = 0 (true) // unboxing
0 > = 0 (true) // unboxing
reference != secondReference (true) as you are creating objects, not a primitive comparison. So it evaluates to while(true) { // Never ending loop }.
The integer objects are different. It is different from the basic int type.
See this answer: How to properly compare two Integers in Java?
The i != j part is true, which you were expecting to be false.
There are two different cases which we have to understand first,
case 1:
Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println((i<=j && j<=i && i!=j));
System.out.println(i!=j);
case 2:
Integer i = 10;
Integer j = 10;
System.out.println((i<=j && j<=i && i==j));
System.out.println(i==j);
both are different, as
in case 1: i!=j will be true because both referencing to two different object in heap and can't be same. But
in case 2: i==j will be true because both 10 are integer literals and Java maintains pool for Integer literals which have value (-128 <= X <= 127). So, in this case 10<=127 results true, So both will have reference to same object.
The loop is not ending because your condition is true( i != j is true because there are 2 different objects, use Integer.valueOf instead) and inside the loop the values are not changing so your condition remains true forever.
Perhaps the reason is that both 'i' and 'j' are objects, and object comparison is not the same as object reference comparison. Please consider using !i.equals(j) instead of i!=j
The integer objects are different. It is different from the basic int type.
so you can just do like that. what you do it's just compare the object and of course the result is true.
Integer a = new Integer(0);
Integer b = new Integer(0);
The <= and >= comparisons will use the unboxed value 0, while the != will compare the references and will succeed since they are different objects.
Even this will also works i,e
Integer a = 1000; Integer b = 1000;
but this doesnot :
Integer a = 100; Integer b = 100;
The reason is because Integer internally uses caching for Integer objects between -128 and 127 and return instances from that cache for the range it covers. I am not sure but I guess you can also change its maximum value in package "java.lang.Integer.IntegerCache.high".
For better understanding check url : https://www.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching
The program keeps on displaying the same value of i because you aren't incrementing or decrementing either the value of i or j. The condition in the for always keeps evaluating to true, so it is an infinite loop.
you have to know its a bit different in && this and this & when you use && then when first condition is true then it check second condition if its false then it not checked third condition because in & operator if one condition is false all of the statement is false if use || then if it see true then it return true in your code because i and j is equal first and second condition are true then in third condition it will be false because they are equal and while condition is false .