Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
The string class is one of the most commonly used complex types in Java. I call it a complex type, because when you create a string, you're creating an instance of a class. It's not a primitive variable, such as char, int, short and the others. I'm working in a project called strings that's part of the Chapter 6 Exercise Files. And the beginning class just has an empty main method. I'll start by declaring a string named s1 and I'll give it a value of "Welcome to California!" When you declare a literal string in Java, you must wrap the string in double quotes; single quotes are only used for character types.
Double quotes are always used for strings. Now I'll output the value to the console using System.out.println(s1). I'll run the application and predictably I get the output "Welcome to California!" Now you can declare strings in one of two ways. This syntax where you assign a literal string directly to the equals assignment operator is actually a shortcut. Because the string class is a complex class, you are actually creating an instance of the class. And you can also use the more conventional instantiation syntax.
It would look like this, I'll declare a second string named s2, I'll use the new keyword, I'll call the constructor method for the string class. A constructor method is a method of a class that has the same name as the class itself, and then I'm going to pass in exactly the same value to the constructor method. I'll select that text including the quotes and paste it between the parentheses and then add the semicolon at the end. And I'll change the variable that I'm outputting from s1 to s2, I'll Save and Run and the output is exactly the same.
You can use either syntax style to create a string, they are functionally equivalent. Now I mentioned in an earlier video that if you want to compare two string values, you shouldn't use the double equals operator. It's unpredictable. There are some circumstances in which you can compare s1 and s2, and if they have the same value, you'll get a true value back and there are some situations where you won't. This is a situation where you won't. If you declare one string using the shorthand syntax with the equals assignment operator and just a literal string, and the second string using the constructor method, then you can't compare the values using double equals.
It's a strange situation, but I want to show you the result. I'll place the cursor at the end of the existing code and I'll create a conditional block. I'll type if and press Ctrl+Space, choose the if statement, and then I'll set my condition as s1 has a value of s2, using the double equals operator. And then in the conditional block, I'll use System.out.println and I'll output the string, ("They match!"). I'll add an else clause and I'll make a copy of this println command, paste it into the else clause and change that to ("They don't match!").
I'll Run the application and I get back a negative result even though those two values look like they match pretty closely. So here's what's going on, the s1 and s2 objects are different objects. Even though they have the same string value, you can't reliably use the double equals operator this way. So instead, you should use a method called equals. The string class has a very deprogramming interface and the equals method is one of its most useful methods. Let's take a look at the documentation for the string class, I'll double-click the data type string, go to Dynamic Help on the Help menu, click the link for the class Java.lang.string and maximize the help view.
I'll click the Method link and scroll down to the es, and show you that there are two versions of the equals method. The equals method is case sensitive. It compares the string to the string of another object. There is also equals ignore case, so you can do a non-case sensitive comparison. So I'll close the Help screen, and I'm going to change my syntax so that instead of using the double equals operator, I'll use the equals method. If (s1.equals(s2)). I'll save and run the application and now I get They Match! Now for non-case sensitive comparisons, use the equals no case method.
I'll go to the second string and change the welcome word to uppercase. I'll run the application and I get They don't match! because the equals method is case- sensitive, but then I'll come down to the equals method and change it to equalsIgnoreCase I'll fix up the code so it's syntactically correct and I'll run the application again, and now I get They match! Finally, I'll show you one more method. I've shown previously that a string contains an array of characters and you can extract that array of characters and then loop through it one character at a time.
I've moved the cursor below the conditional code, I'll declare variable with the data type of char, open bracket, closed bracket—- that's an array of the primitive data type char-- and I'll name it chars and then I'll call the method toCharArray. So it looks like this, s1.toCharArray(); So now I have an array of characters, then I'll loop through the array and I'd put one character at a time. I'll type for and press Ctrl+Space and I'll chose the for-each iteration. For each variable which is going to be data typed as char with the name of c in the char's array, I'll use System.out. println and I'll output the value of c, the character in that position.
I'll run the application and here is the result. Now I'm outputting the string one character at a time, one line at a time. Take a look at the rest of the documentation for the string class. You'll find all sorts of useful methods that allow you to compare, set, extract and otherwise manipulate string values.
There are currently no FAQs about Java Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.