Chapter 2 - Java for Beginners Course

Arithmetic Operators

Now that we have defined the different primitive data types, we can start looking into how can we use variables of the different data types and perform operations with them.

The operations are specified via operators that will take one or more operands and return a resulting value.

As is the case in most programming languages, you can perform simple arithmetic operations between variables in Java as well, and where possible the symbols/operators used follow the ones used in mathematics, i.e. + for addition, - for subtraction, * for multiplication, / for division. The one that doesn’t follow the mathematical convention is the % that is used to calculate the remainder of a division.

To elaborate with some examples:

int cores = 4;
int memoryInGB = 256;

// The + symbol is used for addition
int result1 = memoryInGB + 256;
// result1 is 512
System.out.println(memoryInGB + " + 256 = " + result1);

// The - symbol is used for subtraction
int result2 = cores - 3;
// result2 is 1
System.out.println(cores + " - 3 = " + result2);

// The * symbol is used for multiplication
int result3 = cores * 2;
// result3 is 8
System.out.println(cores + " * 2 = " + result3);

// The / symbol is used for division
int result4 = cores / 2;
// result4 is 2
System.out.println(cores + " / 2 = " + result4);

// The % symbol is used to get the remainder of a division
int result5 = cores % 3;
// result5 is 1
System.out.println(cores + " % 3 = " + result5);

Output:

256 + 256 = 512
4 - 3 = 1
4 * 2 = 8
4 / 2 = 2
4 % 3 = 1

The plus sign and String objects

The plus sign in Java, when used with String objects, provides us with String concatenation. You have seen it in several of the examples we’ve shown in this course, like in all the System.out.println(…​) invocations in the example above.

Another example:

String start = "This is a ";
String end = "sentence";
System.out.println(start + end);

Output:

This is a sentence

In this case, we are concatenating 2 objects of type String, printing out This is a sentence as a result.

If one of the operands is a String and the other operand is not of type String, Java will convert it into a String value for concatenation.

String concatenation using the plus sign is not recommended for code where performance is a concern. Take a look at StringBuilder and StringBuffer that you can use for that purpose depending on your needs. We’ll cover both of these in a separate course.
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
Dependencies

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

Contents
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