Pages

String Handling Interview Questions in Java



String Handling Interview Questions in Java

Hello this evening Herry is here available with new concept of Java programming, Today we discuss String Handling Interview Questions in Java; Our main focus about difference between equal(), == Operator and compareTo() method.

String Handling Interview Questions in Java

What is an Immutable object?


An object whose state cannot be changed after it is created is known as an Immutable object. String, Integer, Byte, Short, Float, Double and all other wrapper class's objects are immutable.

What is equals() methods ?

equals() method are used to compares two strings for equality.

Syntax

boolean equals (Object str)
It compares the content of the strings. It will return true if string matches, else returns false.

String s = "Hell";
String s1 = "Hello";
String s2 = "Hello";
s1.equals(s2);    //true
s.equals(s1) ;   //false

What is == Operator ?

== Operator compares two object references to check whether they refer to same instance. This also, will return true on successful match.

String s1 = "Java";
String s2 = "Java";
String s3 = new string ("Java");
test(Sl == s2)     //true
test(s1 == s3)      //false

What is compareTo() method ?

compareTo() method compares values and returns an int which tells if the string compared is less than, equal to or greater than th other string. Its general syntax is,

int compareTo(String str)


No comments:

Post a Comment

 

Most Reading