Definition and Examples of Java Identifiers
- Java Programming
- PHP Programming
- Javascript Programming
- Delphi Programming
- C & C++ Programming
- Ruby Programming
- Visual Basic
- M.A., Advanced Information Systems, University of Glasgow
A Java identifier is a name given to a package, class, interface, method, or variable. It allows a programmer to refer to the item from other places in the program.
To make the most out of the identifiers you choose, make them meaningful and follow the standard Java naming conventions .

Examples of Java Identifiers
If you have variables that hold the name, height, and weight of a person, then choose identifiers that make their purpose obvious:
This to Remember About Java Identifiers
Since there are some strict syntax, or grammatical rules when it comes to Java identifiers (don't worry, they aren't hard to understand), make sure you're aware of these do's and don't:
- Reserved words like class , continue , void , else , and if cannot be used.
- "Java letters" is the term given to the acceptable letters that can be used for an identifier. This includes not only regular alphabet letters but also symbols, which just includes, without exception, the underscore (_) and dollar sign ($).
- "Java digits" include the numbers 0-9.
- An identifier can begin with a letter, dollar sign, or underscore, but not a digit. However, it's important to realize that digits can be used so long as they exist after the first character, like e8xmple
- Java letters and digits can be anything from the Unicode character set, which means characters in Chinese, Japanese, and other languages can be used.
- Spaces are not acceptable, so an underscore can be used instead.
- The length does not matter, so you can have a really long identifier if you choose.
- A compile-time error will occur if the identifier uses the same spelling as a keyword, the null literal, or boolean literal.
- Since the list of SQL keywords may, at some point in the future, include other SQL words (and identifiers can't be spelled the same as a keyword), it's usually not recommended that you use an SQL keyword as an identifier.
- It's recommended to use identifiers that are related to their values so they're easier to remember.
- Variables are case-sensitive, which means myvalue does not mean the same as MyValue
Note: If you're in a hurry, just take away the fact that an identifier is one or more characters that come from the pool of numbers, letters, the underscore, and the dollar sign, and that the first character must never be a number.
Following the rules above, these identifiers would be considered legal:
- _variablename
- $testvariable
- VariableTest
- variabletest
- this_is_a_variable_name_that_is_long_but_still_valid_because_of_the_underscores
Here are some examples of identifiers that are not valid because they disobey the rules mentioned above:
- 8example (this starts off with a digit)
- exa+ple (the plus sign isn't allowed)
- variable test (spaces are not valid)
- this_long_variable_name_is_not_valid_because_of_this-hyphen (while the underscores are acceptable like in the example from above, even the one hyphen in this identifier renders it invalid)
:max_bytes(150000):strip_icc():format(webp)/143058853-56a12f375f9b58b7d0bcdc3c.jpg)
By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts.
- Data Structure & Algorithm Classes (Live)
- System Design (Live)
- DevOps(Live)
- Explore More Live Courses
- Interview Preparation Course
- Data Science (Live)
- GATE CS & IT 2024
- Data Structure & Algorithm-Self Paced(C++/JAVA)
- Data Structures & Algorithms in Python
- Explore More Self-Paced Courses
- C++ Programming - Beginner to Advanced
- Java Programming - Beginner to Advanced
- C Programming - Beginner to Advanced
- Android App Development with Kotlin(Live)
- Full Stack Development with React & Node JS(Live)
- Java Backend Development(Live)
- React JS (Basic to Advanced)
- JavaScript Foundation
- Complete Data Science Program(Live)
- Mastering Data Analytics
- CBSE Class 12 Computer Science
- School Guide
- All Courses
- Linked List
- Binary Tree
- Binary Search Tree
- Advanced Data Structure
- All Data Structures
- Asymptotic Analysis
- Worst, Average and Best Cases
- Asymptotic Notations
- Little o and little omega notations
- Lower and Upper Bound Theory
- Analysis of Loops
- Solving Recurrences
- Amortized Analysis
- What does 'Space Complexity' mean ?
- Pseudo-polynomial Algorithms
- Polynomial Time Approximation Scheme
- A Time Complexity Question
- Searching Algorithms
- Sorting Algorithms
- Graph Algorithms
- Pattern Searching
- Geometric Algorithms
- Mathematical
- Bitwise Algorithms
- Randomized Algorithms
- Greedy Algorithms
- Dynamic Programming
- Divide and Conquer
- Backtracking
- Branch and Bound
- All Algorithms
- Company Preparation
- Practice Company Questions
- Interview Experiences
- Experienced Interviews
- Internship Interviews
- Competitive Programming
- Design Patterns
- System Design Tutorial
- Multiple Choice Quizzes
- Go Language
- Tailwind CSS
- Foundation CSS
- Materialize CSS
- Semantic UI
- Angular PrimeNG
- Angular ngx Bootstrap
- jQuery Mobile
- jQuery EasyUI
- React Bootstrap
- React Rebass
- React Desktop
- React Suite
- ReactJS Evergreen
- ReactJS Reactstrap
- BlueprintJS
- TensorFlow.js
- English Grammar
- School Programming
- Number System
- Trigonometry
- Probability
- Mensuration
- Class 8 Syllabus
- Class 9 Syllabus
- Class 10 Syllabus
- Class 8 Notes
- Class 9 Notes
- Class 10 Notes
- Class 11 Notes
- Class 12 Notes
- Class 8 Maths Solution
- Class 9 Maths Solution
- Class 10 Maths Solution
- Class 11 Maths Solution
- Class 12 Maths Solution
- Class 7 Notes
- History Class 7
- History Class 8
- History Class 9
- Geo. Class 7
- Geo. Class 8
- Geo. Class 9
- Civics Class 7
- Civics Class 8
- Business Studies (Class 11th)
- Microeconomics (Class 11th)
- Statistics for Economics (Class 11th)
- Business Studies (Class 12th)
- Accountancy (Class 12th)
- Macroeconomics (Class 12th)
- Machine Learning
- Data Science
- Mathematics
- Operating System
- Computer Networks
- Computer Organization and Architecture
- Theory of Computation
- Compiler Design
- Digital Logic
- Software Engineering
- GATE 2024 Live Course
- GATE Computer Science Notes
- Last Minute Notes
- GATE CS Solved Papers
- GATE CS Original Papers and Official Keys
- GATE CS 2023 Syllabus
- Important Topics for GATE CS
- GATE 2023 Important Dates
- Software Design Patterns
- HTML Cheat Sheet
- CSS Cheat Sheet
- Bootstrap Cheat Sheet
- JS Cheat Sheet
- jQuery Cheat Sheet
- Angular Cheat Sheet
- Facebook SDE Sheet
- Amazon SDE Sheet
- Apple SDE Sheet
- Netflix SDE Sheet
- Google SDE Sheet
- Wipro Coding Sheet
- Infosys Coding Sheet
- TCS Coding Sheet
- Cognizant Coding Sheet
- HCL Coding Sheet
- FAANG Coding Sheet
- Love Babbar Sheet
- Mass Recruiter Sheet
- Product-Based Coding Sheet
- Company-Wise Preparation Sheet
- Array Sheet
- String Sheet
- Graph Sheet
- ISRO CS Original Papers and Official Keys
- ISRO CS Solved Papers
- ISRO CS Syllabus for Scientist/Engineer Exam
- UGC NET CS Notes Paper II
- UGC NET CS Notes Paper III
- UGC NET CS Solved Papers
- Campus Ambassador Program
- School Ambassador Program
- Geek of the Month
- Campus Geek of the Month
- Placement Course
- Testimonials
- Student Chapter
- Geek on the Top
- Geography Notes
- History Notes
- Science & Tech. Notes
- Ethics Notes
- Polity Notes
- Economics Notes
- UPSC Previous Year Papers
- SSC CGL Syllabus
- General Studies
- Subjectwise Practice Papers
- Previous Year Papers
- SBI Clerk Syllabus
- General Awareness
- Quantitative Aptitude
- Reasoning Ability
- SBI Clerk Practice Papers
- SBI PO Syllabus
- SBI PO Practice Papers
- IBPS PO 2022 Syllabus
- English Notes
- Reasoning Notes
- Mock Question Papers
- IBPS Clerk Syllabus
- Apply for a Job
- Apply through Jobathon
- Hire through Jobathon
- All DSA Problems
- Problem of the Day
- GFG SDE Sheet
- Top 50 Array Problems
- Top 50 String Problems
- Top 50 Tree Problems
- Top 50 Graph Problems
- Top 50 DP Problems
- Solving For India-Hackthon
- GFG Weekly Coding Contest
- Job-A-Thon: Hiring Challenge
- BiWizard School Contest
- All Contests and Events
- Saved Videos
- What's New ?
- Data Structures
- Interview Preparation
- Topic-wise Practice
- Latest Blogs
- Write & Earn
- Web Development
Related Articles
- Write Articles
- Pick Topics to write
- Guidelines to Write
- Get Technical Writing Internship
- Write an Interview Experience
- Java Tutorial
- Introduction to Java
- Similarities and Difference between Java and C++
- Setting up the environment in Java
- Java Basic Syntax
- Java Hello World Program
- Differences between JDK, JRE and JVM
- How JVM Works – JVM Architecture?
- Java Identifiers
- Variables in Java
- Scope of Variables In Java
- Data types in Java
- Operators in Java
- Java Arithmetic Operators with Examples
Java Assignment Operators with Examples
- Java Unary Operator with Examples
- Java Relational Operators with Examples
- Java Logical Operators with Examples
- Java Ternary Operator with Examples
- Bitwise Operators in Java
- Packages In Java
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else statement with Examples
- Java if-else-if ladder with Examples
- Loops in Java
- Java For loop with Examples
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- return keyword in Java
- Arrays in Java
- Multidimensional Arrays in Java
- Jagged Array in Java
- Strings in Java
- String class in Java
- StringBuffer class in Java
- StringBuilder Class in Java with Examples
- Object Oriented Programming (OOPs) Concept in Java
- Classes and Objects in Java
- Methods in Java
- Access Modifiers in Java
- Wrapper Classes in Java
- Need of Wrapper Classes in Java
- Constructors in Java
- Copy Constructor in Java
- Constructor Chaining In Java with Examples
- Private Constructors and Singleton Classes in Java
- Inheritance in Java
- Java and Multiple Inheritance
- Comparison of Inheritance in C++ and Java
- Polymorphism in Java
- Dynamic Method Dispatch or Runtime Polymorphism in Java
- Method Overloading in Java
- Different ways of Method Overloading in Java
- Overriding in Java
- Difference Between Method Overloading and Method Overriding in Java
- Abstraction in Java
- Abstract Class in Java
- Difference between Abstract Class and Interface in Java
- Encapsulation in Java
- Interfaces in Java
- Nested Interface in Java
- Marker interface in Java
- Functional Interfaces in Java
- Comparator Interface in Java with Examples
- List of all Java Keywords
- Super Keyword in Java
- final Keyword in Java
- abstract keyword in java
- static Keyword in Java
- ‘this’ reference in Java
- enum in Java
- Exceptions in Java
- Types of Exception in Java with Examples
- Checked vs Unchecked Exceptions in Java
- Try, catch, throw and throws in Java
- Flow control in try catch finally in Java
- throw and throws in Java
- User-defined Custom Exception in Java
- Collections in Java
- Collections Class in Java
- List Interface in Java with Examples
- ArrayList in Java
- Vector Class in Java
- Stack Class in Java
- LinkedList in Java
- Queue Interface In Java
- PriorityQueue in Java
- Deque interface in Java with Example
- ArrayDeque in Java
- Set in Java
- HashSet in Java
- LinkedHashSet in Java with Examples
- SortedSet Interface in Java with Examples
- NavigableSet in Java with Examples
- TreeSet in Java
- Map Interface in Java
- HashMap in Java with Examples
- Hashtable in Java
- LinkedHashMap in Java
- SortedMap Interface in Java with Examples
- TreeMap in Java
- Multithreading in Java
- Lifecycle and States of a Thread in Java
- Main thread in Java
- Java Thread Priority in Multithreading
- Thread Pools in Java
- Synchronization in Java
- Method and Block Synchronization in Java
- Importance of Thread Synchronization in Java
- Thread Safety and how to achieve it in Java
- Difficulty Level : Easy
- Last Updated : 09 Dec, 2021
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide.
Types of Operators:
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
This article explains all that one needs to know regarding the Assignment Operators.
Assignment Operators
These operators are used to assign values to a variable. The left side operand of the assignment operator is a variable, and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type of the operand on the left side. Otherwise, the compiler will raise an error. This means that the assignment operators have right to left associativity, i.e., the value given on the right-hand side of the operator is assigned to the variable on the left. Therefore, the right-hand side value must be declared before using it or should be a constant. The general format of the assignment operator is,
Types of Assignment Operators in Java
The Assignment Operator is generally of two types. They are:
1. Simple Assignment Operator: The Simple Assignment Operator is used with the “=” sign where the left side consists of the operand and the right side consists of a value. The value of the right side must be of the same data type that has been defined on the left side.
2. Compound Assignment Operator: The Compound Operator is used where +,-,*, and / is used along with the = operator.
Let’s look at each of the assignment operators and how they operate:
1. (=) operator:
This is the most straightforward assignment operator, which is used to assign the value on the right to the variable on the left. This is the basic definition of an assignment operator and how it functions.
Syntax:
Example:
2. (+=) operator:
This operator is a compound of ‘+’ and ‘=’ operators. It operates by adding the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.
3. (-=) operator:
This operator is a compound of ‘-‘ and ‘=’ operators. It operates by subtracting the variable’s value on the right from the current value of the variable on the left and then assigning the result to the operand on the left.
4. (*=) operator:
This operator is a compound of ‘*’ and ‘=’ operators. It operates by multiplying the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.
5. (/=) operator:
This operator is a compound of ‘/’ and ‘=’ operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the quotient to the operand on the left.
6. (%=) operator:
This operator is a compound of ‘%’ and ‘=’ operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the remainder to the operand on the left.
Please Login to comment...
- nishkarshgandhi
- Java-Operators
Improve your Coding Skills with Practice
Start your coding journey now.
Learn Java interactively.
Learn Java practically and Get Certified .
Popular Tutorials
Popular examples, reference materials.
Learn Java Interactively
Java Introduction
- Java Hello World
- Java JVM, JRE and JDK
- Java Variables and Literals
- Java Data Types
Java Operators
- Java Input and Output
- Java Expressions & Blocks
- Java Comment
Java Flow Control
- Java if...else
- Java switch Statement
- Java for Loop
- Java for-each Loop
- Java while Loop
- Java break Statement
- Java continue Statement
- Java Arrays
- Multidimensional Array
- Java Copy Array
Java OOP (I)
- Java Class and Objects
- Java Methods
- Java Method Overloading
- Java Constructor
- Java Strings
- Java Access Modifiers
- Java this keyword
- Java final keyword
- Java Recursion
Java instanceof Operator
Java OOP (II)
- Java Inheritance
- Java Method Overriding
- Java super Keyword
- Abstract Class & Method
- Java Interfaces
- Java Polymorphism
- Java Encapsulation
Java OOP (III)
- Nested & Inner Class
- Java Static Class
- Java Anonymous Class
- Java Singleton
- Java enum Class
- Java enum Constructor
- Java enum String
- Java Reflection
- Java Exception Handling
- Java Exceptions
- Java try...catch
- Java throw and throws
- Java catch Multiple Exceptions
- Java try-with-resources
- Java Annotations
- Java Annotation Types
- Java Logging
- Java Assertions
- Java Collections Framework
- Java Collection Interface
- Java List Interface
- Java ArrayList
- Java Vector
- Java Queue Interface
- Java PriorityQueue
- Java Deque Interface
- Java LinkedList
- Java ArrayDeque
- Java BlockingQueue Interface
- Java ArrayBlockingQueue
- Java LinkedBlockingQueue
- Java Map Interface
- Java HashMap
- Java LinkedHashMap
- Java WeakHashMap
- Java EnumMap
- Java SortedMap Interface
- Java NavigableMap Interface
- Java TreeMap
- Java ConcurrentMap Interface
- Java ConcurrentHashMap
- Java Set Interface
- Java HashSet
- Java EnumSet
- Java LinkedhashSet
- Java SortedSet Interface
- Java NavigableSet Interface
- Java TreeSet
- Java Algorithms
- Java Iterator
- Java ListIterator
- Java I/O Streams
- Java InputStream
- Java OutputStream
- Java FileInputStream
- Java FileOutputStream
- Java ByteArrayInputStream
- Java ByteArrayOutputStream
- Java ObjectInputStream
- Java ObjectOutputStream
- Java BufferedInputStream
- Java BufferedOutputStream
- Java PrintStream
Java Reader/Writer
- Java Reader
- Java Writer
- Java InputStreamReader
- Java OutputStreamWriter
- Java FileReader
- Java FileWriter
- Java BufferedReader
- Java BufferedWriter
- Java StringReader
- Java StringWriter
- Java PrintWriter
Additional Topics
- Java Scanner Class
- Java Type Casting
- Java autoboxing and unboxing
- Java Lambda Expression
- Java Generics
- Java File Class
- Java Wrapper Class
- Java Command Line Arguments
Related Topics
Java Operator Precedence
Java Ternary Operator
Java Bitwise and Shift Operators
- Java if...else Statement
In this tutorial, you'll learn about different types of operators in Java, their syntax and how to use them with the help of examples.
Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication.
Operators in Java can be classified into 5 types:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
1. Java Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
Here, the + operator is used to add two variables a and b . Similarly, there are various other arithmetic operators in Java.
Example 1: Arithmetic Operators
In the above example, we have used + , - , and * operators to compute addition, subtraction, and multiplication operations.
/ Division Operator
Note the operation, a / b in our program. The / operator is the division operator.
If we use the division operator with two integers, then the resulting quotient will also be an integer. And, if one of the operands is a floating-point number, we will get the result will also be in floating-point.
% Modulo Operator
The modulo operator % computes the remainder. When a = 7 is divided by b = 4 , the remainder is 3 .
Note : The % operator is mainly used with integers.
2. Java Assignment Operators
Assignment operators are used in Java to assign values to variables. For example,
Here, = is the assignment operator. It assigns the value on its right to the variable on its left. That is, 5 is assigned to the variable age .
Let's see some more assignment operators available in Java.
Example 2: Assignment Operators
3. java relational operators.
Relational operators are used to check the relationship between two operands. For example,
Here, < operator is the relational operator. It checks if a is less than b or not.
It returns either true or false .
Example 3: Relational Operators
Note : Relational operators are used in decision making and loops.
4. Java Logical Operators
Logical operators are used to check whether an expression is true or false . They are used in decision making.
Example 4: Logical Operators
Working of Program
- (5 > 3) && (8 > 5) returns true because both (5 > 3) and (8 > 5) are true .
- (5 > 3) && (8 < 5) returns false because the expression (8 < 5) is false .
- (5 < 3) || (8 > 5) returns true because the expression (8 > 5) is true .
- (5 > 3) || (8 < 5) returns true because the expression (5 > 3) is true .
- (5 < 3) || (8 < 5) returns false because both (5 < 3) and (8 < 5) are false .
- !(5 == 3) returns true because 5 == 3 is false .
- !(5 > 3) returns false because 5 > 3 is true .
5. Java Unary Operators
Unary operators are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1 . That is, ++5 will return 6 .
Different types of unary operators are:
- Increment and Decrement Operators
Java also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1 , while -- decrease it by 1 . For example,
Here, the value of num gets increased to 6 from its initial value of 5 .
Example 5: Increment and Decrement Operators
In the above program, we have used the ++ and -- operator as prefixes (++a, --b) . We can also use these operators as postfix (a++, b++) .
There is a slight difference when these operators are used as prefix versus when they are used as a postfix.
To learn more about these operators, visit increment and decrement operators .
6. Java Bitwise Operators
Bitwise operators in Java are used to perform operations on individual bits. For example,
Here, ~ is a bitwise operator. It inverts the value of each bit ( 0 to 1 and 1 to 0 ).
The various bitwise operators present in Java are:
These operators are not generally used in Java. To learn more, visit Java Bitwise and Bit Shift Operators .
Other operators
Besides these operators, there are other additional operators in Java.
The instanceof operator checks whether an object is an instanceof a particular class. For example,
Here, str is an instance of the String class. Hence, the instanceof operator returns true . To learn more, visit Java instanceof .
The ternary operator (conditional operator) is shorthand for the if-then-else statement. For example,
Here's how it works.
- If the Expression is true , expression1 is assigned to the variable .
- If the Expression is false , expression2 is assigned to the variable .
Let's see an example of a ternary operator.
In the above example, we have used the ternary operator to check if the year is a leap year or not. To learn more, visit the Java ternary operator .
Now that you know about Java operators, it's time to know about the order in which operators are evaluated. To learn more, visit Java Operator Precedence .
Table of Contents
- Introduction
- Java Arithmetic Operators
- Java Assignment Operators
- Java Relational Operators
- Java Logical Operators
- Java Unary Operators
- Java Bitwise Operators
Sorry about that.
Related Tutorials
Java Tutorial
Try PRO for FREE
- Coding Ground
- Corporate Training
- Trending Categories

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
What are the assignment operators in Java?
This article will help you understand all about Assignment Operators in Java. Before jumping into Assignment Operators, let us revise about Operators.
In computer programming we often need to perform some arithmetical or logical operations. In such circumstances, we need operators to perform these tasks. Thus, an Operator is basically a symbol or token, which performs arithmetical or logical operations and gives us meaningful result. The values involved in the operation are called Operands.
Here is a basic Pictorial representation of Operators.

In this article we are only discussing about Assignment Operators, so let’s jump into it.
ASSIGNMENT OPERATORS
Assignment Operators are a part of Binary Operators which are included in Arithmetical Operators. They are used to assign values to a variable. The left hand operand of the assignment operator indicates variable and the right hand operand indicates value. The data type of the value on the right must be the same as that of the variable, otherwise will result in Compilation error. For example: =, + =, - =, / =, >> =, ^ =, ! = are few examples of Assignment Operators.
Below are the types of Assignment operators available to use in Java.
= is a simple assignment operator which assigns values from right operands to left operand.
For Example, A = 2 will assign value 2 into A
+= is an Add assignment operator which adds right operand to the left operand and assigns the result to the left operand.
For Example, A += B is equivalent to A = A + B
-= is a Subtract assignment operator which subtracts right operand from the left operand and assigns the result to the left operand.
For Example, A -= B is equivalent to A = A – B
*= is a Multiply assignment operator which multiplies right operand with the left operand and assigns the result to the left operand.
For Example, A *= B is equivalent to A = A * B
/= is a Divide assignment operator which divides left operand with the right operand and assigns the result to the left operand.
For Example, A /= B is equivalent to A = A / B
%= is a Modulus assignment operator which takes modulus using left and right operands and assigns the result to the left operand.
For Example, A %= B is equivalent to A = A % B
<<= is a Left shift assignment operator which left shifts the left operand by number of positions given by the right operand.
For Example, A <<= 2 is same as A = A << 2
>>= is a Right shift assignment operator which right shifts the left operand by number of positions given by the right operand.
For Example, A >>= 2 is same as A = A >> 2
&= is a Bitwise AND assignment operator which adds Bitwise right operand to Bitwise Left operand and assigns the result to the left operand.
For Example, A &= B is same as A = A & B
^= is a Bitwise XOR assignment operator which returns bit by bit XOR values of right operand and assigns the result to the left operand.
For Example, A ^= 2 is same as A = A ^ 2
|= is a Bitwise OR assignment operator which returns bit by bit OR values of right operand and assigns the result to the left operand.
For Example, A |= 2 is same as A = A | 2
Let’s see how to implement all above Assignment Operators in a Java Code.
I hope you have understood all concepts behind Assignment Operators in Java. Thanks for reading the article.

0 Followers
- Related Articles
- What are Assignment Operators in JavaScript?
- What are assignment operators in C#?
- Java Assignment Operators
- What are different assignment operators types in Python?
- Compound assignment operators in Java\n
- Assignment Operators in C++
- Python Assignment Operators
- Perl Assignment Operators
- What is the difference between = and: = assignment operators?
- Compound Assignment Operators in C++
- Compound assignment operators in C#
- Assignment operators in Dart Programming
- What are the unary operators in Java?
- What are the arithmetic operators in Java?
- What are the logical operators in Java?

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
Assignment, Arithmetic, and Unary Operators
The simple assignment operator.
One of the most common operators that you'll encounter is the simple assignment operator " = ". You saw this operator in the Bicycle class; it assigns the value on its right to the operand on its left:
This operator can also be used on objects to assign object references , as discussed in Creating Objects .
The Arithmetic Operators
The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is " % ", which divides one operand by another and returns the remainder as its result.
The following program, ArithmeticDemo , tests the arithmetic operators.
This program prints the following:
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments . For example, x+=1; and x=x+1; both increment the value of x by 1.
The + operator can also be used for concatenating (joining) two strings together, as shown in the following ConcatDemo program:
By the end of this program, the variable thirdString contains "This is a concatenated string.", which gets printed to standard output.

The Unary Operators
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
The following program, UnaryDemo , tests the unary operators:
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version ( ++result ) evaluates to the incremented value, whereas the postfix version ( result++ ) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
The following program, PrePostDemo , illustrates the prefix/postfix unary increment operator:
About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights
Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.
Java Tutorial
Java methods, java classes, java file handling, java how to, java reference, java examples, java operators.
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Try it Yourself »
Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:
Java divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Advertisement
Java Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable called x :
The addition assignment operator ( += ) adds a value to a variable:
A list of all assignment operators:
Java Comparison Operators
Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.
The return value of a comparison is either true or false . These values are known as Boolean values , and you will learn more about them in the Booleans and If..Else chapter.
In the following example, we use the greater than operator ( > ) to find out if 5 is greater than 3:
Java Logical Operators
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values:
Java Bitwise Operators
Bitwise operators are used to perform binary logic with the bits of an integer or long integer.
Note: The Bitwise examples above use 4-bit unsigned examples, but Java uses 32-bit signed integers and 64-bit signed long integers. Because of this, in Java, ~5 will not return 10. It will return -6. ~00000000000000000000000000000101 will return 11111111111111111111111111111010
In Java, 9 >> 1 will not return 12. It will return 4. 00000000000000000000000000001001 >> 1 will return 00000000000000000000000000000100
Test Yourself With Exercises
Multiply 10 with 5 , and print the result.
Start the Exercise

COLOR PICKER

Get your certification today!

Get certified by completing a course today!

Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
[email protected]
Your Suggestion:
Thank you for helping us.
Your message has been sent to W3Schools.
Top Tutorials
Top references, top examples, web certificates, get certified.
- Enterprise Java
- Web-based Java
- Data & Java
- Project Management
- Visual Basic
- Ruby / Rails
- Java Mobile
- Architecture & Design
- Open Source
- Web Services

Java provides many types of operators to perform a variety of calculations and functions, such as logical , arithmetic , relational , and others. With so many operators to choose from, it helps to group them based on the type of functionality they provide. This programming tutorial will focus on Java’s numerous a ssignment operators.
Before we begin, however, you may want to bookmark our other tutorials on Java operators, which include:
- Arithmetic Operators
- Comparison Operators
- Conditional Operators
- Logical Operators
- Bitwise and Shift Operators
Assignment Operators in Java
As the name conveys, assignment operators are used to assign values to a variable using the following syntax:
The left side operand of the assignment operator must be a variable, whereas the right side operand of the assignment operator may be a literal value or another variable. Moreover, the value or variable on the right side must be of the same data type of the operand on the left side. Otherwise, the compiler will raise an error. Assignment operators have a right to left associativity in that the value given on the right-hand side of the operator is assigned to the variable on the left. Therefore, the right-hand side variable must be declared before assignment.
You can learn more about variables in our programming tutorial: Working with Java Variables .
Types of Assignment Operators in Java
Java assignment operators are classified into two types: simple and compound .
The Simple assignment operator is the equals ( = ) sign, which is the most straightforward of the bunch. It simply assigns the value or variable on the right to the variable on the left.
Compound operators are comprised of both an arithmetic, bitwise, or shift operator in addition to the equals ( = ) sign.
Equals Operator (=) Java Example
First, let’s learn to use the one-and-only simple assignment operator – the Equals ( = ) operator – with the help of a Java program. It includes two assignments: a literal value to num1 and the num1 variable to num2 , after which both are printed to the console to show that the values have been assigned to the numbers:
The += Operator Java Example
A compound of the + and = operators, the += adds the current value of the variable on the left to the value on the right before assigning the result to the operand on the left. Here is some sample code to demonstrate how to use the += operator in Java:
The -= Operator Java Example
Made up of the – and = operators, the -= first subtracts the variable’s value on the right from the current value of the variable on the left before assigning the result to the operand on the left. We can see it at work below in the following code example showing how to decrement in Java using the -= operator:
The *= Operator Java Example
This Java operator is comprised of the * and = operators. It operates by multiplying the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left. Here’s a program that shows the *= operator in action:
The /= Operator Java Example
A combination of the / and = operators, the /= Operator divides the current value of the variable on the left by the value on the right and then assigns the quotient to the operand on the left. Here is some example code showing how to use the /= operator in Java:
%= Operator Java Example
The %= operator includes both the % and = operators. As seen in the program below, it divides the current value of the variable on the left by the value on the right and then assigns the remainder to the operand on the left:
Compound Bitwise and Shift Operators in Java
The Bitwise and Shift Operators that we just recently covered can also be utilized in compound form as seen in the list below:
- &= – Compound bitwise Assignment operator.
- ^= – Compound bitwise ^ assignment operator.
- >>= – Compound right shift assignment operator.
- >>>= – Compound right shift filled 0 assignment operator.
- <<= – Compound left shift assignment operator.
The following program demonstrates the working of all the Compound Bitwise and Shift Operators :
Final Thoughts on Java Assignment Operators
This programming tutorial presented an overview of Java’s simple and compound assignment Operators. An essential building block to any programming language, developers would be unable to store any data in their programs without them. Though not quite as indispensable as the equals operator, compound operators are great time savers, allowing you to perform arithmetic and bitwise operations and assignment in a single line of code.
Read more Java programming tutorials and guides to software development .
Latest Posts
Wrike tips and tricks, what is java type casting, how to use latex for technical writing, top python frameworks, a guide to java abstraction, related stories, java encapsulation tutorial.

Assignment Operator in Java | Example Program
An operator which is used to store a value into a particular variable is called assignment operator in java .
In any programming language, an assignment operator is the most commonly used to assign a value to a variable.
There are three categories of assignment operations in java programming. They are as follows:
- Simple assignment
- Compound assignment
- Assignment as expression
Simple Assignment
We can use a simple assignment in two ways:
- To store or assign a value to a variable.
- To store a value of a variable into another variable.
It has the following general format to represent a simple assignment.
- v: It is a variable name that represents a memory location where a value may be stored.
- expression: It may be a constant, variable, or a combination of constants, variables, and operators.
- = is an assignment operator.
For example:
Compound Assignment
The general form of compound assignment is as follows:
- op: It is a Java binary operator. It may be + – * / % << >> etc. v and expression are the same as explained in the simple assignment.
- The operator op = is known as a shorthand assignment operator in Java because
- v op = expression is shorthand notation for v = v operator expression.
1. x += 5; // It is equivalent to int x = x + 5; 2. x -= 10; // It is equivalent to int x = x – 10; 3. a *= 100; // Equivalent to int a = a * 100; 4. a /= (b + c);
Let’s take an example program based on compound assignment operator.
Program code 1:
Explanation:
3. z *= x * y; is equivalent to z = z * (x * y); z = z * (50 * (-70)); z = 50 * (-3500); z = -175000;
Assignment as Expression
In Java, we can also consider an assignment operation an expression because the operation has a result. The result of expression is a value that is stored in a variable. We mainly use it in more than one assignment.
1. int x = y – z + 4; // Here, the expression y – z + 4 is evaluated first and then its result is stored into the variable x.
Let’s take an example program where we will use an assignment as an expression.
Program code 3:
Explanation:
The following steps have been performed to evaluate the above expressions of the program. 1. a += 1; a = a + 1; a = 19 + 1; a = 20;
2. b -= 1; b = b – 1; b = 31 – 1; b = 30;
3. c *= 2; c = c * 2; c = 50 * 2; c = 100;
4. x = (10 + a); x = 10 + 20; // Here, the value of x will be 20, not 19. x = 30;
5. y = x + 100; y = 30 + 100; // Here, the value of x will be 30. y = 130;
6. z = x + y + c; z = 30 + 130 +100; // Here, the value of c will be 100. z = 260;
1. You cannot use more than one variable on the left-hand side of = operator.
x + y = 20; // It is invalid because there will be doubt to Java compiler regarding for storing the value 20.
2. You cannot use a literal or constant value on the left-hand side of = operator.
20 = a; // It is also invalid because how can we store value of x in a number.
Hope that this tutorial has covered all important points related to assignment operator in Java with example programs. I hope that you will have understood this topic clearly and enjoyed it. Thanks for reading!!!
Next ⇒ Unary Operator in Java ⇐ Prev Next ⇒
Java Assignment Operators
Java programming tutorial index.
The Java Assignment Operators are used when you want to assign a value to the expression. The assignment operator denoted by the single equal sign = .
In a Java assignment statement, any expression can be on the right side and the left side must be a variable name. For example, this does not mean that "a" is equal to "b", instead, it means assigning the value of 'b' to 'a'. It is as follows:

Assignment Operators in Java

Introduction to Assignment Operators in Java
Java Assignment Operators are classified into two categories, such as simple and compound assignment operators. As the name says, Assignment operators are used for assigning the values to the variables involved in the operations. Simple assignment operators handle plain, uncomplicated operations like addition, subtraction, multiplication and division. Compound assignment operators are used when there are more logical operations are required in the code, like ^, &, %, <>, >>, <<, etc.
The Assignment Operator is generally of two types. They are:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others

Simple Assignment Operator
Compound assignment operator.
The Simple Assignment Operator is used with the “=” sign where the left side consists of the operand, and the right side consists of a value. The value of the right side must be of the same data type that has been defined on the left side.
The compound operator is used where +,-,*, and / is used along with the = operator.
Types of Assignment Operator
There are various task operators. Assignments would result in the value of the objective/target variable after the assignment.
First, we are going to see and check the working of the Simple Assignment operator with the help of a Java program. The program includes assigning two values to number 1 and number 2 and then printing it in the output to show that the values have been assigned to the numbers.
When we execute the print statement, we get the following as output. We are able to see that the two numbers which have been initialized earlier have been printed. Both the numbers were initialized with the value 5.

Here, we are going to check the working of the compound assignment operator. The following is the list of compound assignment operators.
- += Compound additional assignment operator
- -= Compound subtraction assignment operator
- *= Compound multiplication assignment operator
- /= Compound division assignment operator.
The above mentioned are the four basic compound assignment operators that are present. There are other compound assignment operators which are present such as:
- %= Compound Modulo Assignment operator.
- &= Compound bitwise Assignment operator.
- ^= Compound bitwise ^ assignment operator.
- >>= Compound right shift assignment operator.
- >>>= Compound right shift filled 0 assignment operator.
- <<= Compound left shift assignment operator.
In this article, we are going to check the first four compound assignment operators in detail along with the other operators. The basic advantage of compound assignment operators is that it saves a lot of code within the Java language program.
1. Compound Additional Assignment Operator
This operator is used to add numbers to a variable continuously throughout the entire loop. We are going to see a program in which we have the sum of the first ith natural number with the help of a loop. In the for loop, we are going to use a Compound additional operator.
We see that when we enter the value as 10, that is, we get the sum of the first 10 natural numbers like 45.

2. Compound Subtraction Assignment Operator
This program can be used to delete a number from an existing bigger number. In the next program, we are going to see the deletion of numbers from a bigger number 100.
In the sample code, we see that the number 5 is entered, and from the number 100, the sum until 5 is subtracted, we get the answer as 85.

3. Compound Multiplication Assignment Operator
This program can be used to multiply numbers up to a certain number that the user enters. We see a program that is used to print the multiplication of a certain number by numbers inside a for a loop.
We enter the number as 5, and then we see that the result is the multiplication of the number with numbers below. In other words, this program shows the factorial of a number in simple terms. We see the output of the program in the below screen.

4. Compound Division Assignment Operator
In this case, we are going to see the division of a number using the division operator. We won’t be using any kind of loop in this case, but we are going to see the numerator and the denominator. We will input the value of the numerator and divide it by 10 and produce the output to the user.
In the program, we input 100 as a number, and we divide it by 10 using the Compound Division Assignment operator. We get the output finally as 10, as shown in the below screenshot.

5. Compound Operators (Remaining Operators)
In the below program, we will see the working of the remaining operators present. The remaining operators are %, ^, &, >>, << and >>>The following is the code and output.
In the output, we see the result of the compound assignment operations that were left. The output has been printed correspondingly.

Conclusion – Assignment Operators in Java
This article sees two kinds of Assignment operators- Simple Assignment operators and Compound Assignment operators. We see the working with the help of coding examples. There are advantages as well as disadvantages of using Compound Assignment operators. Assignment operators are used in all other programming languages like C, C++, Python, and wherever value has to be assigned to a variable. The only constraint is that the value has to be of the same data type as the variable which is declared.
Recommended Articles
This is a guide to Assignment Operators in Java. Here we discuss the Introduction and Types of Assignment Operators in Java, including Simple Assignment Operator, Compound Assignment Operator. You may also look at the following articles to learn more –
- Logical Operators in C#
- JavaScript Assignment Operators
- Assignment Operators in C++
- Logical Operators in C

Related Courses

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept
By signing up, you agree to our Terms of Use and Privacy Policy .
Forgot Password?
This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Explore 1000+ varieties of Mock tests View more
Submit Next Question

Browse Our Products

Item added to your cart
All about java assignment operators.
Like every other programming language, Java language also provides a feature called Operators. There are a few types of Operators exist in Java. In this java tutorial, we will focus our learning on Assignment operators.
Assignment Operators exist for assigning a value to a variable in Java. The right side of the operator will be a value and the left side will be the variable that we created. So a general syntax would be as follows:
Simple Assignment Operator:
+= Assignment Operator:
-= assignment operator:, *= assignment operator:, /= assignment operator:.
- Choosing a selection results in a full page refresh.

IMAGES
VIDEO
COMMENTS
Concrete class in Java is the default class and is a derived class that provides the basic implementations for all of the methods that are not already implemented in the base class.
Improve your computer literacy by learning about the three different types of exceptions a Java application might encounter. Errors are the bane of users and programmers alike. Developers obviously don't want their programs falling over at ...
Learn the definition and syntax rules for Java identifiers and how to choose an identifier. See examples of valid and invalid identifiers, as well. A Java identifier is a name given to a package, class, interface, method, or variable. It al...
Types of Assignment Operators in Java ... The Assignment Operator is generally of two types. They are: 1. Simple Assignment Operator: The Simple
Assignment operators are used in Java to assign values to variables. For example, int age; age = 5;. Here, = is the assignment operator. It
Assignment Operators are a part of Binary Operators which are included in Arithmetical Operators. They are used to assign values to a variable.
One of the most common operators that you'll encounter is the simple assignment operator " = ". You saw this operator in the Bicycle class; it assigns the
Assignment operators are used to assign values to variables. In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable
Java assignment operators are classified into two types: simple and compound. The Simple assignment operator is the equals (=) sign, which is
An operator which is used to store a value into a particular variable is called assignment operator in java. In any programming language, an assignment operator
Java Programming: The Assignment Operator in Java ProgrammingTopics Discussed:1. Assignment operator in Java.2. Assignment statements in
The Java Assignment Operators are used when you want to assign a value to the expression. The assignment operator denoted by the single equal sign =.
Java Assignment Operators are classified into two categories, such as simple and compound assignment operators. As the name says, Assignment operators are
Assignment Operators exist for assigning a value to a variable in Java. The right side of the operator will be a value and the left side will be