From the course: Java 8+ Essential Training: Syntax and Structure

Declare and initialize String objects - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Declare and initialize String objects

- [Male Voice] In Java, a string is an object, and instance of a class. And when you create an instance of a class it's referred to sometimes as initialization or as instantiation. In this code, I'm starting with the data type string, that's the name of the class. Then the identifier, the name of the variable. Then after the equals operator, the assignment operator, the new keyword, and then the constructor method for that class. There are multiple constructor methods in this string class. I'm using a version of the constructor method that accepts a single argument or parameter. That value has to be a string, and a literal value wrapped in double quotes is always a string in Java. So this code declares and initializes the value, but you can also declare without initializing. In this code, once again I have the data type and the identifier, but then I finish the statement. This results in a variable where the object it points to is null. That is, it doesn't exist at all. You'll see codes sometimes in Java, particularly in command-line applications where the class essentially instantiates itself. In this code, I have a main method which is called automatically when I run the class from the command-line. And then within the main method, there's code to declare an instance of the same class. This kind of code is typically created for demonstration. It's not common in actual production applications. But after instantiating the object named 'item', I'm setting the value of one of its fields named 'type' and then calling one of its methods. And when the method is called, it displays a message on the console. So, string values are instances of the class java.lang.string. As I've mentioned previously, you don't have to import the java.lang package. It's always available. Here are two different ways of declaring a string. The first example is using classic object construction syntax with the keyword 'new' and then a call to the string constructor. The second version however is using short-hand. It's once again creating an object and assigning a value but it does it more simply, and this is a special feature of the string class. Internally, these two bits of code are doing something a little bit different, but functionally for most applications you get the same result. Here are some other things to know about strings. A string refers to an array of character values. So for example, if I have a string object who's value is hello with an exclamation, that contains an array of char values, six values in an ordered collection. Another thing to know about strings is that they're immutable. Once initialized, their values can't be changed. Java code makes it look like you can do this. For example, I can declare a string variable and then immediately assign a different value, and the compiler will let you do that and it will work. Internally though, this is resulting in creating another object when you reset the value. Because a string is made up of characters, you can start with an array of characters and then create a string from it. In this code I'm declaring an array of char values and then I'm creating a string using a particular constructor of the string class that receives that array. The result is a string just like any other. So those are a few things to know about strings in Java. Throughout the rest of this chapter, I'll share some other things that you can do with string values.

Contents