Basic Operators in Java with Example

Basic Operators in Java with Example

The Java tutorial on the complete example of Java basic operators

The Java operators is an essential thing in Java programming. They included in every logical, algorithm, methods, class, etc of the Java Programming. Java Operators are special symbol or character that use by specific operation between 1-3 operands then return a result. Here's an example of one operand.

int result = 100++;

Two operands.

int result = 100 + 200;

Three operands.

int result = 100 + 200 * 2;

Above example describe that "=", "+" and "*" symbol is an operator and the numbers (100, 200, 300) are operands.

There are a group of operators that classify into the following groups:

  • Java Assignment Operators
  • Java Arithmetic Operators
  • Java Unary Operators
  • Java Equality and Relational Operators
  • Java Conditional Operators
  • Java Bitwise and Bit Shift Operators

We will learn by practice all of those Java operators groups using Netbeans.

Assignment Operators

Java assignment operators assign the value on its right and the operand on its left.

int age = 100;

Above example describe that "int age" is the operand, "100" is the value, and "=" is the assignment operator. There are more than one Assignment operators, you can learn all of them the examples below.

Open the Netbeans application then create a new Java application project and give it the name AssignmentOperators. The newly created Netbeans Java application project will open the AssignmentOperators file automatically which it's the main and runnable Java application.

Next, we will put all Assignment Operators examples inside the main method.

Java Simple Assignment Operator "="

Simple and most used operators that assign a value on its right and operand on its left. It only uses one character or symbol "=".

int a = 100;
System.out.println(a);
 
Output:
100

Java Add and Assignment Operator "+="

The operand on its left increase or add by the value on its right then save the result on the operand. It uses two characters or symbols "+=".

int b = 5;
b += 5;
System.out.println(b);
 
Output:
10

Java Subtract and Assignment Operator "-="

The operand on its left is decreased or subtract by the value on its right then save the result on the operand. It uses two characters or symbols "-=".

int c = 100;
c -= 50;
System.out.println(c);
 
Output:
50

Java Multiply and Assignment Operator "*="

The operand on its left is multiplied by the value on its right then save the result on the operand. It uses two characters or symbols "*=".

int d = 8;
d *= 50;
System.out.println(d);
 
Output:
400

Java Divide and Assignment Operator "/="

The operand on its left is divided by the value on its right then save the result on the operand. It uses two characters or symbols "/=".

int e = 200;
e /= 10;
System.out.println(e);
 
Output:
20

Java Modulus and Assignment Operator "%="

The operand on its left is modulus by the value on its right then save the result on the operand. It uses two characters or symbols "%=".

int f = 20;
f %= 3;
System.out.println(f);
 
Output:
2

Java Left Shift and Assignment Operator "<<="

The operand on its left is a binary left shift by the value on its right then save the result on the operand. It uses three characters or symbols "<<=".

int g = 10;
g <<= 2;
System.out.println(g);
 
Output:
40

How did it work? The default value of the operand is "10" or "1010" then left a shift by "2" or add two digits of "0" on the right of "1010" then result in the value "101000". The result "101000" converted to decimal will be "40".

Java Right Shift and Assignment Operator ">>="

The operand on its left is a binary right shift by the value on its right then save the result on the operand. It uses three characters or symbols ">>=".

int h = 13;
h >>= 2;
System.out.println(h);
 
Output:
3

How did it work? The default value of the operand is "13" or "1101" then right shift by "2" or remove two digits on the right then result in the value "101000". The result "11" converted to decimal will be "3". If the value more same or more than binary digits of its operand, the result will always "0".

Java Bitwise AND and Assignment Operator "&="

The operand on its left is binary AND by the value on its right then save the result on the operand. It uses two characters or symbols "&=".

int i = 20;
i &= 30;
System.out.println(i);
 
Output:
20

How did it work? The default value of the operand is "20" or "10100", binary AND by "30" or "11110" then result in the value "10100". The result "10100" converted to decimal will be "20".

Java Bitwise OR and Assignment Operator "|="

The operand on its left is binary OR by the value on its right then save the result on the operand. It uses two characters or symbols "|=".

int j = 25;
j &= 15;
System.out.println(j);
 
Output:
9

How did it work? The default value of the operand is "25" or "11001", binary OR by "15" or "1111" then result in the value "1001". The result "1001" converted to decimal will be "9".

Java Bitwise XOR and Assignment Operator "^="

The operand on its left is binary XOR by the value on its right then save the result on the operand. It uses two characters or symbols "^=".

int k = 20;
k ^= 30;
System.out.println(k);
 
Output:
10

How did it work? The default value of the operand is "20" or "10100", binary XOR by "30" or "11110" then results in the value "1010". The result "1010" converted to decimal will be "10".

Java Arithmetic Operators

The Arithmetic is part of Mathematics. So, Java Arithmetic operators mean use Mathematics Arithmetic operators.

Still, the Netbeans application opened then create a new Java application project and give it the name ArithmeticOperators. The newly created Netbeans Java application project will open the ArithmeticOperators file automatically which it's the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.

Java Addition Operator "+"

Adds two values together or left value add by right value. It uses the character "+".

int a = 200 + 300;
System.out.println(a);
 
Output:
500<span style="color: rgba(0, 0, 0, 0.8); font-family: Roboto, sans-serif; font-size: 1.17em; background-color: rgb(255, 255, 255);">
</span>

Java Subtraction Operator "-"

Subtract the left value by the right value. It uses the character "-".

int b = 500 - 300;
System.out.println(b);
 
Output:
200

Java Multiplication Operator "*"

Multiply two values or left value multiply by right value. It uses the character "*".

int c = 10 * 10;
System.out.println(c);
 
Output:
100

Java Division Operator "/"

Divide two values or left value divide by right value. It uses the character "/".

int d = 100 / 10;
System.out.println(d);
 
Output:
10

Java Modulus Operator "%"

Modulus two values or left value modulus by the right value and return the result that left from the division. It uses the character "%".

int e = 35 % 4;
System.out.println(e);
 
Output:
3

Java Increment Operator "++"

Increase the value of the operand on the left. It uses two characters or symbols "++".

int f = 20;
++f;
System.out.println(f);
 
Output:
21

Java Decrement Operator "--"

Decrease the value of the operand on the left. It uses two characters or symbols "--".

int g = 20;
--g;
System.out.println(g);
 
Output:
19

Java Unary Operators

The Java Unary Operators use only on one operand that performs a various operation such as incrementing/decrementing a value by one step, negating an expression, and inverse a boolean value.

Still, the Netbeans application opened then create a new Java application project and give it the name UnaryOperators. The newly created Netbeans Java application project will open the UnaryOperators file automatically which it's the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.

Java Unary Plus Operator

Java unary plus operator just an indication of the positive value. It uses a character or symbol "+" before its value.

int a = +1;
System.out.println(a);
 
Output:
1

Java Unary Minus Operator

Java unary minus operator just an indication of the negative value. It uses a character or symbol "-" before its value.

int b = -1;
System.out.println(b);
 
Output:
-1

Java Unary Increment Operator

Java unary increment operator increases the operand value. It uses two characters or symbols "++" after its operand.

int c = 10;
c++;
System.out.println(c);
 
Output:
11

Java Unary Decrement Operator

Java unary decrement operator decreases the operand value. It uses two characters or symbols "--" after its operand.

int d = 10;
d--;
System.out.println(d);
 
Output:
9

Java Unary Logical Complement Operator

Java unary logical complement operator inverts the value of boolean. It uses a character or symbol "!" before its operand.

boolean status = true;
System.out.println(!status);
 
Output:
false

Java Equality and Relational Operators

Java equality and relational operators use to determine one operand is greater than, greater than or equal, equal, less than, less than or equal, not equal to another operand.

Still, the Netbeans application opened then create a new Java application project and give it the name EqualityRelationalOperators. The newly created Netbeans Java application project will open the EqualityRelationalOperators file automatically which its the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.

Java Greater Than Operator ">"

Java greater than operator used to compare that left operand is greater than the right operand. It uses a character or symbol ">".

int a = 20;
int b = 10;
if (a > b)
    System.out.println(true);
 
Output:
true<span style="color: rgba(0, 0, 0, 0.8); font-family: Roboto, sans-serif; font-size: 1.17em; background-color: rgb(255, 255, 255);">
</span>

Java Greater Than or Equal Operator ">="

Java greater than or equal operator use to compare that left operand is greater than or equal right operand. It uses two characters or symbols ">=".

int c = 20;
int d = 20;
if (c >= d)
    System.out.println(true);
 
Output:
true

Java Less Than Operator "<"

Java less than operator used to compare that left operand is less than the right operand. It uses a character or symbol "<".

int e = 10;
int f = 20;
if (e < f)
    System.out.println(true);
 
Output:
true

Java Less Than or Equal Operator "<="

Java less than or equal operator use to compare that left operand is less than or equal right operand. It uses two characters or symbols "<=".

int g = 20;
int h = 20;
if (g <= h)
    System.out.println(true);
 
Output:
true

Java Equal Operator "=="

Java equal operator use to compare that left operand is equal to the right operand. It uses two characters or symbols "==".

int i = 20;
int j = 20;
if (i == j)
    System.out.println(true);
 
Output:
true

Java Not Equal Operator "!="

Java bot equal operator uses to compare that left operand is not equal to the right operand. It uses two characters or symbols "!=".

int k = 20;
int l = 10;
if (k != l)
    System.out.println(true);
 
Output:
true

Java Conditional Operators

Java Conditional Operators "&&" and "||" use to join with conditional between two boolean expressions.

Still, the Netbeans application opened then create a new Java application project and give it the name ConditionalOperators. The newly created Netbeans Java application project will open the ConditionalOperators file automatically which it's the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.

Java Conditional-AND Operator "&&"

Java conditional-AND operator uses to join two boolean expressions which both have the non-null result. This operator uses the characters or symbols "&&".

int a = 10;
int b = 20;
if((a == 10) && (b != 10)) {
    System.out.println(true);
}
 
Output:
true

Java Conditional-OR Operator "||"

Java conditional-OR operator uses to choose two boolean expressions which one of them has the non-null result. This operator uses the characters or symbols "||".

int c = 10;
int d = 20;
if((c == 10) || (b == 10)) {
    System.out.println(true);
}
 
Output:
true

Java Bitwise and Bit Shift Operators

Java bitwise and bit shift operators perform a binary operation between two integer value. Bitwise operators use the characters or symbols AND "&", OR "|", XOR "^", and compliment "~". Bit shift operators use the characters or symbols left shift "<<", right shift ">>", and zero-fill right shift ">>>".

Still, the Netbeans application opened then create a new Java application project and give it the name BitwiseAndBitshiftOperators. The newly created Netbeans Java application project will open the BitwiseAndBitshiftOperators file automatically which it's the main and runnable Java application. Next, we will put all Assignment Operators examples inside the main method.

Java Bitwise AND Operator "&"

Java bitwise AND operator perform a binary operation between two integer value with logical AND. It uses a character or symbol "&".

int a = 13; // 00001101
int b = 12; // 00001100
int c = a & b; // 00001101 & 00001100 = 00001100
System.out.println(c);
 
Output:
12

Java Bitwise OR Operator "|"

Java bitwise OR operator perform a binary operation between two integer value with logical OR. It uses a character or symbol "|".

int d = 13; // 00001101
int e = 12; // 00001100
int f = d | e; // 00001101 | 00001100 = 00001101
System.out.println(f);
 
Output:
13

Java Bitwise XOR Operator "^"

Java bitwise XOR operator performs a binary operation between two integer value with logical XOR. It uses a character or symbol "^".

int g = 13; // 00001101
int h = 12; // 00001100
int i = g ^ h; // 00001101 ^ 00001100 = 00000001
System.out.println(i);
 
Output:
1

Java Bit Left Shift Operator "<<"

Java bit left shift operator to perform a binary operation between two integer value by left shift the left value by the amount of the right value. It uses a character or symbol "<<".

int j = 13; // 00001101
int k = 12;
int l = g << h; // Add 12 digits 0 of 00001101 to the right = 00001101000000000000
System.out.println(l);
 
Output:
53248

Java Bit Right Shift Operator ">>"

Java bit right shift operator perform a binary operation between two integer value by removing left value by the amount of right value. It uses a character or symbol ">>".

int m = 8; // 00001000
int n = 2;
int o = m >> n; // Remove 2 digits of 00001000 to the left
System.out.println(o);

Output:
2

Bit Zero Fill Right Shift Operator ">>>"

Java bit zero-fill right shift operator perform a binary operation between two integer value by left shift the left value by the amount of the right value depends on sign extension. It uses a character or symbol ">>>".

int q = 8; // 1000
int r = 2;
int s = q >>> r; // Remove 2 digits of 00001000 to the left
System.out.println(s);

Output:
2

That it's, Java Tutorial: Basic Operators Examples. You can find the example source code on our GitHub.

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Description
This is the best course to learn to program in Java in Spanish from scratch and without any experience in this fabulous programming language. This is the first course where we will study the Fundamentals of Java, and we will take you step by step until you acquire the bases of the Java language and you can start to study more advanced Java topics.

The content is divided into perfectly structured levels, each level supported by the previous one, with the aim of adding Java knowledge incrementally and so you can focus on mastering the issues little by little and gradually. So ensure the success of your Java training.

We will also offer support for any doubts about the didactic material included in this Java Fundamentals course.

We manage a new teaching methodology that we have called Speed ​​Learning. This methodology consists of concise videos that go directly to the point to be studied, complemented by eBooks with explanations and step-by-step images (which you can print, or search for any text you need, or use for your offline study), since As we know we can not do text search within a video. In addition, our methodology includes perfectly structured and very didactic exercises that will allow you to accelerate your eLearning learning. No loss of time in videos where you have to watch the instructor codify an exercise, too much theory, little practice or anything like that. Our Speed ​​Learning methodology guarantees that in the shortest possible time you will acquire the necessary knowledge for the Java professional and working world.

The Java Fundamentals course includes the following topics for study:

Lesson 1 - Starting with Java Technology

The amazing world of Java programming

What is Java technology (from a practical approach)

Our first Java program from scratch

Lesson 2 - Variables and Operators in Java

Use of Variables in Java and what we use them for

Types of Data in Java and how they are classified

Management and Classification of operators in Java

Lesson 3 - Control statements in Java

Using the if-else structure and where to use it

Handling the switch structure and when applying it

Lesson 4 - Handling Loops in Java

Use of the for loop and its use

Using the while loop and how to apply it

Use of the do-while loop and when to use it

Lesson 5 - Object Oriented Programming

Introduction to Object Oriented Programming (OOP)

Handling Classes in Java

Use of Objects in Java

Lesson 6 - Functions in Java

Declaration of Methods or Functions in Java

Use and call of functions in Java

Lesson 7 - Data Management in Java

Using Arrays in Java

Management of Matrices in Java

Lesson 8 - Inheritance in Java

Inheritance Management in Java

Use of superclasses and subclasses in Java

Final Level Laboratory

Final Exercise where everything learned in this Level is integrated

At the end you get a certificate of having completed the Java Fundamentals course.

We wait for you on the other side.

Ing. Ubaldo Acosta

Founder of Global Mentoring

Passion for Java Technology

Who this course is for:

Anyone who wants to learn how to program in Java
Basic knowledge
Basic knowledge of PC use
Basic management of an operating system such as Windows, Mac or Linux
It is not necessary to know how to program, we will start from scratch !!!
The attitude and desire to start coding and learning Java once and for all from scratch!
What will you learn
Have the basics of the programming language with Java
You will know the basic syntax of the Java language
Manage the concept of Variables and Operators in Java
We will study Object Oriented Programming with Java
You will learn the Control Statements and Loops in Java
We will see the concept of Functions with Java
We will study the concept of Inheritance in Java
We will learn to use Arrays in java
We will handle the concept of Matrices in Java
We will learn to Design Classes in Java
We will make a final application with everything learned in the course
To know more:

Java Essentials : Learn Core Java From Basic to Advance

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

Description
This is only Java related course and it's great because it covers just the right amount of Java which is needed to leaning programming, java.

This is a comprehensive yet simple course on java programming language and it concentrates on Java programming concepts.

*************************** No Prior Coding Experience Needed ***************************

This course assumes that you have no programming background. If you have some experience then, it's just a bonus point. You have never code, have some experience or have a lot of experience any other programming language, this course is one stop place for you.

Java is one of the most and useful programming languages to learn You can build back-end of web applications and build robust test automation framework. Specially for Selenium WebDriver GUI automation, Java is most popular choice and has the largest community.

Each lecture consist of a video screencast and code files

There are quizzes, homework to test your knowledge

High focus on practice and asking questions

You will also learn coding best practices

Market is never short of jobs in Java programming language, there are ample of jobs in both Java development and Automation Testing using Java.

What are you waiting for? Enroll today and learn the powerful Java language !!!

Basic knowledge
Nothing else! It’s just you, your computer and your hunger to get started today
Java concepts are covered in the course, no experience needed
Windows/MAC computer
What will you learn
You will be able to EXPLAIN, DESIGN and IMPLEMENT efficient java Programs
You will be confident to clear test automation interviews
Understand the concepts of Object Oriented Programming Language
Complete understanding of java
Expert-level knowledge of Java code (+ advanced tips and tricks used by the pros)
Suitable for beginner programmers and ideal for users who learn faster when shown
To learn more:

Fundamentos de Java: Aprende Java desde cero, sin misterios | Simpliv

Fundamentos de Java: Aprende Java desde cero, sin misterios | Simpliv

Fundamentos de Java: Aprende Java desde cero, sin misterios

Description
This is the best course to learn to program in Java in Spanish from scratch and without any experience in this fabulous programming language . This is the first course where we will study the Java Fundamentals, and we will take you step by step until you acquire the basics of the Java language and so you can start studying more advanced Java topics.

The content is divided into perfectly structured levels , each level supported by the previous one, with the aim of adding Java knowledge incrementally so that you can focus on mastering the issues little by little and gradually. So ensure the success of your Java training.

In other offer support of any doubt teaching materials included in this course Fundamentals of Java.

To make matters worse, we handle a new teaching methodology that we have called Speed ​​Learning. This methodology consists of concise videos that go directly to the point to study, complemented with eBooks with explanations and step-by-step images (which you can print, or search for any text you need, or use for your offline study), since as we know we cannot do text search within a video. In addition, our methodology includes perfectly structured and very didactic exercises, which will allow you to accelerate your eLearning learning. Without wasting time on videos where you have to watch the instructor codify an exercise, too much theory, little practice or anything like that. Our Speed ​​Learning methodology guarantees that in the shortest possible time you will acquire the necessary knowledge for the professional and professional world of Java.

The Java Fundamentals course includes the following topics of study:

Level. Java basics

Lesson 1 - Starting with Java Technology

The amazing world of Java programming
What is Java technology (from a practical approach)
Our first Java program from scratch
Lesson 2 - Variables and Operators in Java

Use of Variables in Java and what we use them for
Data types in Java and how they are classified
Operator Management and Classification in Java
Lesson 3 - Control sentences in Java

Use of the if-else structure and where to use it
Management of the switch structure and when to apply it
Lesson 4 - Cycle Management in Java

Use of the for cycle and its use
Use of the while cycle and how to apply it
Use of the do-while cycle and when to use it
Lesson 5 - Object Oriented Programming

Introduction to Object Oriented Programming (OOP)
Class Management in Java
Using Objects in Java
Lesson 6 - Functions in Java

Declaration of Methods or Functions in Java
Use and call of functions in Java
Lesson 7 - Data Management in Java

Using Arrangements in Java
Matrix Management in Java
Lesson 8 - Inheritance in Java

Inheritance Management in Java
Use of superclasses and subclasses in Java
Final Level Laboratory

Final Exercise where everything learned in this Level is integrated
At the end you get a certificate of having completed the Java Fundamentals course.

We wait for you from the other side.

Ing. Ubaldo Acosta

Founder of Global Mentoring

Passion for Java Technology

Who this course is for:

Anyone who wants to learn to program in Java
Basic knowledge
Basic knowledge of PC use
Basic operation of an operating system such as Windows, Mac or Linux
It is not required to know how to program, we will start from scratch !!!
The attitude and desire to start coding and learning Java once and for all from scratch !!!
What will you learn
Have the basics of the programming language with Java
You will know the basic syntax of the Java language
Will handle the concept of Variables and Operators in Java
We will study Object Oriented Programming with Java
You will learn Control Sentences and Cycles in Java
We will see the concept of Functions with Java
We will study the concept of Inheritance in Java
We will learn to use Arrangements in java
We will handle the concept of Matrices in Java
We will learn to Design Classes in Java
We will make a final application with everything learned in the course
To continue: