Skip to content

Difference Between String and char[] in Java

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

Short answer

Strings

String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters.

A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify it. This means when you perform operations on a string, you always get a new string, creating a new object in memory. Million operations - million new objects. StringBuilder with its append method optimizes this.

char[] (array of characters)

char[] is an array of characters with only methods from objects, which are mostly not helpful.

The size of an array is fixed. Once created, you cannot increase the array size but replace one array item with another.

More detailed answer

Strings

String has no primitives, it is a defined class type and has methods to operate on it.

A String is a -

  1. Sequence of characters. ✅
  2. Array/list of characters. ✅
  3. Group/set of characters? ❌ (this is not true).

Create, access, and other methods

Strings can be created in Java using string literals or by creating a String object using new keyword.

// creating a string object
String name = new String("John Doe");

// using string literal
String name = "John Doe";

// length
name.length();

// concatenation: Strings can be concatenated using the + operator or by using the concat() method.
String str1 = "Hello";
String str2 = "World";
String result = str1 + " " + str2; // "Hello World"

// accessing/iterating over each character in string
for(int i = 0; i < name.length(); i += 1) {
  char ch = name.charAt(i);
  System.out.println(ch);
}

for(char ch: name.toCharArray) {
  System.out.println(ch);
}

String comparison

"str1" == "str2" // double equals here compares references in memory

// Instead use equals or equalsIgnoreCase
"str1".equals("str2");
"str1".equalsIgnoreCase("str2");

"abc" == "abc" returns true. That's because of the Java string pool, the special memory region where the JVM stores strings. Java tries to optimize memory that way and refers identical strings to the same place in memory.

String pool

Java maintains a special memory area called the "String Pool" for string literals. This can help conserve memory by reusing common strings.

char[]

char[] in Java stores an ordered sequence of characters. char[] arrays are mutable, meaning you can change the value of individual characters after the array is created.

ASCII values

ASCII values are in between [0 255]. We have 256 unique characters in ASCII, each occupying 8-bit OR 1-byte memory.

Create, access, and other methods

// declaration
char[] charArray;

// declaration and initialization
char[] charArray = {'H', 'e', 'l', 'l', 'o'};

// access elements
char firstChar = charArray[0]; // This will be 'H'

// length
int length = charArray.length; // This will be 5

// iterate
for(char ch : charArray) {
  System.out.print(ch + " "); // This will print: H e l l o
}

// modifying
charArray[0] = 'h'; // Changes the first element to 'h'


char[] arrays are commonly used for situations where mutable character data is needed, such as when performing low-level text processing.

String to char[] conversion

You can convert a String to a char[] using the toCharArray() method of the String class.

String str = "Hello";

// Converts the string to a char array
char[] charArray = str.toCharArray(); // ['H', 'e', 'l', 'l', 'o']
Java Interview HandbookJava

Gopi Gorantala Twitter

Gopi is an Engineering Manager with over 14 years of extensive expertise in Java-based applications. He resides in Europe and specializes in designing and scaling high-performance applications.

Comments


Related Posts

Members Public

Spring Boot Hello World Tutorial with Lombok and H2 Database – Quick Start for Beginners

Learn how to create a Hello World Spring Boot application using Lombok for cleaner code and the H2 in-memory database for rapid development. This step-by-step guide includes annotations, project setup, REST API, H2 console access, and more to kickstart your Spring Boot journey.

Spring Boot Hello World Tutorial with Lombok and H2 Database – Quick Start for Beginners
Members Public

NoSuchBeanDefinitionException: The Most Common Spring Bean Error

Learn how to fix NoSuchBeanDefinitionException in Spring Boot with clear examples and best practices for dependency injection, package scanning, and bean registration.

NoSuchBeanDefinitionException for a missing bean in the ApplicationContext.
Members Public

BeanCurrentlyInCreationException: Circular Dependencies in Spring

Spring Boot’s DI can backfire with the dreaded BeanCurrentlyInCreationException, signaling an unresolvable circular dependency. Learn why cycles occur, how they breach SOLID principles, and three fixes—refactor to break loops, use @Lazy injection, and static @Bean factory methods—to restore startup.

Circular dependencies in Spring causing a BeanCurrentlyInCreationException