A String is a data type that can hold one or more ASCII characters.
(it is possible to store no characters at all in a string - this is called the null string "")
The Java development environment provides two classes that store and manipulate data that deals with multiple characters (String
variables):
In Java, there are String variables and String Literals.
String Literals are Strings that are constant (they do not change) and are not stored in a variable. String literals are used all the time in methods:
For example ...
System.out.println("hello");
String variables are variables that can hold one or more ASCII characters (or none). String variables are used to store data that is non-numeric (you can not do math operations on that data). Like every other variable in Java you must create the String variable before you are able to use it.
For example ...
String fname; // creates a String variable called name
fname = "Annie Buddy"; // sets the value of the String variable
System.out.println("hello " + fname); // prints out "hello Annie Buddy"
To store data in a String, that data must come from ...
Assume that the Strings S1, S2, S3, and S4exist
(ie: String S1, S2, S3, S4; )
String variables can be joined together (using the + operator) to form longer Strings. For example ...
String name, first, space, last; // creates String variables
first =Annie"; // sets the value of variable to "Annie"
last = "Buddy"; // sets the value of variable to "Buddy"
space = " "; // sets the value of variable to " " (a space)
name = first + space + last; // joins Strings and stores result in name
System.out.println("hello " + name);
// prints out the phrase "hello Annie Buddy"
Comparing String values in Java is different than comparing the values that are stored in the primitive Java data types .
The "equals( )" method can be used to compare data stored in the current string to the data stored in another string. This method returns the value true
if they are identical and the value false if they are not. The results of the "equals( )" function can be used in an if statement to compare two strings to see if they are equal.
Here is an example of how to compare two strings
(the variable password is compared to the string "ICS3M" )
String password;
System.out.println("("Enter your password");
password = s.next();
if (password.equals("ICS3U")) {
System.out.println("Right on ");
}
else {
System.out.println("No try again");
}
Alternatively you can use the "compareTo( )" method to compare data stored in the current string variable to another String.
The "compareTo( )" method compares the two strings and returns back a number that relates to how different the two Strings are. If the "compareTo( )" method returns the value 0 , then the two Strings are equivalent, and any other non-zero value indicates that the two Strings are not equivalent.
This example is the code above modified to use the "compareTo( )" method rather than the
"equals( )" method
String password;
System.out.println("Enter your password");
password = s.next();
if (password.compareTo("ICS3U") == 0) {
System.out.println("Right on ");
}
else {
System.out.println("No try again");
}