CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Assignment Operator in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Assignment Operator in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X

The assignment operator in Java is denoted by the symbol “=”. This operator is used to assign a value to a variable.

Table of Contents

Syntax of assignment operator
Assign value directly
Assign value of another variable
= is different from ==
Use assignment operator with an arithmetic operator
Conclusion

Syntax of assignment operator

The general syntax for the assignment operator is:

variable = expression;

Assign value directly

We can use the assignment operator to assign a value to a variable. For example:

int x;
x = 5;

In the above code, we declare a variable x of type int and then assign it the value 5 using the assignment operator.

Assign value of another variable

The assignment operator can also be used to assign the value of one variable to another. For example:

int x = 5;
int y;
y = x;

In this code, the value of x (5) is assigned to y.

= is different from ==

It’s important to note that the assignment operator in Java is different from the comparison operator ==, which is used to compare the values of two variables. For example:

int x = 5;
int y = 10;
if (x == y) {
  System.out.println("x and y are equal");
} else {
  System.out.println("x and y are not equal");
}

In this code, the comparison operator == is used to compare the values of x and y. Since x is not equal to y, the message “x and y are not equal” will be printed.

Use assignment operator with an arithmetic operator

The assignment operator can also be combined with arithmetic operators to create compound assignments.

For example:

int x = 5;
x += 10; // x is now 15
x *= 2; // x is now 30

In the first line, the compound assignment += adds the value 10 to x and assigns the result back to x. In the second line, the compound assignment *= multiplies the value of x by 2 and assigns the result back to x.

It’s important to note that the assignment operator has lower precedence than most other operators, so it is usually necessary to use parentheses to specify the correct order of operations. For example:

int x = 5;
int y = 10;
int z = x + y; // z is 15

x = y = z; // x is 15, y is 15

x = (y = z) + 10; // x is 25, y is 15

In the first line, the addition operator + has higher precedence than the assignment operator, so the addition is performed before the assignment. In the second line, the assignment operator is right-associative, so it is performed from right to left. In the third line, the parentheses are used to specify that the assignment should be performed before the addition.

Conclusion

The assignment operator in Java is used to assign a value to a variable. It can also be used to assign the value of one variable to another, and it can be combined with arithmetic operators to create compound assignments. Just be sure to use parentheses to specify the correct order of operations when necessary.

Related Posts:

  • Expressions in Java
  • Arithmetic Operator in Java
  • Control Statements in Java
  • Increment and Decrement Operator in Java
  • Floating point in Java
  • Identifiers in Java
Tags:javalanguage-fundamentals
Was this article helpful?
โ† Previous ArticleCreate LinkedList in Java
Next Article โ†’Singly vs Doubly Linked List in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap