Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 645
Adam Avatar asked Sep 27 '13 03:09

Adam


People also ask

What does >= mean in math?

The greater than or equal to symbol is used to represent inequality in math. It tells us that the given variable is either greater than or equal to a particular value. For example, if x ≥ 3 is given, it means that x is either greater than or equal to 3.

How do you denote a greater than B?

The notation a ≤ b or a ⩽ b means that a is less than or equal to b (or, equivalently, at most b, or not greater than b). The notation a ≥ b or a ⩾ b means that a is greater than or equal to b (or, equivalently, at least b, or not less than b).

What does ≤ mean?

The symbol ≤ means less than or equal to.

How do you prove A or B?

Proving one of these two possibilities is a complete proof. There is no need to do both. Another way to prove an "A or B" statement is to assume both statement A and statement B are false and obtain a contradiction.


2 Answers

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.

like image 133
Henry Avatar answered Oct 16 '22 11:10

Henry


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.

like image 20
erickson Avatar answered Oct 16 '22 11:10

erickson