Example for compareTo() method� 

Example for compareTo() method� 


Posted in : Core Java Posted on : October 1, 2010 at 1:04 PM Comments : [ 0 ]

In this tutorial, We will discuss about the compareTo() method of the Java character class.

Example for compareTo() method 

In this tutorial, We will discuss about the compareTo() method of the Java character class. This method compares two character objects according to code value and returns numerical value. This method returns positive value if the non argument character is greater than argument character, and returns negative argument character is greater than the non argument character. returns 0 if both the characters are same numerically.

Example:ChartacterCompareTo.java

package com.devmanuals;

public class CharacterCompareTo {

	public static void main(String[] args) {
		Character ch1 = new Character('a');
		Character ch2 = new Character('A');
		Character ch3 = new Character('a');
		System.out.println(ch1.compareTo(ch2));
		System.out.println(ch1.compareTo(ch3));
		System.out.println(ch2.compareTo(ch3));
	}

}

Output:

32

0

-32

Download this Example

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics