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

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 \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
Updated on: 2019-07-30T22:30:20+05:30

918 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements