Can stack overflow always set [(a == 1 && a == 2 && a == 3) to true](https://stackoverflow.com/questions/48270127/can-a-1-a-2- The article a-3-ever-evaluate-to-true) is popular. The original story is a javascript problem, but I saw an article doing the same problem in Peri, so I will post an article to get on the trend.
Currently, I mainly use Java in my business, so I thought about how to do it with Java. I was wondering if it was somewhere in Qiita, so I caught a lot of things, but [(a == 1 && a == 2 && a == 3) is always true. Qiita article summary that solved the Stack Overflow problem]( As far as I can see https://qiita.com/aimof/items/bb786c112f7dcc3be6c9), it didn't seem to be. Then if you find it, why not take the first ride? This is the result of crushing the ball by trial and error.
There seem to be various solutions. As an example, it looks like this.
==
operatorReference: ((a == 1 && a == 2 && a == 3) can always be true?)
For the time being, I tried this much. Java has strict coding restrictions, does not allow omission of writing style that can be done in other languages, and often does not implement functions in other languages, so basically it does not work. ..
==
operatorJava does not implement advanced features such as operator overloading. It's in C ++ and Perl.
Main.java
public class Main {
static int i = 1;
public static void main(String[] args) {
System.out.println(a() == 1 && a() == 2 && a() == 3);
//true is displayed
}
static int a() {
return i++;
}
}
I could have done it if Java had a function like "Functions without arguments can omit ()". I think this is probably the most regrettable.
You get angry if you can't compare objects to primitive types in the first place.
Object a = 1;
System.out.println(a == 1 && a == 2 && a == 3); //Compile error
Calling a field or method in an object doesn't work because you have to write something like ʻa.number or ʻa.getint ()
in the end.
There are some convenient classes that can convert the type of an object as it is. It is a wrapper class.
Integer a = 1;
System.out.println(a == 1); //Internally a.intValue() ==Become 1
Then, create a class that inherits this and play with ʻintValue ()`! When I thought, I couldn't inherit it with the final class. That's right.
Isn't it possible to use a rule like "If you use a function interface, you can omit () when there is no argument"?
In Java, it is impossible to always set (a == 1 && a == 2 && a == 3) to true. If you know the solution, please let me know. .. ..
I found it if I looked it up properly. I'm sorry to all my ancestors. I want to output true in Java with a == 1 && a == 2 && a == 3 I want to output true in Java with a == 1 && a == 2 && a == 3 (black magic edition) Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier) I want to output true in Java with a == 1 && a == 2 && a == 3 (PowerMockito edition) I want to output true in Java with a == 1 && a == 2 && a == 3 (black edition)
If you use PowerMockito, you could forcibly inherit Integer.
Recommended Posts