Related Topics

JAVA Programming
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
boolean result1 = str1.equals(str2); // true
boolean result2 = str1.equals(str3); // true
In this example, we create three strings: str1
, str2
, and str3
. We initialize str1
and str2
with the same value (“Hello”), and we create str3
using the new
keyword, which creates a new instance of a String
object with the same value as “Hello”.
We then use the equals()
method to compare str1
and str2
, which should return true
since they have the same value. We also compare str1
and str3
, which should also return true
since they have the same value.
The equals()
method checks the contents of the two strings to determine if they are equal, whereas the ==
operator checks to see if the two strings are the same object in memory. Here’s an example to illustrate the difference:
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
boolean result1 = (str1 == str2); // true
boolean result2 = (str1 == str3); // false
In this example, we create the same three strings as before. We use the ==
operator to compare str1
and str2
, which should return true
since they are both string literals and therefore refer to the same object in memory. We also compare str1
and str3
, which should return false
since they are different objects in memory, even though they have the same value.
In general, it is recommended to use the equals()
method when comparing the contents of two strings, rather than the ==
operator, since it is more reliable and accurate.
String str1 = "Hello";
String str2 = "hello";
boolean result = str1.equalsIgnoreCase(str2); // true
In this example, we create two strings str1
and str2
. We then use the equalsIgnoreCase()
method to compare the two strings. Since the method ignores the case of the strings, the comparison returns true
.
The equalsIgnoreCase()
method is useful when you need to compare two strings for equality, but you don’t care about differences in case. This can be particularly useful when comparing user input, since users may enter input in different cases.
If you want to compare two strings while taking their case sensitivity into account, you can use the equals()
method. For example:
String str1 = "Hello";
String str2 = "hello";
boolean result = str1.equals(str2); // false
In this example, we use the equals()
method to compare the two strings. Since the method is case sensitive, the comparison returns false
.
In general, it’s a good practice to use the equalsIgnoreCase()
method when comparing strings if you don’t need to consider case sensitivity, and to use the equals()
method if you do need to consider it.
String str1 = "apple";
String str2 = "banana";
String str3 = "apple";
int result1 = str1.compareTo(str2); // -1
int result2 = str2.compareTo(str1); // 1
int result3 = str1.compareTo(str3); // 0
In this example, we create three strings: str1
, str2
, and str3
. We then use the compareTo()
method to compare the strings in various combinations.
The return value of compareTo()
is an integer that indicates the result of the comparison. If the first string is lexicographically less than the second string, the method returns a negative integer. If the first string is lexicographically greater than the second string, the method returns a positive integer. If the two strings are equal, the method returns 0.
In the first comparison (str1.compareTo(str2)
), the method returns -1
since “apple” is lexicographically less than “banana”. In the second comparison (str2.compareTo(str1)
), the method returns 1
since “banana” is lexicographically greater than “apple”. In the third comparison (str1.compareTo(str3)
), the method returns 0
since both strings are equal.
The compareTo()
method is useful for sorting strings in lexicographic order, such as in alphabetical order.
String str = "Hello, World!";
int length = str.length();
System.out.println(length); // 13
In this example, we create a string str
that contains the text “Hello, World!”. We then use the length()
method to get the length of the string, which is 13. Finally, we print the length of the string using the println()
method.
The length()
method is useful when you need to find the number of characters in a string. For example, you might use it to ensure that a user’s input is within a certain length limit.
It’s important to note that the length
property is not a method, but rather a field that is included in Java’s String
class. To get the length of a string, you can use either the length()
method or the length
field.
String str = "Hello, World!";
char ch = str.charAt(7);
System.out.println(ch); // W
In this example, we create a string str
that contains the text “Hello, World!”. We then use the charAt()
method to retrieve the character at index 7, which corresponds to the letter “W” in the string. Finally, we print the character using the println()
method.
The charAt()
method is useful when you need to access individual characters in a string. For example, you might use it to extract the first letter of a person’s name, or to process each character in a string one by one.
It’s important to note that the charAt()
method is 0-indexed, meaning that the first character in a string has an index of 0, the second character has an index of 1, and so on. If you try to access an index that is outside the range of the string, the method will throw an IndexOutOfBoundsException
exception.
String str = "Hello, World!";
String subStr = str.substring(7, 12);
System.out.println(subStr); // World
In this example, we create a string str
that contains the text “Hello, World!”. We then use the substring()
method to retrieve a substring that starts at index 7 (corresponding to the letter “W”) and ends at index 12 (corresponding to the letter “d”). The resulting substring is “World”. Finally, we print the substring using the println()
method.
It’s important to note that the ending index of the substring is exclusive, meaning that the character at the ending index is not included in the resulting substring. Also, if you omit the second parameter, the method will return a substring that includes all the characters from the starting index to the end of the string.
The subSequence()
method is similar to substring()
, but it takes two parameters of type int
which represent the start and end indexes of the substring. The method returns a CharSequence
object, which can be a String
, a StringBuilder
, or a StringBuffer
. The subSequence()
method is used when you want to manipulate a portion of a string as a CharSequence
object.
Here’s an example of using the subSequence()
method:
String str = "Hello, World!";
CharSequence subSeq = str.subSequence(7, 12);
System.out.println(subSeq); // World
In this example, we create a string str
that contains the text “Hello, World!”. We then use the subSequence()
method to retrieve a CharSequence
that starts at index 7 (corresponding to the letter “W”) and ends at index 12 (corresponding to the letter “d”). The resulting CharSequence
is “World”. Finally, we print the CharSequence
using the println()
method.
It’s important to note that while substring()
returns a String
object, subSequence()
returns a CharSequence
object. If you want to convert the CharSequence
object back to a String
, you can use the toString()
method.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.