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

• Make all boolean checks true by overloading the `==` operator
• Make a a function and change the return value of a each time it is called
• Make a an object (abbreviated below)

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

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

If you use PowerMockito, you could forcibly inherit Integer.