Thursday, December 24, 2015

equals() Versus ==

equals() Versus ==

We should be clear that equals() method and == operator perform two different operations.The equals() method compares the character inside a string whereas == operator compares two object reference to see whether they refer to the same instances.Here is a small program to demonstrate the difference between equals()method vs == operator:


package stringdemo;


public class Equal {
    public static void main(String[] args) {
        String s1="john";
        String s2=new String(s1);
       
        System.out.println(s1.equals(s2));
        System.out.println(s1==s2);
    }
}
The above program gives the following output:
true
false

Related Posts:

  • Java program to show number pyramid Number Pyramid: package diamond; public class PyramidDemo {     public static void main(String[] args) {         for… Read More
  • Character Extraction Character Extraction: 1) charAt() charAt() method is used to extract single character from String.Here is a small program to demonstrate chatAt() … Read More
  • replace() method Modifying a String: replace();     The replace method is used to replace the one character of string.It has the general form:   &nbs… Read More
  • Fibonacci Using Recusion method Fibonacci Using Recusion method: package diamond; import static diamond.Fibonacci.fibonacciRecusion; public class FibonacciDemo {     pu… Read More
  • Character Extraction Character Extraction: 2)getChars() If you want to extract more than one character at a time,you can use the getChars() method. It has general form… Read More

0 comments:

Post a Comment