0

What is the difference between forming a new object of class String by following 2 methods :

  1. String str1 = "Welcome";
  2. String str1 = new String("Welcome");
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • First one uses a literal object from the constant pool. Which means it will fetch same object every time you refer to `"Welcome"` literal. Second one is creating a new object every time. – 11thdimension Aug 09 '16 at 20:21
  • Can you please elaborate more or post some link regarding the same. What is a literal object ? – Avinash Kumar Rai Aug 10 '16 at 17:04
  • Check the page which is provided by Andy, in particular this answer http://stackoverflow.com/a/23546082/5343269 . A literal in a programming language is a sequence of characters which represent some value but are not stored in any variable. For example, `1234` is an integer literal, `Welcome` is String literal, `True/False` are boolean literals, `1E-20` is a floating point literal. – 11thdimension Aug 11 '16 at 03:45

0 Answers0