Java String

Java String Course

Engineers From These Top Companies and Universities Trust EXLskills

1M+ Professionals | 100+ Institutions



This is the EXLskills free and open-source Java String Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java Strings.

Is this course FREE?

Yes, this a 100% free course that you can contribute to on GitHub here!

Have more questions?

Feel free to reach out to us via live chat here!

Strings

Data Type

🙋 Need help? Ask an expert now!

We covered primitive data types and the operators that work with those data types. As we begin to discuss String, it may seem like a primitive data type. However, String is its own class in the java.lang package. The String class contains objects which are commonly used in Java programming. You will see how useful String is as we explore further. Take a look at the example below of a String that holds the value "Hello World!"

DataTypeStringExample.java
package exlcode;

public class DataTypeStringExample {

  // this creates a new String that holds "Hello World!"
  public static String exampleVariableOne = new String("Hello World!");

  public static void main(String[] args) {
    System.out.println(exampleVariableOne);
  }
}

In Java, String is a line of text that strings together letters or numbers and stores them as a sequence of characters. The string "Hi!" is made up of the characters 'H', 'i', and '!'.

Let's take a look at how the Java operator '+' works with strings and other data types like integers. If a String is before the '+' operator and followed by a non-string value, the '+' operator converts the non-string value into a string and chains them together. For example System.out.println("Hi" + 1 + 2); will not print "Hi3". Instead, it will print "Hi12" as the '+' sign changes the '1' and '2' to strings before chaining the strings together. The '1' and '2' are no longer integers and do not qualify for addition. It is best to avoid arithmetic calculation if a String exists before the addition.

Edit Me on GitHub!

Application Question

Consider the following statement:

System.out.println("Hello" + 1 + 2 + 3);


What is printed as a result of executing this statement?