• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference to be explained  RSS feed

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you tell me the difference between String s="abc" and String s= new String("abc")


Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Padma priya Gururajan wrote:Can you tell me the difference between String s="abc" and String s= new String("abc")

Effectively? None.

Mechanically: I suggest you have a look at the CachedObjects page.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first one just retrieves a reference to a String in the constant pool. The second one creates a new String object that is a copy of one in the constant pool. There's no reason to ever do new String("some string literal").

But, while the second one is bad practice, as Winston says, there's no real practical difference. You'd have to do that a ridiculous number of times before you'd ever begin to notice any different behavior in your code. But still, don't do the second one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!