• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Strings

 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers
Preparing for scjp 5.while reading scjp5 by kathy siera from page 420.
"Creating New Strings".There she explained two approaches for creating Strings.

1st approach:String s=new String("String");
2nd approach:String s="String".

Told there is subtle difference between those two.Once is created with new and the other is not.Also for the 1st approach she said like two String objects are created because of new?how come?.while latter only 1 object is created with 2nd approach

Regards
Kirba
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One String would be in the String pool and the other string would be the one created with the new operator when you type String s = new String("");.

With String s = "hi"; you create one object. This is the one in the String pool
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic