• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

String Object vs String literal  RSS feed

 
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String Object vs String literal :
====================
when we compare a String literal with an String object created using new() operator using == operator, it will return false. Both are pointing same object only then why it return false. Anyone clarify my doubt...

[For Reference]
String a = new String("java");
String b = "java";
System.out.println(a == b); // False

[/color]
 
Greenhorn
Posts: 2
1
Java jQuery Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(This is concept of String constant pool) Actually when you use any string literal in your java code that time jvm allocate one memory slot for that literal. With String literal if you use same literal anywhere else jvm first finds that literal in String constant pool if it present there then jvm does not allocate new memory space for that literal.It directly picks up that same object (String literal) from string constant pool but if you create String object using new keyword then jvm create one object in heap memory and it finds literal("java") whatever argument you passed inside new String("java") in string constant pool if it present in string constant pool then jvm will pick up that object from string constant pool and jvm will pass that literal (String object) inside new String("Object passed from String constant pool") and if literal is not present in string constant pool then jvm first allocate memory for that literal then It will pick up the recently create literal object from String constant pool so If you try to compare both objects it will be different because of both objects have different reference codes .
20160529_022140-1-.jpg
[Thumbnail for 20160529_022140-1-.jpg]
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thiyagarajan Gnanachandran wrote:Both are pointing same object only then why it return false. Anyone clarify my doubt...



Well, obviously, the two references (LHS and RHS of the expression) are not pointing to the same object -- otherwise, as you mentioned, the comparison would be true.

The two references are pointing to two different string objects, that happens to have the same value.


EDIT: No response for two hours, and then two responses within two minutes. Regardless, I like the diagram -- have a cow...

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!