Join David Gassner for an in-depth discussion in this video Working with object data types, part of Java 8 Essential Training.
- So far, I've described variables in Java that use the primitive data types: numbers, characters, and booleans. But there's a whole other category of Java variables that are references to objects. An object in Java is an instance of a Java class. When you create a variable, and you point it at an object, the variable isn't the object itself. Instead, it's simply referencing the object. And you can actually have multiple reference variables that are pointing to a single object in memory.
So, you might declare an object that has a name of anObject, and then you might have three different variables that are all pointing to the same thing. If something changes about that object in memory, all three of those variables would update. Here's an example: let's say that I've defined a Java class. This class is named ClothingItem, and I've declared a string variable that's a member of the class. Notice that this string variable doesn't have the word static.
That means it's something called an instance variable, also known as a field. An instance variable is not a member of the class itself. It's a member of an instance of the class. Now, in my main method, I'm creating an instance of the ClothingItem class. This line of code has a data type, which is the name of the class, then the identifier, which is the name of the variable, item, then the keyword new, and then finally a call to something called a constructor method.
I'll go through constructor methods in detail later, but what you need to know now is that this is how you create an instance of a class in Java. It's called instantiation. Now once the instance is created, the variables that you declared as instance variables are now available. So I can assign a value to the type variable, or field, and that's what the next line is doing: item.type = "Hat"; Class instances, or objects, can also have instance methods, And that's what displayItem is.
This is a method, once again, without the static keyword, and therefore, it's a member of an instance of a class, not a member of the class itself. So, I can call it from item, the object, and then it will output information that belongs to that instance of the class. The expression this.type means the type variable that's a member of the current instance of this class. So, instance variable, instantiation, and instance method are three bits of vocabulary you should know about in order to work successfully with Java classes and instances of those Java classes known as objects.
A string in Java is a complex object. It's an instance of the string class. The string class, just like the helper classes for the primitive data types, is a member of java.lang, so you don't have to import it or use any special notation to use the string class; it's always available to you. Here's an example of creating an instance of the string class. Once again, I start with the data type, String with an uppercase S, then I assign the name, or identifier, then after the equal sign, that's the assignment operator, I have the keyword new, then I'm calling the constructor method of the class and passing in a value.
This creates a string variable. The string class is a little bit special because you can also use a shortcut. The compiler understands this to mean exactly the same thing: create an instance of the string class and assign it this value. But using this syntax, you can eliminate the new keyword and the explicit call to the constructor method. But again, these two lines of code mean exactly the same thing. Here're some other important characteristics of strings.
A string is basically an array of characters. A character is a primitive, a string is a complex object. But a string can have as many characters as you want, assuming you have adequate memory in the computer that's running your code. So a string is a collection of characters in a particular order. This might be a String object, the string "Hello!", but you can break it down into an array of Char values. Another important thing to know about strings is that they're immutable.
That means that once you instantiate and assign their values, you can't really change them. Now, the Java compiler will make it look like you can change them. For example, if I start with a variable named StringOne, and I assign it a value of "Hello!", and then on the very next line I say StringOne = "Hello, world!", it looks like I'm changing the object. But in fact, in the background, I'm de-referencing the original object. That object can now be cleared from memory in a process known as garbage collection, and I'm creating a brand new object with the new value.
It's essential to know this because if you've worked with languages where strings aren't immutable, where you can change them at run time, you might used to simply upending values over and over again. But in Java, that results in creating a lot of objects when you might not mean to do that. And it can create memory management problems in the background, over which you have very little control. Here are some other examples of working with strings and characters. In this class, I'm declaring an array of characters.
The expression char with open bracket and close bracket means an array. Then I have the variable name, chars, that's the identifier, and then I'm explicitly assigning an array using the braces. Each item in the array is separated from the next with a comma. Each of the characters is a literal character, using single quotes. Now, to turn that into an actual string object, I create the string, and I use the string constructor method, and I pass in my character array.
And the result is an actual complex object.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files