Java strings
A string is a grouping of characters sorrounded by double quotes such as "This is a string". Every such entity is part of the String class.
This lesson focuses on:
- Declaring a string
- Concatenation
- Methods of the String class
Declaring a string
A String can be declared as a variable.
Syntax:
String nameOfString = "stringValue";
Example:
String aString = "This is a string";
or with the new keyword:
Syntax:
String nameOfString = new String("stringValue");
Example:
String aString = new String("This is a string");
Whether you declare a string as a variable or with the new keyword, it becomes an instance of the String class.
Concatenation
Concatenation is the process by which two or more strings are joined. This is ahieved with the use of the + operator. You can use concatenation to join two or more strings into one or print two or more strings together.
Example:
class JoinStrings{
public static void main(String[] args){
//declare two strings
String aString = "Here is some text. ";
String anotherString = "Here is some more text.";
//declare a third string and
//combine into it the first two strings
String combinedString = aString + anotherString;
//print the value of combinedString
System.out.println(combinedString);
//print two strings together
System.out.println("Notepad is a "
+ "simple text editor");
}
}
Output:
Here is some text. Here is some more text.
Notepad is a simple text editor
In the above example, three strings are declared. The first string is named aString and it is initialized with the value "Here is some text. ". The second string is named anotherString and it is initialized with the value "Here is some more text.". The third string is named combinedString - it concatenates the values stored in aString and anotherString. The value of combinedString will be "Here is some text. Here is some more text." and it will be printed using the System.out.println() statement. The System.out.println() statement is then used again to print the combination of the two strings "Notepad is a " and "simple text editor"
Methods of the String class
The String class has various methods you can use to work with text.
Methods of the String class:
-
charAt(int index)
Returns a character from a string at a specified index.
-
equals()
Compares two strings and returns true if they are equal.
-
length()
Returns the length of a string.
-
toUpperCase()
Converts all letters in a string to upper case.
Example:
class StringMethods{
public static void main(String[] args){
//declare two strings
String aString = "Here is some text";
String anotherString = "Here is some more text";
//extract the third character
//from the aString string and print it
System.out.println(aString.charAt(2));
//compare the two strings aString and anotherString
//and return true or false based on if they are the
//same or not
System.out.println("The two strings match: "
+ aString.equals(anotherString));
//print the length of the aString string
System.out.println("The length of the aString
string variable is " + aString.length());
//print the anotherString
//string in all uppercase letters
System.out.println(aString.toUpperCase());
}
}
Output:
r
The two strings match: False
The length of the aString string variable is 17
HERE IS SOME MORE TEXT
In the above example, two strings (aString and anotherString) are declared. The charAt() method is used to return the third character from the aString string. The equals() method is used to test if the two strings are equal and return a true or false value accordingly. The length() method is used to return the length of the aString string. The toUpperCase() method is used to print the anotherString string in all uppercase letters.




