4

Why is it that Java requires the char variable to enclose anything inside of it with single quotes rather than double? An example of this:

char s = 's'; //This will not break

vs:

char s = "s"; //This will break

I understand that double quotes are primarily for strings but is there a specific reason as to why chars were built to only use single quotes and to break when a double quote is entered?

I have tried to do some research but the only thing that was relevant was another SO Question, which refers to the difference between single and double quotes, not the reasoning behind the syntactical development of the char.

Community
  • 1
  • 1
BuddhistBeast
  • 2,652
  • 2
  • 21
  • 29

2 Answers2

21

Because char and String are two different types, and how else is Java supposed to know whether you want a character or string of length 1?

For example, given the methods:

public void foo(String s) { }
public void foo(char c) { }

And the call

foo("a");

If characters could be made with double quotes, how would Java know which method to call?

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 4
    Yeah you can't get a more accurate answer than this. – Georgian Jan 04 '14 at 23:07
  • This pretty much explains it... I wasn't sure if there was any other specific reasoning behind the development of Java set aside from the ability to differentiate between char and string. – BuddhistBeast Jan 04 '14 at 23:08
  • 2
    @BuddhistBeast I think this one is enough specific :) – Alexis C. Jan 04 '14 at 23:09
  • I would rather java let me use single or double quotes for strings and chars(if I choose) and suffer having to do `foo((char) "a");` in places where type inference cant do that for me. – goat Jan 04 '14 at 23:09
  • @ZouZou, I completely agree. I just was not sure if there was any other reasoning behind it. – BuddhistBeast Jan 04 '14 at 23:11
  • @rambocoder Really? So you would prefer `((String) 'The variable is ') + someIntVar + ((String) '.')`? – tckmn Jan 04 '14 at 23:16
  • @rambo that would be horrific for any optimisation, chars are primative and quick; what you propose could cause a large (and wholey unnecessary) garbage collection problem for any program using lots of chars – Richard Tingle Jan 04 '14 at 23:19
  • @DoorknobofSnow no, I think strings should be the default for both quote styles since strings are orders of magnitude more common than char. – goat Jan 04 '14 at 23:25
  • 1
    @rambocoder ...which would **drastically** slow down any program using `char`s. – tckmn Jan 04 '14 at 23:26
  • @RichardTingle I think it could all be done at compile time. Again, type inference can solve most of it without the programmer needing to do explicit type labeling (like how we suffix a long literal). – goat Jan 04 '14 at 23:26
  • @rambo why? Why does string need two ways to write it? Thats like saying 3.0 and 3.0f should both give doubles because doubles are more common. The sole purpose of the single quote is to indicate chars, if chars didn't exist there would be no single quote – Richard Tingle Jan 04 '14 at 23:28
  • @rambocoder So you're basically proposing something like `"a"c` -> char, `"a"` -> String? That would be weird, as no C based languages do that AFAIK, and ugly. – tckmn Jan 04 '14 at 23:28
  • @RichardTingle because `"this is a common \"ugly and hard to read\" java string";` but `'this is "much" better';`. again, this is just my desire. even better would be a different quote altogether, like `"""anything goes""";` – goat Jan 04 '14 at 23:32
  • @DoorknobofSnow ya a suffix like that would be decent, imo. – goat Jan 04 '14 at 23:33
  • 1
    @rambocoder That's not "ugly and hard to read"; that's a normal and pretty obvious to read string literal any C/C++ programmer (or any C-based lang programmer) would see. The second one would look odd in a C-based language, and the third is just... I don't even – tckmn Jan 04 '14 at 23:33
2

This is a question written by a person used to the modern easy scripting languages whose goal is to make programming easy to learn and fast to type.

Java is a language for people who know what the CPU does underneath. In the world of low level languages (assembley, C, Java etc.) a character is an integer (UTF may require more than one int) whereas a string is an array of integers. By allowing a programmer to define the exact variable type, Java allows you to write more efficient code.

But it also allows you to write String s = "s"; if you don't care about efficiency.

Daniel Nuriyev
  • 635
  • 6
  • 10
  • ummmmm... what? How does this answer the question exactly? – tckmn Jan 04 '14 at 23:17
  • 1
    I think you've misunderstood the question. BuddhistBeast isn't asking why `char` and `String` are two separate things - they're asking why char literals can only be created with single-quotes, and string literals only with double-quotes. – Michael Petrotta Jan 04 '14 at 23:20
  • 2
    "Java is a language for people who know what the CPU does underneath." There's a bit of truth to that, but humorous none the less, given the JVM. – Gringo Suave Feb 23 '14 at 22:48
  • A virtual machine makes coding easier but it does not mean that a good programmer may be ignorant. – Daniel Nuriyev Feb 26 '14 at 16:00
  • What are you talking about? Please enclose the question in your answer!!! – Stack Overflow Jan 17 '19 at 22:13