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


Character literals represents alphabets (both cases), numbers (0 to 9), special characters (@, ?, & etc.) and escape sequences like
, \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

Ali
Ali

Updated on: 30-Jul-2019

619 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements