Assignment, Arithmetic, & Unary Operators

Assignment Operator

The most common operator that you’ll come across as a Java programmer is the simple assignment operator “=”. The assignment operator assigns the value on its right to the operand on its left. The list of below statements uses the assignment operator with a few of the primitive data types.

int jcdInt = 1;
byte jcdByte = 127;
short jcdShort = 30000;
boolean jcdBool = false;
String jcdString = "Java Code Depot";

Arithmetic Operator

The Java Programming Language allows you to perform traditional arithmetic calculations with the arithmetic operators. With Java you can perform addition ( + ), subtraction ( – ), multiplication ( * ), and division ( / ). In order to divide one operand by another operand, you need to use the remainder operator that uses the percent sign ( % ) to perform the calculation.

Operator Description
+ Addition Operator
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator

Addition Operator Sample

int jcdAddResult = (10 + 10);
System.out.println("Result: "+jcdAddResult);

Result: 20

Subtraction Operator Sample

int jcdSubtractResult = (100 - 10);
System.out.println("Result: "+jcdSubtractResult);

Result: 90

Multiplication Operator Sample

int jcdMultiplyResult = (10 * 10);
System.out.println("Result: "+jcdMultiplyResult);

Result: 100

Division Operator Sample

int jcdDivideResult = (100 / 20);
System.out.println("Result: "+jcdDivideResult);

Result: 5

Remainder Operator Sample

int jcdRemainderResult = (100 % 21);
System.out.println("Result: "+jcdRemainderResult);

Result: 16

Unary Operator

The unary operators require only one operand; they perform various operations such as incrementing and decrementing a value by exactly one, negating an expression, or reversing the value of a boolean.

Operator Description
+ Unary plus operator (indicates positive value)
- Unary minus operator (negates an expression)
++ Increment operator (increases by 1)
-- Decrement operator (decreases by 1)
! Logical complement operator (reverse value)

Unary plus, Unary minus, Increment, Decrement, and Logical Complement Operator samples.

int jcdStartingInt = +1;
System.out.println("Result: "+jcdStartingInt);
Result: 1

System.out.println("Result: "+jcdStartingInt);
Result: 0

System.out.println("Result: "+jcdStartingInt);
Result: 1

jcdStartingInt = -jcdStartingInt;
System.out.println("Result: "+jcdStartingInt);
Result: -1

boolean jcdSuccess = false;
System.out.println("Result: "+jcdSuccess);
Result: false

Result: true

Now that your done with lesson 6.1, be sure to move onto the next lesson.

Add a Comment

Your email address will not be published. Required fields are marked *