aspose file tools*
The moose likes Beginning Java and the fly likes Difference of double quotes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference of double quotes" Watch "Difference of double quotes" New topic
Author

Difference of double quotes

Jamil Akhtar
Greenhorn

Joined: Sep 06, 2011
Posts: 2
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

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sandy chops
Greenhorn

Joined: Nov 11, 2010
Posts: 8
Try this it will help clear your doubt.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36498
    
  16
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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36498
    
  16
. . . but neither of those can be called the null string.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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

Joined: Aug 16, 2005
Posts: 13875
    
  10

I wouldn't call "" a null string, that would be very confusing. I would just call it an empty string.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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

Joined: Oct 13, 2005
Posts: 36498
    
  16
I would agree with Jesper and call it an empty String.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Mar 10, 2008
Posts: 326

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference of double quotes
 
Similar Threads
doubt: equals&==
Some tips to create these methods
how to take a series of string from command line?
<br /> or <br>
Variable Syntax