• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class Integer's equals() function?  RSS feed

 
Jack Lee
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the api document of Java 5.0, the equals() func is described as:

"public boolean equals(Object obj)Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object. "

However, result of the following code is

Integer i = new Integer(42);
Integer j = new Integer(42);
System.out.println(i.equals(j));
System.out.println(i.equals(42));

true
true

Anybody help?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the equals method is expecting some sort of object (not a primitive) as an argument, the int literal 42 is automatically converted to an instance of Integer. This is a process called "boxing" or "autoboxing."

See this Autoboxing article for more details.
[ June 12, 2006: Message edited by: marc weber ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!