Back to feed
Renewal·서른의 생활코딩

Day 4) Basic Operators and Operator Precedence 20180531 AM

NS
normalstory
cover image

package b_operation;

public class OperationBasic {

public static void main(String[] args){

/* 1. Basics of operators

- Unary operators 

: ++, --, +(positive), -(negative), ~(bitwise NOT = tilde), !, (cast operator = type name)

- Binary operators 

: arithmetic: +, -, *, /, %(modulo), <<(shift — for speed, C-style), >>, >>>

: comparison: <, >, <=, >=, ==, !=, instanceof

: logical: &, |(or), ^(xor), &&(logical AND), ||

: assignment: =, op= (+=, -=, etc.) 

- Ternary operator 

: 5==5? run when true : run when false

  //{} block, [] array

2. Operator precedence

- arithmetic > comparison > logical > assignment (=)    int a = 3 + 4 * 2;

- unary > binary > ternary              but varies case by case

- default evaluation is left-to-right except for unary and assignment (=) operators*/

}

}




package b_operation;

public class OperationEx01 {

public static void main(String[] args){

/*1. Increment/decrement operators (++, --)

- Increment operator: increases the operand's value by 1

- Decrement operator: decreases the operand's value by 1 */

int i = 3;

i++; //4

++i; //5

int i2=7;

int i3=5;

int i4= ++i2 + i3; // ++ is prefix, so the value changes before the operation

//8 + 5 =13  //(i=8) 

int i5= i2++ + i3; //8++ + 5 = 13 //(i=9) the operation value differs from the variable value 

int i6 = 10;

int i7 = 20;

int i8 = i6++ + ++i7; //31

int i9 = 21;

int i10 = 5;

int i11 = ++i9 + ++i10 + i9++ + ++i10; // 22 + 6 + 22(i9=23) + 7(i=7) = 57

System.out.println(i9);

System.out.println(i10);

System.out.println(i11);

/*2. Sign operators (+, -)

- Cannot be used on boolean or char among the primitives. */


/*3. Bitwise NOT operator (~)  // anything bit-related, think in binary.

- Only for integer types and char.

- Flips 0 to 1 and 1 to 0 in the binary representation. */

char c = 'A';

char d = (char)~c;

System.out.println(d);

byte b = 10;

int bb = ~b; // bb needs to be declared as int because of the operation.

System.out.println(bb);

/*4. Logical NOT operator

- Usable only on booleans.

- true -> false, false -> true

- Implements a logical toggle like a TV power button. */

// Problem: declare and initialize a variable "power" holding false.

boolean power = false;

System.out.println(!power);

power = !power;                     // ! cannot follow the variable 

System.out.println(power);

/*5. Arithmetic operators 

- Include the four basic arithmetic ops, modulo, and shifts.

* They are binary operators taking two operands.

- For binary operators, if both operands are small,

they are first converted to 4-byte (int) before the operation.

6. The four arithmetic operators

- Operations between types smaller than int (4 bytes) are converted to int.

(both small)

byte + short => int + int

- Between two operands, the smaller type is promoted to the larger type.

(only one is smaller)

byte + long => long + long

char + float => float + float

- Integer division by zero is forbidden. */


// Problem 6-1: make ddd and store aaa + bbb into it.

byte aaa =1;

short bbb = 29;  

int ddd = aaa + bbb;

System.out.println(ddd);

// Problem 6-2: declare and initialize gg to hold cc * ff.

char cc = 'F'; //70

byte ff = 120; 

int gg = cc * ff;

System.out.println(gg);

// Problem 6-3: store hh + ii into jj, making sure the result is correct.

// Make sure a normal value is printed.

int hh = 10;

int ii = 2147483647;

long z = hh;

long x = ii;

long jj = z+x;

System.out.println(jj);

int hh2 = 10;

int ii2 = 2147483647;

//long jj2 = hh2 + ii2; 

// even if jj2 is long, the addition overflows before the assignment.

long jj2 = (long)hh2 + ii2;

System.out.println(jj2);

/*7. Modulo operator

- Divides the left operand by the right and returns only the remainder.

- Usable on every primitive except boolean. */


// Problem 7-1: store the quotient of ba / ca in the variable share.

int ba= 10;

int ca= 3;

int share = ba/ca;

System.out.println(share);


int remain = ba%ca;

System.out.println(remain);

// Problem 7-2: "10 divided by 3 has quotient 3 and remainder 1."

System.out.println(ba+" divided by "+ca+" has quotient "+share+" and remainder "+remain);

}

}



This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.

More on the author's page

Keep reading

Renewal

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min