0% found this document useful (0 votes)
24 views11 pages

Java Strings (M 27 & 28)

Here are programs to perform common string operations in Java: 1. Copy a string: ```java public class Main { public static void main(String[] args) { String str = "Hello"; String copy = str; System.out.println(copy); } } ``` 2. Reverse a string: ```java public class Main { public static void main(String[] args) { String str = "Hello"; String reverse = ""; for(int i = str.length() - 1; i >= 0; i--) reverse += str.charAt(i); System.out.println(reverse); }

Uploaded by

Asif Shaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

Java Strings (M 27 & 28)

Here are programs to perform common string operations in Java: 1. Copy a string: ```java public class Main { public static void main(String[] args) { String str = "Hello"; String copy = str; System.out.println(copy); } } ``` 2. Reverse a string: ```java public class Main { public static void main(String[] args) { String str = "Hello"; String reverse = ""; for(int i = str.length() - 1; i >= 0; i--) reverse += str.charAt(i); System.out.println(reverse); }

Uploaded by

Asif Shaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Strings

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'.

We use double quotes to represent a string in Java. For example,

// 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.

And, all string variables are instances of the String class.


This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
class Main {
public static void main(String[] args) {

// 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

1. Get Length of a String


To find the length of a string, we use the length() method of the String. For example,

class Main {
public static void main(String[] args) {

// create a string
String greet = "Hello! World";
System.out.println("String: " + greet);

// get the length of greet


int length = greet.length();
System.out.println("Length: " + length);
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
2. Join two Strings
We can join two strings in Java using the concat() method. For example,

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,

// create a string using the new keyword


String name = new String("Java String");
In the above example, we have created a string name using the new keyword.

Here, when we create a string object, the String() constructor is invoked.

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) {

// create a string using new


String name = new String("Java String");

System.out.println(name); // print Java String


}
}

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.

1. While creating strings using string literals,


String example = "Java";
Here, we are directly providing the value of the string (Java). Hence, the compiler first checks the
string pool to see if the string already exists.

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.

2. While creating strings using the new keyword,

String example = new String("Java");


Here, the value of the string is not directly provided. Hence, the new string is created all the time.

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.

You might also like