devxlogo

Using Single Quotes in Java

The difference is visible when you use the single quote with the + sign. The char value is converted to the ascii value and then summed up.

However, you have to be careful when using in concatenation with a String. The value is just concatenated and not ascii converted.

public class UsageOfSingleQuote{   public static void main(String args[])   {      UsageOfSingleQuote usageOfSingleQuote = new UsageOfSingleQuote();      usageOfSingleQuote.proceed();   }      private void proceed()   {      System.out.println("Prints: " + 'A'); //Prints the char      System.out.println("Prints: " + 'a'); //Prints the char      System.out.println('A' + 'A'); //Converts to ascii value , sums up and then prints the value      System.out.println("Abc" + 'A'); //Pure String concatenation   }}/*

Expected output:

[root@mypc]# java UsageOfSingleQuotePrints: APrints: a130AbcA*/ 
Image

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.