• Post Reply Bookmark Topic Watch Topic
  • New Topic

how can we prove that String class is non mutable class  RSS feed

 
kiran kumar
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new to java.

Can anybody help out me with code. How can we prove String is immutable class?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd prove it by reading the API. If you go to the String page, it says:

Strings are constant; their values cannot be changed after they are created.
 
Manish Sawjiani
Greenhorn
Posts: 1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe we can try this to prove
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't believe the API docs that Fred references, the only way to prove that String is immutable is to check the source code. But you could get a pretty good indication by trying the various methods that look like they could change it and seeing what happens. E.g.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!