Method Of Comparing Two Numbers 

Java Exercise : This is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one. 

First of all, name a class "Comparing" and take two numbers in this class. 

Here we have taken a=24 and b=25, now we have to find out whether a=b, a>b or b>a. 

To find out this apply if and else condition one by one. Now apply the condition "if (a=b)", if this satisfies then type that both are equal in the system class. If this doesn't satisfy, then check whether a>b by applying the "else if" condition and type the message "a is greater than b" in the system class. Again this doesn't satisfy then 'else' condition as shown in the example will show that b is greater than a. 

Now compile and run the program and you will find the desired output. If you are getting any error then check the whole program thoroughly and surely you will get correct result. 

By compiling and running this exact program, you will find that b is greater than a.

class  Comparing{
  public static void main(String[] args) {
    int a=24, b=25;
    if (a == b){
      System.out.println("Both are equal");
    else if(a>b){
      System.out.println("a is greater than b");
      System.out.println("b is greater than a");

Have Java Problem
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.