Java – Math and Arithmetic Operators in Java




Java Tutorial for Beginners
Java Tutorial for Beginners

The Java programming language supports various arithmetic operators for all floating point
and integer numbers. These operators are (+)(addition), (-)(subtraction),(*)
(multiplication), (/)(division), and (%)(modulo). The following table summarizes the
binary arithmetic operations in the Java programming language.
Operator Use Description

Operator Use Description
+ op1 + op2 Adds op1 and op2; also used to concatenate strings
op1 – op2 Subtracts op2 from op1
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing op1 by op2

So Let’s take an example to understand operators better

/*************************************************************************
 * Math and Arithmetic Operators in Java Example
 *
 *************************************************************************/

public class MyClass {
	public static void main(String[] args) {
		//a few numbers
		int i = 37;
		int j = 42;
		double x = 27.475;
		double y = 7.22;
		System.out.println("Variable values...");
		System.out.println(" i = " + i);
		System.out.println(" j = " + j);
		System.out.println(" x = " + x);
		System.out.println(" y = " + y);
		//adding numbers
		System.out.println("Adding...");
		System.out.println(" i + j = " + (i + j));
		System.out.println(" x + y = " + (x + y));
		//subtracting numbers
		System.out.println("Subtracting...");
		System.out.println(" i - j = " + (i - j));
		System.out.println(" x - y = " + (x - y));
		//multiplying numbers
		System.out.println("Multiplying...");
		System.out.println(" i * j = " + (i * j));
		System.out.println(" x * y = " + (x * y));
		//dividing numbers
		System.out.println("Dividing...");
		System.out.println(" i / j = " + (i / j));
		System.out.println(" x / y = " + (x / y));
		//computing the remainder resulting from dividing numbers
		System.out.println("Computing the remainder...");
		System.out.println(" i % j = " + (i % j));
		System.out.println(" x % y = " + (x % y));
		//mixing types
		System.out.println("Mixing types...");
		System.out.println(" j + y = " + (j + y));
		System.out.println(" i * x = " + (i * x));
	}
}

OUTPUT:

The output from this program is:
Variable values...
i = 37
j = 42
x = 27.475
y = 7.22
Adding...
i + j = 79
x + y = 34.695
Subtracting...
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37
Dividing...
i / j = 0
x / y = 3.8054
Computing the remainder...
i % j = 37
x % y = 5.815
Mixing types...
j + y = 49.22
i * x = 1016.58

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





1 Comment

  1. java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have java installed, and more are created every day.
    java was developed by Sun microsystem as an Object oriented language for general purpose business applications. The target of java is to write a program once and then run this program on multiple operating systems.The first publicly available version of java ( java 1.0) was released in ‘95. Sun Microsystems was acquired by the Oracle Corporation in 2k10. Oracle has now the steermanship for java . In 2k06 Sun started to make java accessible under the GNU General Public License (GPL). Oracle continued this plan called Open JDK.

    Over time new improved versions of java have been released. The present version of java is java 1.9 which is also known as java 9.
    java is defined by a specification and consists of a programming language, a compiler, core libraries and a runtime ( java virtual machine) The java runtime permit software developers to write program code in other languages than the java programming language which still runs on the java virtual machine. The java platform is usually associated with the java virtual machine and the java core libraries.
    The java syntax is similar to C++. java is case-sensitive, e.g., variables called myValue andmyvalue are treated as different variables.

    Basic topics Covered in java is
    INDEX
    Introduction of java

    JDK vs JVM vs JVM

    Java Data Types

    Java Operators

    Java Loop Control

    Java Decision Making

    Java Array

    Java String

    Class And Object

    This Keyword in Java

    Static Keyword in Java

    Constructor in Java

    Overloading in Java

    Overriding in Java

Leave a Reply

Your email address will not be published.


*