Let’s take a look on operator = . This is basically assigning a value to our variable. This can also be used to assign object reference. More of this later on my series of java tutorial.

Aside from the equal operator we also have the following:

Java Arithmetic Operators

Operator Description
+ addition and can also be used as string concatenator
subtraction
* multiplication
/ division
% modulo operator

Java Unary Operators

Operator Description
+ Unary plus operator, indicates positive value however unsign values indicates positive values
Unary minus operator, it negates an expression; mathematically signifies negative numbers
++ Increment operator, increments a value by 1 (example of which is i++)
Decrement operator,decrements a value by 1(example of which is i – -)
! Logical complement operator,inverts the value of a boolean (example is !booleanVar)

This example clearly demonstrates the usage of the java plus operator,java arithmetic operators, and unary operators as well.

package com.javatutorialhq.sample;

public class JavaOperator {

	/**
	 * This tutorial demonstrate usage of operators that is widely used
	 * in Java Programming. A simple if condition is also included.
	 */
	public static void main(String[] args) {
		//variable assignment
		int age=28;
		String firstName = "Randy";
		String lastName = "Chu";
		boolean isMarried = true;
		float taxRate = 5.17f;
		int salary = 23000;
		int numberOfChildren = 3;
		boolean isWifePregnant = true;
		if(isWifePregnant){
			numberOfChildren++;
		}

		//compute netsalary
		float netSalary = salary - (taxRate/100) * salary ;

		//print information
		System.out.println("Name:"+firstName + " " + lastName);
		System.out.println("Is he married:"+isMarried);
		System.out.println("Age:" + isMarried);
		System.out.println("Net Salary:"+"$"+netSalary);
		System.out.println("Number of Children:"+numberOfChildren);
	}

}

and here is the ouput after executing above source code

Name:Randy Chu
Is he married:true
Age:true
Net Salary:$21810.9
Number of Children:4

Notice that we put a simple if condition on the above source code. This is to facilitate demonstration of java unary operator ++. We will be discussing a bit more on the conditional statements later on. Going back on the sample source code, the printing of details gives an overview on how to use +  operator in string manipulation. Usage of primitive data types are also in place like boolean, float, and int.

If you have queries on this part of my java tutorial, don’t hesitate to comment below.

Suggested Reading List