Found 9321 Articles for Object Oriented Programming

Difference between == and equals() method in Java.

Alankritha Ammu
Updated on 26-Feb-2020 06:42:24

2K+ Views

The equals() 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.Example Live Demopublic class Sample {    public static void main(String []args) {       String s1 = "tutorialspoint";       String s2 = "tutorialspoint";       String s3 = new String ("Tutorials Point");       System.out.println(s1.equals(s2));       System.out.println(s2.equals(s3));    } }Outputtrue falseYou can also compare two strings using == operator. But, it compares references of the given ... Read More

How do I compare strings in Java?

Kumar Varma
Updated on 26-Feb-2020 07:09:39

245 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

Why StringBuffer is mutable in Java?

Arjun Thakur
Updated on 26-Feb-2020 06:01:26

1K+ 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.

What is the difference between equals and compareTo in Java?

Rama Giri
Updated on 18-Jun-2020 07:22:14

2K+ 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

How to check if two strings are equal in Java?

Anjana
Updated on 26-Feb-2020 06:39:51

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

How to use equals() and equalsIgnoreCase() in Java.

Manikanth Mani
Updated on 26-Feb-2020 06:37:53

283 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

What does the compareTo do in Java?

Arjun Thakur
Updated on 26-Feb-2020 07:07:03

117 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

How to compare strings in Java?

Fendadis John
Updated on 26-Feb-2020 08:11:55

282 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

How to compare String equality in Java?

Ayyan
Updated on 26-Feb-2020 06:30:47

235 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

Java String comparison, differences between ==, equals, matches, compareTo().

Akshaya Akki
Updated on 26-Feb-2020 06:28:55

541 Views

The equals() 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.Examplepublic class Sample{    public static void main(String []args){       String s1 = "tutorialspoint";       String s2 = "tutorialspoint";       String s3 = new String ("Tutorials Point");       System.out.println(s1.equals(s2));       System.out.println(s2.equals(s3));    } }Outputtrue falseYou can also compare two strings using == operator. But, it compares references of the given variables not values.Examplepublic class ... Read More

Advertisements