jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java" Watch "java" New topic
Author

java

pawan sharma
Greenhorn

Joined: Feb 12, 2001
Posts: 7
when we create a String variable as
String str="pawan";
Stirng str=new Sring("pawan");
what is difference between both .
Pawan Sinha
Greenhorn

Joined: Jan 16, 2001
Posts: 10
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

Joined: Jan 07, 1999
Posts: 6920
This is not a servlet question, so I've moved it to "Java in General, Beginner"


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
This thread has an excellant explaination of the difference between the literal and the object.
http://www.javaranch.com/ubb/Forum33/HTML/000601.html


"JavaRanch, where the deer and the Certified play" - David O'Meara
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
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

sona<br />SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java
 
Similar Threads
passed exam with 85%,help needed
variable declaration and object creation in side loop
Sanitizing String in Java.
Server Types
question regarding strings