Difference between char and string in Java
This post will discuss the difference between char and string in Java.
A char and string have some significant differences and trade-offs that make them suitable for different scenarios and use cases. The following sections will compare and contrast char and string in Java:
1. Overview of char and string in Java
The char is a primitive data type that represents a single 16-bit Unicode character in Java. A char can be any letter, digit, symbol, or punctuation mark that can be represented using Unicode. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
String is one of the most commonly used classes in Java belonging to the java.lang package. A String represents a sequence of characters, and can contain zero or more characters, such as letters, digits, symbols, or punctuation marks. The characters in a String can be of any type, such as ASCII, Unicode, or UTF-16. It offers many benefits.
- String uses a char array to store its characters, and allows fast access to characters by using index that starts from zero.
- String maintains the order of the characters in the sequence.
- String allows duplicate characters in the sequence.
- String class provides several utility methods, such as length, charAt, substring, concat, replace, trim, etc.
2. Difference between char and string in Java
Here are some of the main differences between char and string in Java:
- A char is a single character, while a string consists of a sequence of characters (zero or more characters).
- A char is a primitive data type, while a string is a class in Java. Note that there also exists an object Wrapper type
Characterfor primitive char. - A char literal needs to be enclosed in a single quotes, while a string literal needs to be enclosed in the double-quotes. For example:
char ch = 'A'; String str = "ABC"; - A char is mutable in Java, which means that it can be changed after it is created. A string is immutable, which means that it cannot be changed after it is created.
3. When to use char and string in Java?
As we have seen, char and string are useful to store and process data in Java. Here are some general recommendation on how to choose between them:
- If you need to store a single letter, digit, symbol, or punctuation mark that can be represented using Unicode, we can use a char.
- If you need to store a sequence of zero or more letters, digits, symbols, or punctuation marks that can be represented using Unicode, we should use a String.
4. Conversion between char and string in Java
The standard approach to convert a char to a string in Java is to use the String.valueOf(char) method, which returns the string representation of the specified char argument.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Main { public static String charToString(char ch) { return String.valueOf(ch); } public static void main(String[] args) { char c = 'A'; String s = charToString(c); System.out.println(s); // "A" } } |
We can convert a string containing exactly one character to its equivalent primitive char value in Java. The idea is to call the charAt(int) method on the specified string to retrieve the character value present at the first index.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Main { public static char stringToChar(String str) { return str.charAt(0); } public static void main(String[] args) { String s = "A"; char c = stringToChar(s); System.out.println(c); // 'A' } } |
That’s all about the differences between char and string in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)