Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with == ( double equals ) method.

 
pradeep kumar rama
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand how the first output of the following piece of code returns "true"

As far as I know, the == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. But here s1 and s5 point to different memory locations. Still how can the result be true ?

The Code I was refering to :



The result I got in the command prompt:

C:\Java Progs>javac EqualsTest.java

C:\Java Progs>java EqualsTest
== comparison : true
== comparison : true
Using equals method : true
false
Using equals method : true

C:\Java Progs>
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See our SCJP FAQ: Why do separate autoboxing conversions sometimes return the same reference?.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the What is the String pool? FAQ would be more useful here.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals behave differently than the rest of the world.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic