Home Java Beginners Java examples for beginners
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java examples for beginners

Advertisement
Java examples for beginners: Java examples for beginners to compare between two numbers in Java. The basic comparing strings or numbers in Java example will help you to understand how actually a comparison can be performed in your application. Go through the article to find the code structure of basic Comparison method.

In this tutorial, you will be briefed about the word of Java examples, which help to understand functioning of different Java classes and way. It also provides you the techniques of programming in the simple way with the help of examples.

Java examples for beginners: Comparing Two Numbers

The tutorial provide you the simple form of examples of Java, which will highlight you the method of comparing two numbers and finding out greater one.

First of all, you will have to give the name a class "Comparing" and then take two numbers in this class. We have taken here two numbers like 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 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 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");
}
else{
System.out.println("b is greater than a");
}
}
}
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 13, 2013

Ask Questions?    Discuss: Java examples for beginners  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com