Join Peggy Fisher for an in-depth discussion in this video Using strings, part of Learning Java (2015).
- Strings are technically not a primitive data type. But since it is used so often, it is sometimes included when discussing primitive data types. String variables are stored as an array or a sequence of characters. Strings can be a literal constant, such as the sentence Hello World. Note that strings are enclosed in double quotes, while characters are enclosed in single quotes. They can be defined by using the string data type, which is again, technically a sequence of characters.
Two or more strings can be added together using concatenation. So you can combine the strings, Hello and World to create one string Hello World. It is also possible to break a string up into smaller pieces using the substring methods. A substring can be a single character, the entire string, or some portion of the string. We can obtain the length of any string using a method called length. We will discuss these methods shortly. Let's revisit our program to calculate the cost of a trip.
This time, we will modify the program to ask the user to enter the values we need, but also to include the type of vehicle, such as convertible, sedan, coupe, jeep, etc. We will then run the program to see our results. I'm gonna start by adding a string variable. String must be declared with a capital S. I'll call this vehicleType. At the very end, I'm gonna ask the user what type of vehicle it is. So I'm gonna add System.out.printlne, Please enter the vehicle type.
I read a string in almost the same way almost the same way that I read in numbers. vehicleType is equal to, the only difference is, instead of in.nextDouble, I'm gonna just do in.next. This allows me to read a string. Now I wanna add this value that I just read to my output line that says The trip is going to cost, so much money. Let's go ahead and add, since you are driving, a. Now I need to concatenate the value vehicleType.
Ok, so we've added a string variable here in line 20, called vehicleType. Lines 28 and 29 we've asked the user to tell us what the type of vehicle it is. And in lines 32 and 33 I've added the vehicleType to my output. Let's run it and see what happens. Ok, we'll go ahead and continue our example of 500 miles, and we'll say that this is the hybrid car that gets 50 miles per gallon. The price is still 2.55, and I'm just going to go ahead and say hybrid as the type of vehicle.
And you can see it says, The trip is going to cost $25.50 since you are driving a hybrid. Notice that the a comes right after the g. We can fix that by going back to our program, and adding a space after the g in driving. So this is our first example of using a string along with numbers in our program.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs