Search Java Programs

Showing posts with label equals(). Show all posts
Showing posts with label equals(). Show all posts

Wednesday, January 20, 2010

Java String equals Example

String class equals method example


String class equals method example:- This method demonstrates the working of equals() method. it returns boolean values true and false after examining the two objects taken under checking process

Syntax:-equals(Object anObject)

Here is the code:-

/**
* @(#) EqualsString.java
* EqualsStringString class demonstrates the working of equals() method of String class of lang package
* @version 15-May-2008
* @author Rose India Team
*/





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

Integer in = new Integer(12234);
String str = new String("12234");

// since both objects posses same value but the type of value is
// different. Here there are two types of objects compared one is
// Integer type and the other one is String type so the method returns
// false as values types didnot match

System.out.println( "value types don't matches: " + in.equals(str));


// method returns true if and only if none of the Strings compared
// posses null value and also must that there value matches each other
// in sequence, cases and all aspects of equality including value types
String str1 = new String(" hare krishna hare rama! rama rama hare hare");
String str2 = str1.substring(0);
String str3 = " ", str4 = " ";
String str5 = "heram", str6 = "Heram";
//Method returns true here as both Strings str1 and str2 posses same values
System.out.println(" 'true' is returned: " + str1.equals(str2));
//Both the String mentioned above don't have any value but still Strings are equal as both are null type objects compared"
System.out.println("Method returns true here: " + str3.equals(str4));
//Method here returns false because values of Strings did not match when their respective letters case are concerned
System.out.println(" 'false' is returned: " + str5.equals(str6));

}
}
Output of the program:-
value types don't matches: false

'true' is returned: true

Method returns true here: true

'false' is returned: false

Compare string example - Equals()

In this section, you will learn how to compare two strings in java. The java lang package provides a method to compare two with their case either upper and lower. The equals() method provides the facility of comparing the two strings. The following program uses the equals() method and helps you to compare the two strings. If both strings are equal, it will display a message "The given strings are equal" otherwise it will show "The given string are not equal".

Description of code:

equals():
This is the method that compares an object values and returns Boolean type value either 'true' or 'false'. If it returns 'true' for the both objects, it will be equal otherwise not.

Here is the code of program:

import java.lang.*;
import java.io.*;

public class CompString{
public static void main(String[] args) throws IOException{
System.out.println("String equals or not example!");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter first string:");
String str1 = bf.readLine();
System.out.println("Please enter second string:");
String str2 = bf.readLine();
if (str1.equals(str2)){
System.out.println("The given string is equals");
}
else{
System.out.println("The given string is not equals");
}
}
}

Output of program:

Both are equals:

C:\vinod\Math_package>javac CompString.java

C:\vinod\Math_package>java CompString
String equals or not example!
Please enter first string:
Rose
Please enter second string:
Rose
The given string is equals

Both are not equals:

C:\vinod\Math_package>java CompString
String equals or not example!
Please enter first string:
Rose
Please enter second string:
rose
The given string is not equals

Website Design by Mayuri Multimedia