Chapter 2 - Java for Beginners Course

Unary Operators

Operators that require a single operand are called Unary Operators, in contrast for example to the arithmetic operators that require two. Note that some of these operators also assign a new value to the operand.

Plus and minus unary operators

The plus unary operator is seldom used and indicates that a value is positive. Note that a positive value doesn’t need the plus operator, so the statement below is equivalent to int result1 = 1:

int result1 = +1;
// result1 is 1
System.out.println("result1 = " + result1);


result1 = 1

The minus unary operator is used to negate an expression, as in, it will return the negative value of a positive expression, or viceversa. For example:

int result2 = -result1;
// result2 is -1
System.out.println("result2 = " + result2);


result2 = -1

Increment/decrement operators

A double plus operator can be used to increment the value of a variable by 1, for example a++, and a double minus operator can be used to decrement the value of a variable by 1, for example a--.

These 2 operators can be used as a prefix or postfix operator, as in, both a++ or ++a will have the same effect on the variable a.

The following examples show the difference between the prefix and postfix versions:

Example 1: Prefix version

// prefix increment operator
int value1 = 0;
int result3 = ++value1;
// result3 is 1 and value1 is also 1
System.out.println("result3 = " + result3);
System.out.println("value1 = " + value1);


result3 = 1
value1 = 1

When using the prefix version, the incremented value of the variable is returned, hence in this case, result3 has a value of 1.

Example 2: Postfix version

// postfix increment operator
int value2 = 0;
int result4 = value2++;
// result4 is 0 and value2 is 1
System.out.println("result4 = " + result4);
System.out.println("value2 = " + value2);


result4 = 0
value2 = 1

When using the postfix version, the original value of the operand is returned, as in, before the increment happens, hence in this case, result4 has a value of 0.

The same applies to the prefix/postfix versions of the decrement operator.

Logical complement operator

This operator, represented by an exclamation mark (!), inverts the value of a boolean variable.

For example:

// logical complement operator
boolean value3 = true;
boolean result5 = !value3;
System.out.println("value3 = " + value3);
System.out.println("result5 = " + result5);


value3 = true
result5 = false

Note that the original operand isn’t updated, this means that value3 keeps its original value.

Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Main class for this step

This is a list of recommended tutorials or courses that might be useful before starting this one.

Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations