• 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

Declare and initialize empty String

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read someone's code. There are two String declarations and initializations for empty String. Could anyone explain the difference? Which is better way to use?

1. String = "";
2. String = null;

Thanks a lot,
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, you need a variable name in there for it to be legal.


Question: does line 2 initialize 'b' to an empty string?

Line 1 create a String object and assigns it the reference 'a'.
Line 2 only creates a reference( 'b' ) to a String object. It will need to be assigned a String object before it can be used. If you try to use before-hand, you'll get a null pointer exception.

Which one is correct? Preference.
 
Ranch Hand
Posts: 218
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably going to sound totally dumb here and embarras myself!!

BUT if I don't say something I won't learn...........

I thought the following:

null means it is empty i.e. nothing there

And

" " means it is just initialised to something

(Please, please be kind!)
 
Cathie Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, my typo, without variables...
Thanks for the explanation.

So my understand is:
Line 1: "" means a value with empty.
Line 2: null means no value.

The same as in Oracle.
Correct me if I'm wrong.

Thanks,
 
Mark Patrick
Ranch Hand
Posts: 51
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null means "nothing".

"" is a String object with in length of 0.
null means no object

Similar to Oracle. In Oracle, you can't perform math on NULL because NULL is non-existant. In Java, instead of meaning no-value, null means no object is assigned to the variable.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aloe1138 --

Welcome to JavaRanch!

On your way in, you might not have noticed the sign on the wall explaining our policy on display names. Let me spell it out fer ya: yer handle has to be a real-lookin' first and last name. Please head over here and fix yours up, please.

Thanking you kindly,

-- your local law enforcement officer.
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic