Comparing Strings with equals and compareTo methods - Java Language Basics

Java examples for Language Basics:String

Description

Comparing Strings with equals and compareTo methods

Demo Code

public class Main {
  public static void main(String[] args) {
    String apple = new String("Apple") ;
    String orange = new String("Orange") ;
    //from  w ww  .ja va2 s  . c  om
    System.out.println(apple.equals(orange));
    System.out.println(apple.equals(apple));
    System.out.println(apple == apple);
    System.out.println(apple == orange);
    System.out.println(apple.compareTo(apple));
    System.out.println(apple.compareTo(orange));
  }
}

Result


Related Tutorials