CodeGym /Java Course /Java Syntax Zero /Structure of the String class

Structure of the String class

Java Syntax Zero
Level 10 , Lesson 4
Available

1. Structure of the String class

Today we will talk about the String class. After ints, the String class is the most popular class in Java. It is used absolutely everywhere. It has a bunch of useful methods that you are better off knowing.

The String class is the only class other than primitive types whose literals can be used in a switch statement; the compiler handles string addition and String objects in a special way; String objects are storied in memory in a special way. Basically, the String class is a very special class.

Also, the Stringclass has a bunch of helper classes whose purpose is to further simplify working with strings in Java. When you learn all this, many thing will really become much easier for you to do. Well, we'll start from the very core of this ecosystem — the organization of the String class.

Array of characters

The structure of the String class is actually very simple: inside it is a character array (char array) that stores all the characters of the string. For example, this is how the word 'Hello' is stored:

Structure of the String class
Important!

Actually, this is not quite accurate. Because the String class is very important, it uses a lot of optimizations, and the data is internally stored not as a character array, but simply as a byte array.


2. Methods of the String class

The String class has a lot of methods: it has 18 constructors alone! So below we only mention the most basic of them:

Methods Description
int length()
Returns the number of characters in the string
boolean isEmpty()
Checks whether the string is an empty string
boolean isBlank()
Checks that the string contains only whitespace characters: space, tab, new line, etc.
char charAt(int index)
Returns the character at the index position in the string.
char[] toCharArray()
Returns an array of the characters (a copy) that make up the string
byte[] getBytes()
Converts a string to a set of bytes and returns the array of bytes.
String[] split(String regex)
Splits a string into multiple substrings.
String join(CharSequence delimiter, elements)
Joins multiple substrings together
String intern()
Puts a string into the string pool.

You can learn more about constructors in the article Java Constructors.

Let's write a program that converts a file path from Unix style to Windows style. Unix uses the / character to separate folders, while Windows uses the \ character.

Solution 1: using a char array

Code Notes
Scanner console = new Scanner(System.in);
String path = console.nextLine();

char[] chars = path.toCharArray();
for (int i = 0; i < chars.length; i++)
   if (chars[i] == '/')
      chars[i] = '\\';

String result = new String(chars);
System.out.println(result);
Create a Scanner object
Read a line from the console

Convert a string to a character array
Loop over the characters
If the character is /,
replace it with \. Don't forget about escaping.

Create a new string based on the character array.
Display the string.

Solution 2: — using the split() and join() methods

Code Notes
Scanner console = new Scanner(System.in);
String path = console.nextLine();

String[] array = path.split("/");


String result = String.join("\\", array);


System.out.println(result);
Create a Scanner object
Read a line from the console

Convert string to an array of strings. The / character is used as a separator (the extra two slashes are the result of double escaping).
Concatenate all the strings in the array of strings. The \ is used as a separator (we see it escaped).

Display the string.

Solution 3: — using the replace(char oldChar, char newChar) method

Code Notes
Scanner console = new Scanner(System.in);
String path = console.nextLine();

String result = path.replace('/', '\\');

System.out.println(result);
Create a Scanner object
Read a line from the console

Simply replace one character with another
(the second is escaped)
Display the string.
10
Task
New Java Syntax, level 10, lesson 4
Locked
Populate the Friend class
A friend (Friend class) must have three initializers (three initialize methods): - Name - Name, age - Name, age, sex. Note: name is a String, age is an int, and sex is a char.
10
Task
New Java Syntax, level 10, lesson 4
Locked
Populate the Dog class
A dog (Dog class) must have three initializers: - Name - Name, height - Name, height, color.
10
Task
New Java Syntax, level 10, lesson 4
Locked
Let's put together a rectangle
The data for the rectangle (Rectangle class) will be top, left, width, and height. Create as many initialize(...) methods as possible Examples: - 4 parameters are specified: left, top, width, height - width/height is not specified (both are 0); - height is not specified (it is equal to the width), w
10
Task
New Java Syntax, level 10, lesson 4
Locked
Initializing objects
A person (Person class) should have a String name and int age. Add the initialize(String name, int age) method where you will initialize the variables name and age. In the main method, create a Person object and store a reference to it in the variable person. Call the initialize method with any valu
10
Task
New Java Syntax, level 10, lesson 4
Locked
Initializing cats
A cat (Cat class) must have five initializers: - Name - Name, weight, age - Name, age (standard weight) - Weight, color (unknown name, address and age, i.e. a homeless cat) - Weight, color, address (someone else's pet). The initializer's job is to make the object valid. For example, if the weight i
10
Task
New Java Syntax, level 10, lesson 4
Locked
Populate the Circle class
The Circle class must have three initializers: - centerX, centerY, radius - centerX, centerY, radius, width - centerX, centerY, radius, width, color.
10
Task
New Java Syntax, level 10, lesson 4
Locked
Initializing objects
Study the Person class carefully. Correct the class so that only one initialize method initializes all of the Person class's instance variables.
Comments (3)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Campbell, Jackson Level 12, Corona, USA
30 October 2023
Pretty cool :) ඩ
Abhishek Tripathi Level 72, Rewa, India Expert
6 July 2023
Very interesting topic but hard to remember the methods.
Chrizzly Level 12, Earth, Germany
10 November 2022
Very interesting topic!