0

Why does my code work, when I use " in the following code:

for(int i=0;i<7;i++){
        if(grid[row][i]!=0){
            if(player == "yellow"){
                grid[row][i-1] = 'y';
            }
            else if(player == "red"){
                grid[row][i-1] = 'r';
            }
        }
    }

But don't work, when I use ' in the following code:

for(int i=0;i<7;i++){
        if(grid[row][i]!=0){
            if(player == 'yellow'){
                grid[row][i-1] = 'y';
            }
            else if(player == 'red'){
                grid[row][i-1] = 'r';
            }
        }
    }

It is always saying, "Invalid character constant".
- grid is a 2 dimensional char variable
- row is an interger
- player is the super class (window) protected and in the constructor of the super class occupied with the value yellow:

protected String player;

public window() {
    player = "yellow";
}
Ichor de Dionysos
  • 1,107
  • 1
  • 8
  • 30
  • 7
    "" is for Strings, '' is for chars. – jyoonPro Apr 20 '15 at 21:03
  • 3
    Always compare String objects using the equals method – copeg Apr 20 '15 at 21:04
  • This might explain it very well: http://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c – DTH Apr 20 '15 at 21:07
  • *Why does my code work, when I use " in the following code* when you use double quotes your code *compiles* but **it won't work as expected** because `String`s are compared using `equals` method, not `==`. For more info on this, check [How do I compare strings in Java?](http://stackoverflow.com/q/513832/1065197) – Luiggi Mendoza Apr 20 '15 at 21:10
  • Darn, I was typing up a neat answer and the question got closed. – jyoonPro Apr 20 '15 at 21:12
  • @DTH please provide answers based on the specific programming language OP asks. That question belongs to C/C++ and this is Java, they're different (even if the answer may apply for this case). – Luiggi Mendoza Apr 20 '15 at 21:13
  • Since I can't post an answer, I'm summarizing it in this comment. Basically, a char is a primative data type, while String is an object. This means that String will provide you with some decent methods like `substring()` (there is a Character class, which is a wrapper class of char though). – jyoonPro Apr 20 '15 at 21:16

2 Answers2

5

A character is just a single character, such as 'a' or 'b'. A String is one or many characters strung together, like "hello world" or "p". Single quotes(') are used for character literals, while double quotes(") are used for Strings.

kirbyquerby
  • 735
  • 5
  • 16
  • 2
    Your initial statement isn't quite right, unless this is an area java differs from C#, a char is a single character, however just because it has a single character doesn't mean it's a char. "a" and 'a' are two different values. "a" is a string with a length of 1 and 'a' is a char value. – CalebB Apr 20 '15 at 21:07
  • You're absolutely right, thank you for noticing that. – kirbyquerby Apr 20 '15 at 21:08
  • Okay, thank you. That helps me understanding Java even better. – Ichor de Dionysos Apr 21 '15 at 20:36
3

In java you use "" for strings of letters (Strings) and '' for a single character (Chars)

Example:

  • "The fox jumped" would be surrounded by double quotes because it has more than one character.
  • 'c' would be surrounded by single quotes because it has only one character.

Be aware that a String can be composed by a single or multiple characters, while a char consists of a single character.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Forseth11
  • 1,418
  • 1
  • 12
  • 21