Java Long compareTo Example

Long class compareTo method example.


Long class compareTo method example. This example shows you how to use compareTo method. This method compares the pair of values of Long, String, and other wrapper classes objects values with each other and returns 0 if true

Syntax: compareTo(Long anotherLong)

Here is the code:-
/**
 * @(#) CompareToLong.java
*   CompareToLong class demonstrates the working of
compareTo() method of Long class of lang package
 @version 9-May-2008
 @author Rose India Team
 */

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

    // two long objects initialized
    Long x = new Long(12);
    Long y = new Long(21);

    // method checks the numerical values of two objects
    if (x.compareTo(y== 0) {
      System.out.println("both long objects are equal");
    }

    else if (x.compareTo(y!= 0) {
      System.out.println("long objects are not equal");
    }

    // here method checks just the value hold by the String objects not how
    // the the values are assigned
    String m = "this is my" " first page";
    String n = "this is my first page";

    if (m.compareTo(n== 0) {
      System.out.println("both Strings are equal");
    else if (m.compareTo(n!= 0) {
      System.out.println("Strings are not equal");
    }

  }
}
  

null

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US