What is the difference between character literals and string literals in Java?

Java 8Object Oriented ProgrammingProgramming

Character literals represents alphabets (both cases), numbers (0 to 9), special characters (@, ?, & etc.) and escape sequences like \n, \b etc.

Whereas, the String literal represents objects of String class.

Example

Live Demo

public class LiteralsExample {
   public static void main(String args[]){
      char ch = 'H';
      String str = "Hello";
      
      System.out.println("Value of character: "+ch);
      System.out.println("Value of string: "+str);
   }
}

Output

Value of character: H
Value of string: Hello
raja
Ali
Published on 10-Jan-2018 05:54:17
Advertisements