• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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: 24206
46
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.
 
Get off me! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic