• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals and ==

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1: String s= "may";
2: String s1=new String("may");
3: if(s==s1)
System.out.println("==equal");
4: if(s.equals(s1))
System.out.println("equal");

why line 3 is not equal
am i not right,firstmay in places in the stringpool and then s is refereed to it
and then inline 2 it searches for the string "may"if its found it also refers tothe same object.
i know here one obj is places on the heap and otherin the string pool

wat i inferred was in line 1 reference is to the string obj in the pool and in line 2 reference is to the string object in the heap ,
Am i right,if not plz do explain.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and then inline 2 it searches for the string "may"if its found it also refers tothe same object.


No. The "new" keyword will create a new reference without looking in the pool.
If you had, String s1= "may", it would look for the same string in the pool.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 2, a new object is created (even though the string literal "may" is present in the pool) and is returned to the reference variable s1. Thus, the variables s and s1 refer to different objects and the == operation returns a value false.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok ,Thanks a lot
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi if a same object exists in pool will the new operator create a object in pool

for eg
String str1= "hello";
String str2 = new String("hello"); // will this statement create 2 objects ( one in pool and one not in pool) or only 1 object (not in pool) because a same object already exists in pool
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi mohit,

the latter: when the second "hello" is found during class loading, it is recognized that a string "hello" already exists in the pool, and so no new object is created for the pool.
reply
    Bookmark Topic Watch Topic
  • New Topic