• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question on String

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
on this site I read following statement:
"Strings are initialized to null, not empty string."

can anyone pls expain why this is so..
If haphazardly we use a string initialized to null and used directly
it may give NullPointerException.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are like any object, when you declare one it will hold the value of null.
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shaily,

Think of it this way:

Declaring a String object is similar to declaring an Integer object. If neither is initialized, they have a default value of null.

That being said,

Both objects are null at this point. Now, saying:

is similar to saying:

Both objects have been initialized. An empty string is a legitimate value. Kind of a confusing concept such as -0.0 == 0.0. .

Aloha,
Doug
 
Shaily Sharma
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
basically my question is what is a standard method
to initialize a String?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shaily Sharma:
basically my question is what is a standard method
to initialize a String?



Hi Shaily,

try this whenever you want to use a standard way to initialize a String object

String str = new String();

One of the constructors of String class will accept "" as its initial value. Hope i am right.

Regards,
Ratnankur
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ratnankur Kushary:
try this whenever you want to use a standard way to initialize a String object

String str = new String();


No, just do this:

String str = "";

It is not necessary to create a new String object explicitly like this. By using = ""; you take advantage of string pooling; i.e. if you have multiple String variables that you need to initialize with the empty string, then they'll all point to the same "" String object in the pool, so there's no need to create separate String objects.
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic