I wanted to make (a == 1 && a == 2 && a == 3) true in Java

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.

Possible solutions

There seem to be various solutions. As an example, it looks like this.

Reference: ((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. ..

Overloading the == operator

Java does not implement advanced features such as operator overloading. It's in C ++ and Perl.

Make a a function

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.

Make a an object

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"?

Conclusion

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. .. ..

Postscript

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

I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
I tried to make a login function in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to make a client of RESAS-API in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I tried to make a talk application in Java using AI "A3RT"
I tried to create a Clova skill in Java
I created a PDF in Java.
How to make a Java container
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
How to make a Java array
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
[Beginner] I made a program to sell cakes in Java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried to convert a string to a LocalDate type in Java
I wanted to think I understood Elm's custom types in Java
How to make a Java calendar Summary
Easy to make Slack Bot in Java
How to make a Discord bot (Java)
[Java] I tried to make a maze by the digging method ♪
I made a primality test program in Java
I tried to make a parent class of a value object in Ruby
Make Blackjack in Java
Two ways to start a thread in Java + @
I tried to make Basic authentication with Java
I want to send an email in Java.
How to display a web page in Java
java I tried to break a simple block
Code to escape a JSON string in Java
Try to create a bulletin board in Java
[JAVA] Project Euler, I got stuck in Q8, so make a note
I tried to implement deep learning in Java
I wrote a primality test program in Java
I made a rock-paper-scissors game in Java (CLI)
I wanted to implement a slide show in a fashionable way with slick.
rsync4j --I want to touch rsync in Java.
I tried to output multiplication table in Java
I want to ForEach an array with a Lambda expression in Java
I tried to create Alexa skill in Java
I wrote a prime factorization program in Java
How to make a follow function in Rails
I tried to break a block with java (1)
[Small story] I tried to make the java ArrayList a little more convenient
How to create a Java environment in just 3 seconds
Make "I'm not a robot" in Java EE (Jakarta EE)
I want to do something like "cls" in Java
I tried to implement Firebase push notification in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
# 2 [Note] I tried to calculate multiplication tables in Java.
[Personal memo] Make a simple deep copy in Java
How to create a data URI (base64) in Java
I want to define a function in Rails Console
What I learned when building a server in Java
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
Try to solve a restricted FizzBuzz problem in Java
I want to click a GoogleMap pin in RSpec
I tried to implement the Euclidean algorithm in Java