# Java Math Operators & Special Operators

2. Subtraction.
3. Multiplication
4. Division
5. Remainder
6. Special operators

We perform addition by using `+ `operator on numeric data types

`int num1 = 5;int num2 = 6;int sum = num1 + num2;System.out.println(sum); // 11int num3 = sum + 10;System.out.println(num3); // 21num3 = num3 + 1;System.out.println(num3); // 22`

# Subtraction

We perform subtraction by using `— `operator on numeric data types

`int num1 = 5;int num2 = 6;int sum = num2 - num1;System.out.println(sum); // 1int num3 = 100 - sum;System.out.println(num3); // 99num3 = num3 - 10;System.out.println(num3); // 89`

# Multiplication

We perform multiplication by using `* `operator on numeric data types

`int num1 = 5;int num2 = 6;int sum = num1 * num2;System.out.println(sum); // 30sum = sum * 2;System.out.println(sum); // 60`

# Division

We perform division by using `/ `operator on numeric data types

`int num1 = 50;int num2 = 10;int sum = num1 / num2;System.out.println(sum); // 5// Sometimes when dividing we can get a floating numberint num3 = 5 / 2;System.out.println(num3); // 2// Actual result is 2.5 but we should always remember that int is only for whole numbers so it will be 2.int num4 = 8 / 5;double num5 = 8.0 / 5;System.out.println(num4); // 1System.out.println(num5); // 1.6// remember it will not round, it will always truncate `

# Remainder

The remainder operator will calculate what remains after division. `%` — this is the remainder.

`System.out.println(12 % 10); // 2 because 12 / 10 is 1 and 2 remainsSystem.out.println(12 % 5); // 2 because 12 / 5 is 2and 2 remainsSystem.out.println(10 % 5); // 0 System.out.println(107 % 10); // 7System.out.println(9 % 4); // 1System.out.println(119 % 100); // 19System.out.println(25 % 10); // 5System.out.println(12 % 3); // 0`

# Special operators

Regular:

`int i = 5;i = i + 2;System.out.println(i); // 7`

With special operator:

`int i = 5;i += 2;System.out.println(i); // 7`

Both above examples are doing the exact same thing — adding 2 to our i variable.

Regular:

`int i2 = 10;i2 = i2 -  2;System.out.println(i2); // 8int i3 = 4;i3 = i3 * 3;System.out.println(i3); // 12int i4 = 10;i4 = i4 / 2;System.out.println(i4); // 5`

With special operator:

`int i2 = 10;i2 -= 2;System.out.println(i2); // 8int i3 = 4;i3 *= 3;System.out.println(i3); // 12int i4 = 10;i4 /= 2;System.out.println(i4); // 5`

# Increment and Decrement operators

The increment operator will increase the value of a numeric variable by 1.

`int num = 10;num++;System.out.println(num); // 11System.out.println(num++); // 11 System.out.println(num); // 12System.out.println(++num); // 13`
• post-increment (++ after variable) will increase value in the next use of the variable.
• pre-increment will increase value immediately.

The decrement operator will decrease the value of a numeric variable by 1.

`int num = 10;num--;System.out.println(num); // 9System.out.println(num--); // 9System.out.println(num); // 8System.out.println(--num); // 7`
• post-decrement(after variable) will decrease the value the next time we use our variable.
• pre-decrement will decrease value immediately.
`Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming OS, File, and File SystemWorking with terminal Welcome to Java Programming LanguageVariables and Primitives in JavaConvert String to numeric data typeInput from the terminal in JavaMethods with JavaJava Math Operators and special operatorsConditional branching in JavaSwitch statement in JavaTernary operator in JavaEnum in JavaString class and its methods in JavaLoops in JavaAccess modifiers in JavaStatic keyword in JavaThe final keyword in JavaClass and Object in JavaObject-Oriented Programming in JavaOOP: Encapsulation in JavaOOP: Inheritance in JavaOOP: Abstraction in JavaOOP: Polymorphism in JavaThe method Overriding vs Overloading in JavaArray in JavaData Structures with JavaCollection framework in JavaArrayList in JavaSet in JavaMap in JavaDate and Time in JavaException in JavaHow to work with files in JavaDesign PatternsGenerics in JavaMultithreading in javaAnnotations in JavaReflection in JavaReflection & Annotations - The Powerful CombinationRun terminal commands from JavaLambda in JavaUnit Testing in JavaBig O Notation for coding interviewsTop Java coding interview questions for SDET`

--

--

--

## More from Beknazar

Love podcasts or audiobooks? Learn on the go with our new app.

## Wearables and HRV monitor my stress, but digital stress balls don’t work for me. Help! ## Create and deploy your own COVID-19 tracker in 3 days ! ## DojoToken 5% Commission Referral Program ## General Tree Implementation ## Embracing functional style within object-oriented paradigm ## GC Deep Dive — Matter DeFi ## Running a flask app in a docker containers with VS Code docker extension ## Dynamic Scheduling with Spring Boot  ## Understanding NULL in Java ## Basics of Java Programming ## Unit Testing in Java 