EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Java for EmSAT Achieve  >  Java Assignment Operators

Java Assignment Operators | Java for EmSAT Achieve PDF Download

Introduction

In Java, assignment operators are used to assign values to variables. They provide a convenient way to perform operations while assigning values. This article will cover the various assignment operators in Java and provide examples to help you understand their usage.

Basic Assignment Operator

The basic assignment operator in Java is the equal sign (=). It assigns the value on the right-hand side of the operator to the variable on the left-hand side. Let's look at an example:

int x = 10;

System.out.println("x = " + x); // Output: x = 10

Explanation:

  • We declare a variable x of type int and assign it the value 10.
  • The System.out.println() statement is used to print the value of x on the console.

Arithmetic Assignment Operators

Java provides several arithmetic assignment operators that combine arithmetic operations with assignment. These operators perform the specified arithmetic operation and assign the result to the variable. Here are the arithmetic assignment operators:

  • Addition assignment (+=): Adds the value on the right to the variable.
  • Subtraction assignment (-=): Subtracts the value on the right from the variable.
  • Multiplication assignment (*=): Multiplies the variable by the value on the right.
  • Division assignment (/=): Divides the variable by the value on the right.
  • Modulus assignment (%=): Calculates the remainder of division and assigns it to the variable.

Let's see examples of each arithmetic assignment operator:

Addition assignment (+=)

int a = 5;

a += 3;

System.out.println("a = " + a); // Output: a = 8

Explanation

  • The value of a is initially 5.
  • Using the += operator, we add 3 to a, and the result (8) is assigned back to a.

Subtraction assignment (-=)

int b = 10;

b -= 4;

System.out.println("b = " + b); // Output: b = 6

Explanation:

  • The value of b is initially 10.
  • Using the -= operator, we subtract 4 from b, and the result (6) is assigned back to b.

Multiplication assignment (*=)

int c = 3;

c *= 5;

System.out.println("c = " + c); // Output: c = 15

Explanation:

  • The value of c is initially 3.
  • Using the *= operator, we multiply c by 5, and the result (15) is assigned back to c.

Division assignment (/=)

int d = 20;

d /= 4;

System.out.println("d = " + d); // Output: d = 5

Explanation:

  • The value of d is initially 20.
  • Using the /= operator, we divide d by 4, and the result (5) is assigned back to d.

Modulus assignment (%=)

int e = 17;

e %= 5;

System.out.println("e = " + e); // Output: e = 2

Explanation:

  • The value of e is initially 17.
  • Using the %= operator, we calculate the remainder of dividing e by 5, which is 2, and assign it back to e.

Sample Problems

Here are some sample problems to test your understanding of assignment operators:

1. Write a program to calculate the compound interest using the formula:
A = P * (1 + r/n)^(nt), where:

  • A is the final amount
  • P is the principal amount
  • r is the annual interest rate
  • n is the number of times interest is compounded per year
  • t is the number of years

Solution:

double principal = 1000;

double rate = 0.05;

int time = 3;

int compound = 2;


double amount = principal * Math.pow((1 + rate/compound), compound * time);


System.out.println("Compound interest: " + amount);

2. Write a program that swaps the values of two variables without using a temporary variable.

Solution:

int a = 5;

int b = 10;


a = a + b;

b = a - b;

a = a - b;


System.out.println("a = " + a); // Output: a = 10

System.out.println("b = " + b); // Output: b = 5

Conclusion

Assignment operators in Java provide a convenient way to perform operations while assigning values to variables. We covered the basic assignment operator (=) and various arithmetic assignment operators (+=, -=, *=, /=, %=). It's essential to understand how these operators work to write efficient and concise code. By practicing the examples and solving the sample problems, you'll gain a solid understanding of Java assignment operators.

The document Java Assignment Operators | Java for EmSAT Achieve is a part of the EmSAT Achieve Course Java for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
60 videos|37 docs|12 tests

Top Courses for EmSAT Achieve

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Semester Notes

,

shortcuts and tricks

,

Java Assignment Operators | Java for EmSAT Achieve

,

Java Assignment Operators | Java for EmSAT Achieve

,

Objective type Questions

,

past year papers

,

Important questions

,

video lectures

,

MCQs

,

pdf

,

study material

,

Java Assignment Operators | Java for EmSAT Achieve

,

Summary

,

Viva Questions

,

Extra Questions

,

practice quizzes

,

Sample Paper

,

Previous Year Questions with Solutions

,

Exam

,

Free

,

ppt

,

mock tests for examination

;