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

Structure of the String class

New Java Syntax
Level 9 , Lesson 4
Available

"What do you think, Amigo? What is the most popular Java class after int?"

"You already gave me a spoiler in the title of the lesson, Ellie. It's String!"

"Indeed, it is a spoiler. String 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."

"I can't wait."

"Well, we'll start from the very core of this ecosystem — the organization of the String class. 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

It's 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.

Methods of the String class

The String class has a lot of methods: it has 18 constructors alone! So, below I will list only the most frequently used ones:

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.

"That looks very good!"

"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.

"I liked the third solution the most. But I'll practice all three."

"Well done, Amigo. I see that you're already impatient to put your new knowledge into practice. The lesson is over."

Comments (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
curi0usmind Level 9, Switzerland Expert
27 November 2024
Do you know why Windows uses backslashes \? Because they're always moving backwards.
LX40GL Level 28, Birmingham, United States
3 September 2022
In Solution 2, about

path.split("\\/")
, I tested this code in IDEA. It suggested that "\\/" is redundant. And when I used

path.split("/")
, it works too. Honestly, I don't understand "the extra two slashes are the result of double escaping".🤔