• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference of double quotes

 
Jamil Akhtar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to Java and I have got a question for those are skilled Java programmers. I am using "Head First Java" book . In this book there is an example where there are two types of double quotes ("") in the print statements. One type of double quotes are with a space between them i. e ; " " and other without space like ; "". Somebody told me that they are the same thing, but I noticed the difference. I couldn't understand the difference between them. Please anyone can help.

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One is the empty string, the other is a string with a single space character. Whoever told you that they are the same is very wrong.
 
sandy chops
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this it will help clear your doubt.

 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they have used a word-processor for writing HFJ, because you find code like this: System.out.println(”Campbell‟);
You should however always use straight quotes like this "" except inside String or char literals, where you can use the Unicode escapes (\u201d and \u201f).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to further beat the dead horse...

one is a string of length 0 - it has nothing in it.

one is a string of length 1 - it has a character in it. That character happens to be a space, but that is far from insignificant.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but neither of those can be called the null string.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . but neither of those can be called the null string.

hmmm....i think i would disagree with you here. IMHO, "" is a null string - a String with nothing in it. it is not, however, the same as a reference variable pointing to null.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't call "" a null string, that would be very confusing. I would just call it an empty string.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:I wouldn't call "" a null string, that would be very confusing. I would just call it an empty string.


I agree on that. null is null and not even a String. "null" is the null-string. ;)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what is IN the string? nothing. or null.

I guess i see is as a null String vs. a null reference variable.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would agree with Jesper and call it an empty String.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would agree with Jesper and call it an empty String.

Given that it can be tested for by String.isEmpty(), I think the String class agrees with you as well.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Campbell Ritchie wrote:I would agree with Jesper and call it an empty String.

Given that it can be tested for by String.isEmpty(), I think the String class agrees with you as well.


Yep.

And


gives you a null pointer exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic