Hemza SM
last month
Hemzacountry asked

I learned JavaScript first. In JavaScript, there isn't much difference between ' ' and " ". How are they different in Java?

Kelish Rai
Expert
2 weeks ago
Kelish Rai answered

You're right that in JavaScript, there's not much difference between using single quotes (' ') and double quotes (" "), as both are used to define strings.

But in Java, there's an important distinction:

  • Single quotes are used for single characters — this data type is called char.

  • Double quotes are used for strings, which can contain multiple characters.

Here's an example:

// Single character using single quotes
char letter = 'A';

// A string of characters using double quotes
String word = "Hello";

If you try to store multiple characters inside single quotes like this:

char letter = 'Hello'; // This will cause an error

Java will throw an error because char can only hold a single character, not a sequence.

In short,

  • char is meant for a single character (like 'A', 'b', '5', or '@').

  • String is meant for text that can have one or many characters (like "Hello", "123", "Good morning").

Java
This question was asked as part of the Learn Java Basics course.