Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In Java, both char and String types deal with characters. But they have different properties and uses.

In this tutorial, we’ll explore the differences between char and String in Java programming.

2. char and String

A char represents a single 16-bit Unicode character in Java, such as a letter, digit, or symbol. Therefore, the char type’s range is ‘\u0000‘ to ‘\uffff‘ (inclusive). We can also say char is an unsigned 16-bit integer from 0 to 65535 (216-1).

However, String is an essential class in Java. A string consists of single or multiple characters.

As we can see, both char and String have something to do with characters. Next, let’s take a closer look at these two data types and discuss their differences.

For simplicity, we’ll see the result using unit test assertions in examples.

3. char Is a Primitive Type, While String Is a Class

The first difference between char and String in Java is that char is a primitive type, while String is a class. In other words, it’s a reference type.

Therefore, char doesn’t require the overhead of an object. Thus, it has the advantage in terms of performance and memory footprint. However, primitive types are limited in functionality as they don’t have any member methods.

Further, Java Generics doesn’t support primitive types either.

4. char Can Represent Only One Single Character, but String Is a CharSequence

Now, let’s look at the String class’s signature:

public final class String implements Serializable, Comparable<String>, CharSequence, Constable, ConstantDesc{ ... }

As the code shows, the String class implements the CharSequence interface. That is to say, a String object is a sequence of characters.

But a char variable can only carry one single character.

Let’s understand it through an example:

char h = 'h';
char e = 'e';
char l = 'l';
char o = 'o';

String hello = "hello";
assertEquals(h, hello.charAt(0));
assertEquals(e, hello.charAt(1));
assertEquals(l, hello.charAt(2));
assertEquals(l, hello.charAt(3));
assertEquals(o, hello.charAt(4));

As we can see, four char variables (h, e, l, o) are in the test above. The string “hello” consists of five characters. We can verify it by comparing each char variable with the result of CharSequence‘s charAt() method.

Also, we can treat a string as an array of chars. The String class offers us the toCharArray() method to break a string into chars in an array:

char[] chars = new char[] { h, e, l, l, o };
char[] charsFromString = hello.toCharArray();
assertArrayEquals(chars, charsFromString);

5. The Addition (+) Operator

When we apply the addition operator to two strings, it concatenates them:

String h = "H";
String i = "i";
assertEquals("Hi", h + i);

However, we should note that the result is an int when we do char + char. The value is the sum of these two chars. An example can address it straightforwardly:

char h = 'H'; // the value is 72
char i = 'i'; // the value is 105
assertEquals(177, h + i);
assertInstanceOf(Integer.class, h + i);

Furthermore, when we “add” a char and a String, the char will be converted to a string and concatenated with the given string. For example, we often “add” an empty string to a char to turn the char variable into a single-character String:

char c = 'C'; 
assertEquals("C", "" + c);

However, we should beware of the strings’ position when we “add” multiple char variables with strings. Next, let’s see another example:

char h = 'H'; // the value is 72
char i = 'i'; // the value is 105
assertEquals("Hi", "" + h + i); //(1)
assertEquals("Hi", h + "" + i); //(2)
assertEquals("177", h + i + "");//(3)

In this example, the first two additions produce the string “Hi“. But the last one’s result is the string “177. This is because the addition expressions are executed from left to right.

In the first two expressions, no matter “” + h or h + “”, Java converts the char variable h into a String and concatenates it with the empty string. However, in the last one, h + i produces an int result (72 + 105 = 177), as we’ve learned earlier. Then, the int result is converted into a string to concatenate with the empty string.

6. Conclusion

In this article, we discussed the differences between char and String in Java. Let’s list them here as a summary:

  • char is a primitive type. But String is a reference type.
  • char represents only one single character, while String can contain multiple characters.
  • char + char = the sum of the two chars as an int. String + String concatenates the two strings.

As usual, all code snippets presented here are available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.