1

Is there any reason why you may want to use " " and not ' ' to output blanks in Java (any version)?

In "Data Structures and Problem Solving Using Java (4th Edition)" by Mark Allen Weiss, it says that it is a common error to not use " " instead of ' ': i.e. We should use " ".

As far as I know, there are no differences between the two.

Trying:

System.out.println("a b c" + ' ' + "d e f");
System.out.println("a b c" + " " + "d e f");

There seems to be no difference in the output.

Soo Kyung Ahn
  • 67
  • 1
  • 7
  • It depends if you need a String or a char. – khelwood Jun 20 '19 at 21:46
  • My mistake, was working in Python prior to this and forgot about 'char' and "String" difference associated with the two quotation mark types. However, I still believe it shouldn't make a difference if you're just joining two Strings. – Soo Kyung Ahn Jun 20 '19 at 21:49

4 Answers4

4

With your example if may have no effect. However, there are cases where it matters.

char is an integral type, and

System.out.println(1 + ' ' + 2); //output: 35 because 1 + 32 + 2 

produces a different result from

System.out.println(1 + " " + 2); //output: 1 2

So you should make sure you know the difference between ' ' and " ".

I imagine that the context of the advice in the book gives some indication of what situation it is talking about.

khelwood
  • 55,782
  • 14
  • 81
  • 108
1

It is because + operator has two meanings in Java

  1. concatenation if at least one operand is String like Str + ... or ... + Str
  2. addition if both operands are numeric types:
    • all primitives, except boolean (no in Java true and false are not equivalents of 1 and 0),
      BUT including char which numeric value is position of character in Unicode Table like 'A' is 65 and space ' ' is 32.
    • wrapper classes for numeric primitive types like Integer, Double

So if you have num + Str operator + will represent concatenation and you will get String.

But in case of num + char operator + will represent addition and will return numeric value like System.out.println(1+' ') will be treated as 1+32 so 33 will be printed.

So if you use " " with + you are guaranteed that it will be concatenation, while meaning of + with ' ' will depend on second argument.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

We use single quotes for literal chars and double quotes for literal Strings. So you're going to add them together with this:

System.out.println("a b c" + ' ' + "d e f");

but there is no error in it. You can use " " or ' ' to output a space.

  • "*You can use " " or ' ' to output a space*" not always, see this answer https://stackoverflow.com/a/56694399 to see cases which shows difference in output when we use `" "` vs `' '`. – Pshemo Jun 20 '19 at 22:17
0

The reason your Java book tells you that double quotes is better than single quotes is that ' ' is a char, and " " is a String. This is built-in java behaviour. char is a java primitive, like an int or a float. String is a supplied object type, not a primitive. When you put " " in your code, java interprets this as an instance of java.lang.String.

You're right that it has no practical effect in your example though. That's because when you concatenate strings and characters, java automatically converts the whole thing to a String (the + operator does this). You can also concatenate other primitives in this way.

However, these are actually quite different in Java, and you can prove this with a simple test:

@Test
public void testCharVsString(){
    assertTrue(" ".equals(' '));
}

The above test will fail, as these two objects are not the same.

As a general rule it's OK to stick with Strings when representing characters. char is useful for more advanced coding tasks.

  • 1
    I have to respectfully disagree with your last paragraph. It may be good to stick with `String` when *printing* specific characters, but I wouldn't encourage people to use `String` to represent a single character in algorithms or business logic. – Silvio Mayolo Jun 20 '19 at 22:27
  • Agreed :) I probably should have said "where there might be more than one character". I wanted to keep the language accessible for a new coder. But you're absolutely right of course - and that's why I hinted at "more advanced coding tasks" – dannywalk Jun 20 '19 at 23:03