Java Fundamentals Prework
The String Data Type
Creating and working with the String type
15 min
The String type is unique for a few reasons. The first of these is that it’s one of the few object types with a literal representation: letters surrounded by double quotes - "This is a string!".
❓ How is this different from the char data type?
What this means is that you can instantiate (create an instance) of a String like this:
src/main/java/Main.java
/*
String variables can be created with the same syntax
we use to create primitive variables
*/
String myStr = "I'm a string.";
This contrasts with how the data for many other object data types is typically created - by using the new keyword to instantiate a new object of the specified data type:
src/main/java/Main.java
// Variable assigned like an object using a constructor
String myOtherStr = new String("I'm a string too!");
You can create strings using either of these methods, but you’ll typically use the literal notation.
🧠 There are subtle differences in strings created using these two methods - check out this article for some of the details behind this if you’re curious.
null
A null value is an empty value. It is specifically signifies a lack of value.
Strings, like all other object types, are given a value of null when they are declared but not initialized:
src/main/java/Main.java
String myUninitializedString;
System.out.println(myUninitializedString);
// This will cause an error - you cannot use an uninitialized variable.
String helper methods
Because a string is an object, it has predefined methods you can use.
length()
Used to find the length of a string:
src/main/java/Main.java
System.out.println("Hello!".length());
// Prints: 6
charAt()
To get the first letter of a string:
src/main/java/Main.java
System.out.println("Hey".charAt(0));
// Prints: "H"
replace()
To replace part of a string:
src/main/java/Main.java
System.out.println("Hello, world".replace("world", "friend"));
// Prints: "Hello, friend"
toUpperCase()
To make a string uppercase:
src/main/java/Main.java
System.out.println("hello!".toUpperCase());
// Prints: "HELLO!"
concat()
To concatenate two strings together:
src/main/java/Main.java
System.out.println("hello".concat(" world"));
// Prints: "hello world"
You can also add strings together using +:
src/main/java/Main.java
String twoStringsTogether = "Hello" + " World";
System.out.println(twoStringsTogether);
// Prints: "hello world"
These are just the beginning
Check out the Method Summary section of the Java String class docs for even more.
String mutation
Note that none of the above methods mutate an existing string. Instead, they return a new string without modifying the original. For example:
src/main/java/Main.java
String greeting = "Hello"
String greetingsFriend = string.concat(", friend")
System.out.println(greeting);
// Prints: "Hello"
System.out.println(greetingsFriend);
// Prints: "Hello, friend"
Strings cannot be mutated in Java.
Equality among strings
What if you want to compare one string to another?
src/main/java/Main.java
String blue = "blue";
boolean withSign = (blue == "blue"); //=> true
boolean withWords = (blue).equals("blue"); //=> true
Do you know which one of these would be preferred? Let’s walk through another example to show you which and why:
src/main/java/Main.java
String blue = "blue";
String bl = "bl";
String ue = "ue";
System.out.println(bl+ue); //=> blue
boolean withSigns = (bl+ue == blue); //=> false
boolean withWords = (bl+ue).equals(blue); //=> true
Why isn’t withSigns true when the output looks the same? Remember, bl and ue hold String data, which is an object type, and object types hold a reference, not a value.
The == operator compares the location where the object is stored in memory to see whether or not they’re the same. blue contains a reference to where it’s stored on the computer, and that’s different from where bl + ue is stored.
Put another way, the == operator compares identity.
The equals() method, on the other hand, can be called on an instance of a String object compares whether or not the characters in each String are the same, not whether or not the references are the same.
Or in another way, the equals() method compares value.
In short, when comparing object types, you will most often want to compare value, not identity. Therefore you will use the equals() method when comparing strings, and almost any other object type.
Comparing data types
Why can you call methods on a variable with a String data type but not an int?