
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

783 Views
You can compare two Strings in Java using the compareTo() method, equals() method or == operator.The compareTo() method compares two strings. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string.The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would ... Read More

2K+ Views
We all know that the String class in Java is mutable i.e. once we create a String variable we cannot modify its data or do any manipulations.But, there may be scenarios where we need to modify the data of String variables. In such cases, we could use StringBuffer class.This class −is like a String, but can be modified. It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.Is safe for use by multiple threads.

3K+ Views
The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would return true.ExampleLive Demopublic class StringDemo { public static void main(String[] args) { ... Read More

15K+ Views
You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Exampleimport java.lang.* public class StringDemo { public static void main(String[] args) { String str1 = "Tutorialspoint"; String str2 = "Tutorialspoint"; String str3 = "Hi"; // checking for equality boolean retval1 = ... Read More

359 Views
The equals() methodThis method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Exampleimport java.lang.*; public class StringDemo { public static void main(String[] args) { String str1 = "sachin tendulkar"; String str2 = "amrood admin"; String str3 = "amrood admin"; // checking for equality boolean retval1 = str2.equals(str1); boolean retval2 = str2.equals(str3); ... Read More

201 Views
The compareTo() method in Java compares two strings lexicographically.ExampleLive Demopublic class Test { public static void main(String args[]) { String str1 = "Strings are immutable"; String str2 = new String("Strings are immutable"); String str3 = new String("Integers are not immutable"); int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); } }Output0 10

794 Views
You can compare two Strings in Java using the compareTo() method, equals() method or == operator.The compareTo() method compares two strings. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.The result is a negative integer if this String object lexicographically precedes the argument string.The result is a positive integer if this String object lexicographically follows the argument string.The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would ... Read More

347 Views
You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Exampleimport java.lang.* public class StringDemo { public static void main(String[] args) { String str1 = "Tutorialspoint"; String str2 = "Tutorialspoint"; String str3 = "Hi"; // checking for equality boolean retval1 = ... Read More

925 Views
When working with strings in Java, one of the most common operations is comparing strings to check for equality or ordering. Java provides several methods to perform string comparison: equals(), matches(), and compareTo(). While all of them are used to compare strings, each method serves a different purpose and behaves differently. Different approaches Following are the different approaches for string comparison − equals() Method matches() Method compareTo() MethodWhen to Use Each MethodUse equals() when you need to check if two strings are identical in content.Use matches() when you ... Read More

2K+ Views
The equalsIgnoreCase() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.ExampleLive Demopublic class Sample { public static void main(String args[]) { String Str1 = new String("This is really not immutable!!"); String Str2 = Str1; String Str3 = new String("THIS IS REALLY NOT IMMUTABLE!!"); boolean retVal; retVal = Str1.equalsIgnoreCase(Str2); System.out.println("Returned Value = " + retVal ); retVal = Str1.equalsIgnoreCase( Str3 ); System.out.println("Returned Value = " + retVal ); } }OutputReturned Value = true Returned Value = true