4) Java's Primitive Data Types Lesson

What is a Java String?

7 min to complete · By Ryan Desmond

A String is a non-primitive data type that represents characters in Java. It is the first Object type you will use. Since day one, you have been working with Strings. For example:

// prints the String "Hello world" to the console
System.out.println("Hello world"); 

How to Use Strings?

Strings are instances of the String class, and there are several ways that they can be used.

//printing a string to the console
System.out.println("hello world"); 

Create String variables

// string variable
String variable = "hello world"; 

Create Strings Using the String Constructor

Strings can also be created with their constructor, just like any other object. It is uncommon to create a String using the constructor, but it is necessary to know that it is possible because it is an Object. Here's an example:

// creating a string using the String constructor
String stringObject = new String("hello world"); 

Concatenate Strings

You can "concatenate" Strings - which means you can combine different pieces of information into a single String. You concatenate Strings using the + operator. Here's an example:

// concatenating two strings and 
// printing them to the console
System.out.println("String one " + "String two");

Another way to use Strings is to use the built-in methods that come with the String class.

What are String Methods?

There are a few built-in methods to operate on Strings.

Length of the String

This method returns the length of the String, which means the number of characters.

/* returns the length of the String */  
int length = "hello".length();  

Strings Equal

This method checks whether two strings are equal.

/* returns true if Strings match */  
boolean equals = "hello".equals("hello");  

Search String

This method searches for a substring inside of another String.

/* searches "hello" for the substring "o" and returns 
the index of the first match. Returns -1 if not found */  
int search = "hello".indexOf("o");  

Are Strings Immutable?

Strings are immutable objects, meaning you cannot change them after they have been created. However, you can change the value of a String since it creates a new object for the new String, and the previous object from the previous String is placed in the garbage collector.

//This is the original object
String str = "original String";

//This is still "str", but it is actually a new object
str = "some new String";  

Experiment with Java's String Class

In the REPL below, you can see a few examples of the many methods that you can call on a String. Feel free to explore and play around with the code.

Colorful illustration of a light bulb

Tip: You can expand the width of the content area of this screen, and the code editor/playground by collapsing the left navigation by click the "X" towards the top left of the page, just above the navigation.

class Main {
  public static void main(String[] args) {
    String str1 = "Hello String 1!";
    String str2 = "Hello String 2!";

    // concatenating Strings
    System.out.println("String1 length is " + str1.length());

    // utilizing the String class method equalsIgnoreCase() 
    // (these will make more sense soon)
    boolean equalStrings = str1.equalsIgnoreCase(str2);
    System.out.println("Do str1 & str2 match? " + equalStrings);

    // utilizing the String class method subString()
    String subStr = str1.substring(8, 12);
    System.out.println("The substring is: " + subStr);

    // utilizing the String class method charAt()
    char letterAt = str2.charAt(8);
    System.out.println("The char at the 8th index of str2 is " 
      + letterAt);

    // utilizing the String class method toUpperCase()
    System.out.println("All uppercase: " + str1.toUpperCase());
    // utilizing the String class method to LowerCase()
    System.out.println("All lowercase: " + str1.toLowerCase());

  }
}

Summary: What is a String in Java?

  • The String class represents characters in Java
  • Strings are immutable
  • Strings are of the Object data type (aka non-primitive)

How to Use Strings?

  • Use System.out.println() to print them to the console
  • Use standard variable notation to create them as a variable
  • Use new String() to create them using the class constructor
  • Use + to concatenate two strings together
  • Use length() method to find the number of characters
  • Use equal() method to see if two Strings are the same
  • Use indexOf() method to search for a substring
Morty Proxy This is a proxified and sanitized view of the page, visit original site.