Join David Gassner for an in-depth discussion in this video Using the String class, part of Java Essential Training.
- So let's create a few strings. I'm working in the project StringClass and I'll add code to the main method. I'll declare a string that I'll name s1. And I'll assign its value with a literal. String literals are always wrapped in double quotes. And I'll assign This is a String. Then I'll output that value using System.out.println and I'll output s1. And I'll run that code and I get what's expected.
This is a String. Now I'll declare a second String but this time I'll use class constructor method syntax. And I'll pass in another String literal of This is also a String. I'll duplicate and move this line down and I'll output s2. And again I get back the expected result. So again there is no functional difference between these two syntax styles. They're both creating a String object and which you use is a matter of preference.
You can also create strings by concatenating or appending values together. I'll create another String that I'll call s3 and I'll give this a value of Shirt size. Then I'll create another String with a value of M for medium. And finally I'll create a String where I put those values together and then I'll output the result. And I get Shirt size : M. And I can continue concatenating values. To the end of this I'll add another literal value starting with a comma then Qty: and then I'll append a numeric literal to the end of that.
When you append numbers as we've seen previously they're automatically turned into Strings during the compilation process. And now when I output the value I get a single String consisting of all of those values. I mentioned earlier that Strings can be built from arrays of characters. So I'll declare an array of characters starting off with the primitive data type char followed by a pair of brackets. I'll name this array chars and I'll instantiate it with a pair of braces and then the five characters in the word hello.
Each of these is a literal character. So they're wrapped in single quotes. So now I have an array of characters and I want to turn it into a String. So I'll declare a String that I name s6 and I'll instantiate it with this String's constructor method. And I'll use the second version that accepts a character array passing in chars and then I'll output the result and I get Hello. Now let's take a look at what happens when I go in the other direction.
When I want to take a String and turn it into an array of chars. I'll declare another character array. And I'll name it chars2 and I'll get its value by calling it an instance method of the String class to character array. That returns exactly what I'm looking for an array of characters. Now to print this out I'll need to print one character at a time. I'm going to use something called a for-each loop. This is a bit of syntax we haven't seen before.
I'll type it in and then I'll explain it. I'll start with the key word for then inside a pair of parentheses I'll set a data type char and a variable name. Then a colon and then the name of the array I want to work with chars2. Then I'll add a code block and within the code block I'll output the current character. So let's look at this syntax. Within the parentheses I'm saying for each character in the character's array.
The character I'm currently working with on each iteration of the loop will have a data type of char and a name of c and I'll keep on looping as long as there are items available in the array. Within the array I can then operate on just that one character. That one primitive value. And here is the result. At the end of my output I'm outputting one character per line outputting the word Hello vertically. So Strings again can be created using simple literal syntax or by using more complex constructor method syntax.
Either way it's an instance of the String class. And you have access in the String class to all the instance methods that are members of this class. You can find out all about that class and its members by looking at the Java API documentation. I'll place the cursor in the name of the class and press Shift F1. And that takes me to the documentation. Then I'll click on the METHOD link and I'll see all the different methods that are available from the String class.
- 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