Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java

 
pawan sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we create a String variable as
String str="pawan";
Stirng str=new Sring("pawan");
what is difference between both .
 
Pawan Sinha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
string str= new String("Pawan") creates a new memory space for string str, so if u will equate the two string variables declared as in yr query, the result will be false as these two string variable are having diffrent reference.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a servlet question, so I've moved it to "Java in General, Beginner"
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread has an excellant explaination of the difference between the literal and the object.
http://www.javaranch.com/ubb/Forum33/HTML/000601.html
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String str = "pawn"
stores the constant pawn in the variable str
String str = new String(pawn)
creates a new string object and stores the refernce to that object in str and pawn is passed to the constructor of that object
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic