Java Strings (M 27 & 28)
Java Strings (M 27 & 28)
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Java String
In Java, a string is a sequence of characters. For example, "hello" is a string containing a
sequence of characters 'h', 'e', 'l', 'l', and 'o'.
// create a string
String type = "Java programming";
Here, we have created a string variable named type. The variable is initialized with the
string Java Programming.
Note: Strings in Java are not primitive types (like int, char, etc). Instead, all strings are
objects of a predefined class named String.
// create strings
String first = "Java";
String second = "Python";
String third = "JavaScript";
// print strings
System.out.println(first); // print Java
System.out.println(second); // print Python
System.out.println(third); // print JavaScript
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Java String Operations
class Main {
public static void main(String[] args) {
// create a string
String greet = "Hello! World";
System.out.println("String: " + greet);
class Main {
public static void main(String[] args) {
// create first string
String first = "Java ";
System.out.println("First String: " + first);
// create second
String second = "Programming";
System.out.println("Second String: " + second);
// join two strings
String joinedString = first.concat(second);
System.out.println("Joined String: " + joinedString);
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
3. Compare two Strings
In Java, we can make comparisons between two strings using the equals() method. For
example,
class Main {
public static void main(String[] args) {
// create 3 strings
String first = "java programming";
String second = "java programming";
String third = "python programming";
// compare first and second strings
boolean result1 = first.equals(second);
System.out.println("Strings first and second are equal: " + result1);
// compare first and third strings
boolean result2 = first.equals(third);
System.out.println("Strings first and third are equal: " + result2);
}
} This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Methods of Java String
Methods Description
substring() returns the substring of the string
replace() replaces the specified old character with the specified new character
charAt() returns the character present in the specified location
getBytes() converts the string to an array of bytes
indexOf() returns the position of the specified character in the string
compareTo() compares two strings in the dictionary order
trim() removes any leading and trailing whitespaces
format() returns a formatted string
split() breaks the string into an array of strings
toLowerCase() converts the string to lowercase
toUpperCase() converts the string to uppercase
valueOf() returns the string representation of the specified argument
toCharArray() converts the string to a char array
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Creating strings using the new keyword
Since strings in Java are objects, we can create strings using the new keyword as
well. For example,
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Example: Create Java Strings using the new keyword
class Main {
public static void main(String[] args) {
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Create String using literals vs new keyword
In Java, the JVM maintains a string pool to store all of its strings inside the memory. The string
pool helps in reusing the strings.
If the string already exists, the new string is not created. Instead, the new reference, example
points to the already existed string (Java).
If the string doesn't exist, the new string (Java is created.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
• Write a Program to copy a string.
• Write a Program to reverse a string.
• Write a Program to concatenate a string.
• Write a Program to print the length of a string.
• Write a Program to compare two strings.