I investigated Java primitive types

Introduction

When I use Java at work, I often use equals for equivalence evaluation. When I was a newcomer, I heard that "Equivalent use equals!" However, this is a story of object types such as String type.

Here, I was worried about int / Integer and long / Long that have primitive types. The question came up, "Long type is an object type, so you can't compare using == normally?"

It's a simple question, so I tried to verify it.

Identifying patterns to be compared

The target is a Long type with a primitive type, and the comparison method uses only ==. It feels like specifying a Long type instance on the left side and a Long type on the right side in various ways. Also, a comparison between a long constant and a Long type. It may be unsatisfactory because it is just the pattern that I came up with quickly.

Let's verify! !!

The verification was confirmed by running the following method on Junit4. I think it doesn't depend on the version, but I used Java 8 just in case. Some have been verified under the premise that "Long type is an object type".

@Test
public void testEqualsMethod() {
	Long value1 = 100L;

	//Long type made by either method is evaluated as equivalent
	assertTrue(value1==100L);                   // (1)
	assertTrue(value1==Long.parseLong("100"));  // (2)
	assertTrue(value1==Long.valueOf("100"));    // (3)
	assertTrue(value1==Long.valueOf(100));      // (4)
	assertTrue(100L==Long.parseLong("100"));    // (5)
	assertTrue(100L==Long.valueOf(100));        // (6)

	Long value2 = value1;        //If the Long type is an object type
	++value2;           //If you change the value of value2, the value of value1 should also change....
	assertTrue(100L==value1);    //But value1 does not change
	assertTrue(101L==value2);    //value2 is firmly increased by 1
	assertTrue(value1!=value2);  //Of course the objects are different...Seems to be
}

Consideration

From the above test method, it was found that the equivalence evaluation can be performed using == when the Long type is generated by any method. However, the feeling of "Long type-> object type-> equals is equals" does not disappear ... It was also found that even if the Long type is assigned to another object, there is no behavior that the object type shares. In other words, the Long type is created as a different object when assigned to another variable.

This result can be said for similar Integer types. However, in the Double type and Float type, the evaluation was NG probably because valueOf () returned the object type in the test methods (3) and (4). (On the contrary, I wonder why Long type and Integer type became True by valueOf () ...)

The point to note when setting "xxx == Xxx.parseXxx ()" or "xxx == Xxx.valueOf ()" is whether it is an integer type or a decimal type. So, the table below shows whether or not they are equivalent.

Mold Xxx.parseXxx() Xxx.valueOf()
Long Possible Possible
Integer Possible Possible
Double Possible 不Possible
Float Possible 不Possible

In fact, (5) of the test method is a comparison between primitive types, and (1), (2), and (6) are equivalent comparisons of [primitive type] == [object type]. Therefore, it is possible that "Isn't it True?"

If this is left as it is, it will be dragged and dragged, so I would like to examine this question at another time. It's open once. .. ..

bonus

I also wanted to confirm the nature of autoboxing, so I experimented with the following test method. It's like preparing a method with an Object type argument and throwing various values into the method to see what kind of class it will be. (Verification environment: Java8, JUnit4)

@Test
public void testAutoBoxing() {
	//A class that just returns the class name of the argument
	class AutoBoxingTester {
		public String get(Object obj) {
			return obj.getClass().getName();
		}
	};
	AutoBoxingTester tester = new AutoBoxingTester();

	//I tried throwing in various values
	assertEquals(Integer.class.getName(), tester.get(100));
	assertEquals(Long.class.getName(), tester.get(100L));
	assertEquals(String.class.getName(), tester.get("str"));
	assertEquals(Float.class.getName(), tester.get(3.14f));
	assertEquals(Double.class.getName(), tester.get(6.0e23));
}

Recommended Posts

I investigated Java primitive types
Java primitive types, reference types, Immutable, Mutable
About Java primitive types and reference types
About Java data types (especially primitive types) and literals
I first touched Java ②
I first touched Java ③
I first touched Java ④
I investigated test automation
Java Primitive Specialization Summary
I first touched Java
[Java] Main data types
[Java] Variables and types
Java basic data types
I summarized the types and basics of Java exceptions
Equivalence comparison of Java wrapper classes and primitive types
What I researched about Java 8
I investigated the enclosing instance.
I started Java Gold (Chapter 1-1)
What I researched about Java 6
What I researched about Java 9
About Java class loader types
I took Java SE8 Gold.
I tried Drools (Java, InputStream)
What I researched about Java 7
I tried using Java REPL
[Java] I implemented the combination.
Java concurrency I don't understand
I investigated Randoop, a JUnit test class generator for Java
I wanted to think I understood Elm's custom types in Java
Generate Stream from an array of primitive types in Java
Java Learning 1 (learning various data types)
I studied the constructor (java)
I tried metaprogramming in Java
What I researched about Java 5
I sent an email in Java
Java variable declaration, initialization, and types
[Java] Basic types and instruction notes
Java Primer Series (Variables and Types)
I created a PDF in Java.
I made a shopify app @java
Basic data types and reference types (Java)
I checked Java Flight Recorder (JFR)
I fell into Java Silver (crying)
I tried to interact with Java
I tried UDP communication with Java
Java array variables are reference types
I wrote Goldbach's theorem in java
I tried the Java framework "Quarkus"
I tried using Java8 Stream API
What I learned with Java Gold
I made an annotation in Java.
I tried using JWT in Java
I tried to summarize Java learning (1)
Java basic data types and reference types
List of types added in Java 9
What I learned with Java Silver
What I researched about Java learning
I tried to summarize Java 8 now
I tried using Java memo LocalDate
I compared Java and Ruby's FizzBuzz.
[Java] Exception types and basic processing