2

Arithmetic and Assignment Operators Explained in Java

 3 years ago
source link: https://www.makeuseof.com/arithmetic-assignment-operators-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Arithmetic and Assignment Operators Explained in Java

By Jerome Davidson

Published 16 hours ago

We promise these Arithmetic and Assignment Operators are more fun than the ones you used in Algebra II.

Arithmetic operators allow you to perform algebraic arithmetic in programming. That is, they enable you to add, subtract, divide and multiply numbers.

This article will also cover assignment operators. These enable you to give (assign) a certain value to a variable.

This tutorial is not just for Java programmers. Many other programming languages like C and Python use these same operators. Therefore, you can easily transfer and apply the knowledge you gain here.

Arithmetic Operators

There are 5 arithmetic operators in Java—the table below summarizes them.

Operator NameSymbolSample ExpressionAddition+x+3Subtraction-y-8Multiplication*x*yDivision/x/2Remainder%y%3

The symbols (+, -, /) should seem familiar. That's because they're the same as those typically used in algebra.

It's important to take note that the division operator (/) refers to integer division here. That is, 19/5 will evaluate to 3. Any fractional part that results from this computation is truncated.

Related: What Is a Constructor in Java and How Do You Use It?

You should have also noticed that the Java operator for multiplication is an asterisk (*) and not the usual multiplication symbol (×).

To get the modulus of two integers, Java uses the % symbol. The example given in the table is similar to the algebraic expression: y mod 3. The % operator gives the remainder after y is divided by 3. That is, 19%5 will evaluate to 4.

It's good practice to use parentheses for grouping subexpressions. This eases readability and helps to avoid logic and syntax errors.

( 4*y+(z/3)) // example

When you have multiple arithmetic operators in one expression, Java uses the rules of operator precedence to determine which subexpressions to evaluate first.

The table below categorizes the levels of operator precedence.

PrecedenceOperator Description1*
/
%Multiplication, division and modulus have the same level of precedence. If there are multiple operators of this type used, they are evaluated from left to right. 2+
-Addition and subtraction have the same level of precedence. If there are multiple operators of this type used, they are evaluated from left to right.3=This operator is evaluated last .

The operators (*, /%) have the highest level of precedence, then followed by (+, -) and finally (=). The operators (*, /, %), and (+, -) all associate from left to right. This simply means that their evaluation begins from the leftmost operator.

The third operator (=) associates from right to left. So if have x=3, that means 3 is assigned to x, and not x is assigned to 3.

Assignment Operators

The assignment operator (=) assigns a value to a variable.

y = y+7;

The above expression adds 7 to y and then assigns the final result to y. If you're new to programming, this expression might seem a little weird. This shouldn't bother you as the compiler will understand what you're trying to do.

Compound Assignment

You can simplify the way you express an assignment by using a compound assignment operator.

In the previous example, we could've simply written:

y+=7;

See the table below on how you can use compound assignment operators.

Compound OperatorSample Expression Expanded Form +=x+=2x=x+2-=y -=6y=y-6*=z *=7z=z*7/=a /=4a=a/4%=b %=9b= b%9

Increment & Decrement Operators

If you have the compound assignment +=1, you can simply write it as ++. This is known as the "increment operator". Similarly, the decrement operator is --.

Related: How to Write a for Loop in Java

When used before the operand, the increment and decrement operators are known as "prefix operators". And when used after the operand, they're called "postfix operators".

With prefix, the variable being operated on is first modified and then used while with postfix, the initial value before modification is used.

y++; //postfix, most preferred form by many programmers
++y; // prefix

Generally, both postfix and prefix operators yield the same answer. It's only when dealing with large expressions that the answer may change.

Make Operators Work For You

It's important to note that increment and decrement operators only act on variables (e.g. x++) and not direct values (but not 5++). You should also not leave any whitespace while using increment and decrement operators, unlike with the operators before that. Doing so will give a compile-time error.

Always use parentheses when possible to logically group expressions. This will avoid unnecessary logic errors.

With these operators under your belt, understanding how to use access modifiers in Java will be a piece of cake.

About The Author

60851024d5062-my-pic.jpg?fit=crop&w=100&h=100

Jerome Davidson (12 Articles Published)

Jerome is a Staff Writer at MakeUseOf. He covers articles on Programming and Linux. He's also a crypto enthusiast and is always keeps tabs on the crypto industry.

More From Jerome Davidson

Subscribe To Our Newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK