Assignment Operators

Start course
Overview
Difficulty
Beginner
Duration
2h 12m
Students
17
Description

In this course, we'll learn the fundamentals of Java. 

Learning Objectives

  • Variables
  • Data Types
  • Type Conversion & Type Casting
  • Operators
  • Operator Precedence 
  • Expressions, Statements and Block

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge
Transcript

Hi there, in this video, we'll talk about Assignment Operators. The assignment operators are the most common operators. An assignment operator assigns the value on its right to the operand on its left. = is the simple assignment operator. It assigns values from the right side operand to the left side operand. += is add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand. -= is the subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. *= is multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. /= is the divide AND assignment operator. 

t divides the left operand with the right operand and assigns the result to the left operand. %= is a modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. Let's make some examples with assignment operators. In exercise project, right click on the operator package and select 'New, Class'. Specify the class name as AssignmentOperator and select the checkbox for the main method. Okay, let's start to code. Let's declare three variables with int type. First x and assign 20. Second, y and assign 15, and last z with the initial value 0. Let's assign x + y to variable z. This is the simple assignment operator. 

Let's print the result by using the print method. We may run the program after all operations. Let's copy the code and change the code with z += x. This is the add AND assignment operator, it's the same as z = z + x. Let's change the code with z += x in the print method. Let's copy the code and change the code with z -= x. This is the subtract AND assignment operator. It's the same as z = z - x. Change the code with z -= x in the print method. Let's copy the code again and change the code with z *= x. This is the multiply AND assignment operator. It's the same as z = z * x. Change the code with z *= x in the print method. Let's copy the code again and change the code to z /= x. 

This is the divide AND assignment operator. It's the same as z = z / x. Change the code with z /= x in the print method. Let's copy the code again and change the code with z %= x. This is the modulus AND assignment operator. It's the same as z = z % x. Change the code with z %= x in the print method. That's all. Let's run the code. As you see in the console, x + y = 35. So, the current value of z is 35. The value of x is 20, so z = z + x = 55. The current value of z is now 55, so z = z - x is 55 - 20. So, the result is 35. The current value of z is now 35, so z = z * x is the same with 35 * 20, so the result is 700. The current value of z is now 700, so z = z / x is the same with 700 / 20, so the result is 35. The current value of z is now 35. And lastly, z = z % x  is the same 35%20, so the result is 15, meaning that the remainder is 15. The assignment operators in Java are like this. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
81
Courses
18
Learning Paths
1

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics