Computer Science
Java Assignment Operators
Java Assignment Operators are used to assign values to variables. They combine the assignment operator (=) with other operators such as addition, subtraction, multiplication, division, and modulus. These operators allow for concise and efficient code when assigning values to variables.
Written by Perlego with AI-assistance
Related key terms
1 of 5
7 Key excerpts on "Java Assignment Operators"
- eBook - ePub
100+ Solutions in Java
A Hands-On Introduction to Programming in Java: A Hands-On Introduction to Programming in Java (English Edition)
- Dhruti Shah, DHRUTI SHAH(Authors)
- 2020(Publication Date)
- BPB Publications(Publisher)
The assignment operator = is used to assign the value of the operand on its right to the operand on its left. More than one variable can be assigned a value simultaneously. For example, int a =10; int b = c = 20; Java also supports compound assignment to assign the result of an expression to a variable. For example, int a = 10; a += 6; // this is resolved as a = a + 6;Arithmetic
Arithmetic operators operate on numeric data. However, they can be used with character data as well. These are binary operators which means that they operate on two operands. The following are the arithmetic operators in Java:- Addition (+): Performs the addition operation on the operands.
- Subtraction (-): Performs the subtraction operation on the operands.
- Multiplication (*): Performs the multiplication operation on the operands.
- Division (/): Performs the division operation on the operands.
- Modulo (%): Returns the remainder of a division operation.
Figure 2.4: Using Arithmetic operatorsNote that the modulo operation returned a 0 because 4 is directly divisible by 2.Unary
Unary operators operate on a single operand. Java supports different types of unary operators which are as follows:- Unary plus (+): Indicates a positive value.
- Unary minus (-): Negates an expression.
- Increment (++): Increments the value of a variable by 1.
- Decrement (--): Decrements the value of a variable by 1.
- Logical complement (!): Inverts a boolean value.
There are two ways of using increment and decrement operators, that is, prefix or postfix notation. Both will increment the value of a variable by 1. However, the prefix version will first increment the value and then assign it, whereas, the postfix version will first assign the value and then increment it. - eBook - ePub
Java
The Comprehensive Guide
- Christian Ullenboom(Author)
- 2022(Publication Date)
- SAP PRESS(Publisher)
2.4 Expressions, Operands, and Operators
Let’s start with some mathematical expressions and then illustrate how they are written in Java. A mathematical formula, such as the expression -27 * 9 , consists of operands and operators . For example, an operand is a variable, a literal, or the return of a method call. In the case of a variable, the value is read from the variable and the calculation is performed with it.The operators link the operands. Depending on the number of operands, the following types of operators are distinguished:-
An operator defined on exactly one operand is referred to as a unary operator (or monadic operator ). The minus (negative sign ) before an operand is a unary operator since it applies to exactly the following operand.
-
The usual operators (plus, minus, multiply, and divide) by are binary ( dyadic ) operators .
- A question mark operator is used for conditional expressions, which have 3 characters.
Operators allow the connection of individual expressions to new expressions. Some operators are familiar from school, such as addition, comparison, assignment, and others. C(++) programmers will recognize many old friends.2.4.1 Assignment Operator
In Java, the equal sign = is used for an assignment . [ 57 ] The assignment operator is a binary operator with the variable to be assigned on the left and an expression on the right.[eg] Example Let’s look at some expressions with assignments:int quantity = 12 , total ; total = quantity * 2 ;The multiplication calculates the product of 12 and 2 and stores the result in total . From all primitive variables that occur in the expression, the value is read and inserted into the expression. [ 58 ] This process is also called a value operation because the value of the variable is considered (and not its location or even its variable name).Only after the expression has been evaluated does the assignment operator copy the result into the variable. If runtime errors occur, for example, due to a division by zero, no write access to the variable is possible. - eBook - PDF
- Doug Lowe(Author)
- 2023(Publication Date)
- For Dummies(Publisher)
The key to understanding the rest of this section is realizing that in Java, assign- ments are expressions, not statements. In other words, a = 5 is an assignment expression, not an assignment statement. It becomes an assignment statement only when you add a semicolon to the end. The result of an assignment expression is the value that’s assigned to the variable. The result of the expression a = 5, for example, is 5. Likewise, the result of the expression a = (b + c) * d is the result of the expression (b + c) * d. The implication is that you can use assignment expressions in the middle of other expressions. The following example is legal: int a; int b; a = (b = 3) * 2; // a is 6, b is 3 As in any expression, the part of the expression inside the parentheses is evalu- ated first. Thus, b is assigned the value 3. Then the multiplication is performed, and the result (6) is assigned to the variable a. Now consider a more complicated case: int a; int b = 2; a = (b = 3) * b; // a is 9, b is 3 108 BOOK 2 Programming Basics What’s happening here is that the expression in the parentheses is evaluated first, which means that b is set to 3 before the multiplication is performed. The parentheses are important in the previous example because without paren- theses, the assignment operator is the last operator to be evaluated in Java’s order of precedence. Consider one more example: int a; int b = 2; a = b = 3 * b; // a is 6, b is 6 This time, the multiplication 3 * b is performed first, giving a result of 6. Then this result is assigned to b. Finally, the result of that assignment expression (6) is assigned to a. Incidentally, the following expression is also legal: a = b = c = 3; This expression assigns the value 3 to all three variables. Using Compound Assignment Operators A compound assignment operator is an operator that performs a calculation and an assignment at the same time. - eBook - PDF
Java Programming Fundamentals
Problem Solving Through Object Oriented Analysis and Design
- Premchand S. Nair(Author)
- 2008(Publication Date)
- CRC Press(Publisher)
Kirk; 54 ■ Java Programming Fundamentals Consider the assignment statement: hoursWorked = 5 * 8 - 3; The computer first evaluates the expression on the right to an int data type of value 37. Now the variable on the left-hand side is of type int . Therefore, int value 37 is stored at memory location hoursWorked . Semantics of other assignment statements are similar. A Java statement such as k = k + 1; means take the current value of variable k , add 1 to it, and assign the new value to the memory location k . In the case of an assignment statement, the expres-sion on the right side is evaluated first irrespective of the variable on the left-hand side. Once the right-hand side value is computed, the result is stored in the memory location specified by the variable on the left side. Thus, if value of k was 10 before the execution of the statement, k becomes 11 after the execution of the statement. Semantics of Assignment Operator Statement i j k p q int i; 0 int j = 10; 10 int k = j +2; 12 double p; 0.0 double q = 1.234; 1.234 i = j * i + k; 12 k = j % i; 10 p = i / (k + 2) + q; 2.234 k = i / (k + 2) + q; error Suppose that a, b, c, and d are variables of the same data type. The following statement is legal in Java: a = b = c = d; In this statement, first the value of d is assigned to c . Since = is an operator, c = d produces (or returns) a value and the value retuned is equal to that of c . Therefore, this statement is equivalent to the following two statements: c = d; a = b = c; By the same argument, the statement a = b = c; is equivalent to the following two statements: b = c; a = b; Class and Java Fundamentals ■ 55 Thus, the statement a = b = c = d; is equivalent to the following three statements: c = d; b = c; a = b; Note 2.15 The associativity of the assignment operator is from right to left. There-fore, the assignment operator, =, is evaluated from right to left. Self-Check 31. Assume that x and y are two variables of type int and let x be 10 and y be 20 . - eBook - ePub
- Mario De Ghetto(Author)
- 2022(Publication Date)
- Youcanprint(Publisher)
decision in the program flow . Let's see, then, in detail what are the types of operators we have available. The operators can be classified into the following types:•assignment operators;•arithmetic operators;•comparison operators;•boolean logical operators;•bit by bit and shift operators;•equality operators;Assignment operatorsThe assignment operator used in Visual Studio is the same one that is used by many other languages: "= ".NOTE – This symbol is NOT used to compare two values. In C# we compare two values using a different symbol: "== ", that is, two equality symbols side by side.The value to be assigned or the expression are placed to the right of the assignment operator, while the name of the variable to be modified is placed to the left. For example, to assign the value “http://deghettoen.wordpress.com ” to a string variable named URL , or to assign the result of an expression to an integer variable, you can proceed as follows:string URL = "https://deghettoen.wordpress.com"; // Blog Address int result = 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21;// Sum of the first 8 // Sequence numbers of FibonacciThe same variable used to store the result can also be used in the expression. In this case, first the result to the right of the assignment operator is calculated, then the result is assigned to the variable indicated to the left of that operator:counter = counter + 1;If before the counter operation was valid 10 , the operation 10+1 is performed and the value 11 is assigned to the counter . After the execution of this instruction, counter will have the value 11 - eBook - PDF
- Todd Greanier(Author)
- 2006(Publication Date)
- Sybex(Publisher)
All do the same thing, of course: assign a value. The difference is that five of them involve an extra step before assignment occurs. ◆ = ◆ += ◆ -= ◆ *= ◆ /= ◆ %= The Basic Operator: = You have been using this assignment operator since Chapter 2. The equal sign ( = ) assigns a value to a variable. It is used for primitives and objects alike. int x = 50; Robot robot = new Robot(); Keywords and Operators 81 The single equal sign is used only to assign values and is never used for equality checking. Equality checking is a conditional operation that will be discussed in the next section. The Combination Operators The final five operators in the list of assignment operators all perform their action (add/subtract/multiply/divide/modulus) on the value to the right of the equal sign to the variable on the left. They are just shorthand; the same thing could be said in a more verbose fashion. You are never required to use these five combination operators, but you may find that they make your code more legible. For example, both of the following two lines add 10 to the current value of x . The first statement uses the combination assignment operator, which performs the addition, and then the assignment. The second statement does exactly the same thing, but in a more verbose fashion. Which form you use is your choice; the JVM will execute both statements exactly the same way. int x += 10; int x = x + 10; The remaining four combination assignment operators work exactly the same way except that the actual operation changes. The Relational Operators Six operators determine the relationship between two operands. Each of these operators returns either true or false . You will use conditional operators quite a bit when you work with flow control in Chapter 4, “Flow Control.” ◆ == ◆ > ◆ < ◆ >= ◆ <= ◆ != The Equality Test Operator: == When you want to test a variable to see if it is equal to a specific value, this is the operator to use. - eBook - ePub
Learn Java with Projects
A concise practical guide to learning everything a Java professional really needs to know
- Dr. Seán Kennedy, Maaike van Putten(Authors)
- 2023(Publication Date)
- Packt Publishing(Publisher)
3
Operators and Casting
In Chapter 2 , we learned that variables are simply named pigeonholes and contain values. These values vary and Java provides eight primitive data types accordingly. These primitive types cater for whole numbers (byte , char , short , int , and long ), decimal numbers (float and double ), and the literals true and false ( boolean ).We also learned how to declare a variable. As Java is a strongly typed language, this means you must give every variable a data type immediately upon declaration. This is where primitive data types are very useful.Now that we know how to declare variables, let’s do something interesting with them. By the end of this chapter, you will be able to combine variables using Java’s various operators. In addition, you will understand Java casting, including what it is, and when and why it occurs.In this chapter, we are going to cover the following main topics:- Learning how Java’s operators cooperate
- Understanding Java’s operators
- Explaining Java casting
Technical requirements
The code for this chapter can be found on GitHub at https://github.com/PacktPublishing/Learn-Java-with-Projects/tree/main/ch3 .Learning how Java’s operators cooperate
Java provides numerous operators for us to work with. By way of definition, if we have an expression 3 + 4 , the + is the operator , whereas 3 and 4 are the operands . Since + has two operands, it is known as a binary operator.Before we discuss the operators themselves, we must first discuss two important features relating to Java operators, namely order of precedence and associativity .Order of precedence
Order of precedence specifies how operands are grouped with operators. This becomes important when you have shared operands in a complex expression. In the following code segment, we have an expression of 2 + 3 * 4 , where * represents multiplication and + represents addition:int a = 2 + 3 * 4;System.out.println(a);In the preceding code, 3 is shared by both 2 and 4 . So, the question arises, do we group 3 with 2 , where the expression is (2 + 3) * 4 , giving us 20 ; or do we group 3 with 4 , where the expression is 2 + (3 * 4) , giving us 14 ? This is where the order of precedence applies. As * has higher precedence than + , 3 is grouped with 4 and therefore the expression evaluates to 2 + (3 * 4)
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.






