Java String equals

The java string equals()method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.
The String equals() method overrides the equals() method of Object class.

Signature

public boolean equals(Object anotherObject)

Parameter

anotherObject : another object i.e. compared with this string.

Returns

true if characters of both strings are equal otherwise false.

Overrides

equals() method of java Object class.

Java String equals() method example

public class EqualsExample{  
public static void main(String args[]){  
String s1="catchmecoder";  
String s2="catchmecoder";  
String s3="CATCHMECODER";  
String s4="java";  
System.out.println(s1.equals(s2)); //true because content and case is same  
System.out.println(s1.equals(s3)); //false because case is not same  
System.out.println(s1.equals(s4)); //false because content is not same  
}}     

Output:

true
false
false

Java String format

The java string format() method returns the formatted string by given locale, format and arguments.
If you don't specify the locale in String.format() method, it uses default locale by calling Locale.getDefault() method.
The format() method of java language is like sprintf() function in c language and printf() method of java language.

Signature

There are two type of string format() method:
public static String format(String format, Object... args)  
and,  
public static String format(Locale locale, String format, Object... args) 

Parameters

locale : specifies the locale to be applied on the format() method. format : format of the string. args : arguments for the format string. It may be zero or more.

Returns

formatted string

Throws

NullPointerException : if format is null. IllegalFormatException : if format is illegal or incompatible.

Java String format() method example

public class FormatExample{  
public static void main(String args[]){  
String name="pramesh";  
String sf1=String.format("name is %s",name);  
String sf2=String.format("value is %f",42.4454);  
String sf3=String.format("value is %42.12f",42.33434);   //returns 12 char fractional part filling with 0  
System.out.println(sf1);  
System.out.println(sf2);  
System.out.println(sf3);  
}}     

Output:

name is pramesh
value is 42.445400
value is 42.334340000000