Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Creating Strings
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!
There are two distinct ways to create a String: use double-quotation marks or create a new object. Either put text on the same line in double quotation marks as if it was primitive datatype: String stringName = "hi"; or initialize them like this: String stringName = new String("hi");. Take a look below for examples on how we can initialize a String.
StringsExample.java
package exlcode;
public class StringsExample {
public static String exampleVariableOne = new String("Hello World!");
// the next two statements create an empty String
public static String exampleVariableTwo = new String();
public static String exampleVariableThree = "";
public static String exampleVariableFour = "Java";
public static void main(String[] args) {
System.out.println(exampleVariableOne);
System.out.println(exampleVariableTwo);
System.out.println(exampleVariableThree);
System.out.println(exampleVariableFour);
}
}
To create a new object, we use the Java reserved word new because String is a class within the java.lang package. However, Java has made the process of importing easier for programmers. As you see above, you can initialize the String without having to type out new. Ultimately, it is up to you to decide which method of creating a String works best for you.
Remember, it is possible to create an empty String by either leaving the parenthesis blank or omitting characters on the inside of the double-quotation marks.