Friday 19 February 2016

Arithmetic Operators in Java - Live practical examples : Part 1

In today's post we'll cover the topic of arithmetic operators in Java with some real live examples. In this post, we'll skip the theory and go straightaway for some practical examples. With these examples, you will get to grasp and understand the operator functions much quicker than ever.

Check out the following code in which we have used multiple combinations of arithmetic operators in different positions.

package com.ironcladzone;

public class operator1 {

public static void main(String[] args) {
System.out.println("Example 1 :" + 3 + 4 + 5);
System.out.println("Example 2 : " + 3 + 4 * 5);
System.out.println("Example 3 : " + 3 + 12 * 10);
System.out.println("Example 4 : " + 3 * 4 + 5);
System.out.println("Example 5 : " + 3 * 4 / 5);
System.out.println("Example 6 : " + 5 / 4 * 5);
System.out.println("Example 7 : " + 10 / 4 + 5);
System.out.println("Example 8 : " + 10 + 4 / 5);
System.out.println("Example 9 : " + 10 + 15 / 5);
System.out.println("Example 10 : " + 125 / 10 / 5);
System.out.println("Example 11 : " + 50 / 10 / 5);
System.out.println("Example 12 : " + 23 / 10 / 5);
System.out.println("Example 13 : " + 23 / 10 / 5 + 4);
System.out.println("Example 14 : " + 3 + 4 + 5 / 7);
System.out.println("Example 15 : " + 3 + 4 + 5 * 2);
System.out.println("Example 16 : " + 3 + 5 / 5 * 2);
System.out.println("Example 17 : " + 3 + 22 / 5 * 2);
System.out.println("Example 18 : " + 3 + 7 / 5 * 2);
System.out.println("Example 19 : " + 2 + 8 / 5 * 2);
System.out.println("Example 20 : " + 3 * 9 / 5 + 2.59);

}


}

Output : 

Example 1 :345
Example 2 : 320
Example 3 : 3120
Example 4 : 125
Example 5 : 2
Example 6 : 5
Example 7 : 25
Example 8 : 100
Example 9 : 103
Example 10 : 2
Example 11 : 1
Example 12 : 0
Example 13 : 04
Example 14 : 340
Example 15 : 3410
Example 16 : 32
Example 17 : 38
Example 18 : 32
Example 19 : 22
Example 20 : 52.59

If you observe closely, you may find a few of the combinations are tricky. For instance, note that the + sign we have used, is for string concatenation and not to be confused with the plus sign used for addition.

This was just the first set of live examples. Another exhaustive post on more examples is on its way. Stay tuned folks. Am also quite sure this code will help you cover the basic operator concepts if you're planning to give the Oracle Java Certification 1Z0-803. Good luck guys. Signing out.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker