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

Day 5) Java Operators & Examples (2) _20180601 am

NS
normalstory
cover image

package b_operation;

public class OperationEx01 {

public static void main(String[] args){

/* 8. Shift operators (<<, >>, >>> used in graphics)

- Usable only on integer-type variables

- Each digit (in binary) of the operand moves right or left

- Used because multiplication and division run faster this way

- << : x<<n means x*2^n

- >> : x>>n means x/2^n

- If positive, fill empty slots with 0; if negative, fill with 1 */

System.out.println(10<<18);


/* 9. Comparison operators

- Operators used to compare two variables or literals

- Mostly used in the condition expressions of if / loop statements; result is true or false  

*similar to the ternary operator

- Binary operator //however, both sides must be of the same type

10. Relational comparison operators (<> <= >=)

- Can be used on primitive types except boolean

11. Equality comparison operators (==, !=)

- Can be used on any data type

- For primitives, compares the stored value

- For reference types, compares the stored address

expression result

x < y true if x is less than y, false otherwise

x > y true if x is greater than y, false otherwise

x <= y true if x is less than or equal to y, false otherwise

x >= y true if x is greater than or equal to y, false otherwise

x == y true if x equals y, false otherwise

x != y true if x differs from y, false otherwise  */

//[Example]

//Declare and initialize a variable that can hold 15

int da= 15;

//Declare and initialize a variable that can hold 15.000001

float db= 15.000001f;

//Print whether da and db are equal

System.out.println(da==db);

System.out.println(0.1==0.1f); //double is an approximation of 0.1 (2^n), float is 0.1

System.out.println('A'==65); //types need to match for the operation. Converts to int (A=65) then compares 


/* 12. Bitwise operators &, |, ^  bit -> convert to binary first

- Performs binary bit operations

- Usable on all primitive types except float and double

& (AND): 1 only when both operands are 1

| (OR): 1 if either operand is 1

^ (XOR): 1 when the operands differ

3 00000011

5 00000101

^ 00000110 -> 6

| 00000111 -> 7

& 00000001 -> 1

Used in circuit operations  */

System.out.println(3^5);


/* 13. Logical operators (=> receive or operate on boolean results) (&&, ||)

- Operands must be boolean or condition expressions that evaluate to boolean

- Used to combine conditions in if / loop statements.

&& (AND) - true only if both operands are true <- same precedence as *

|| (OR) - true if either operand is true <- same precedence as +

//basis for judging a regular expression    */ 

//Q1. Declare a variable dc that can hold one character

char dc;

//Q2. Initialize dc with K

dc = 'K';

//Q3. Print true when dc is an uppercase English letter

System.out.println('A'<= dc && dc<='Z'); 

//Q4. Print true when dc is a lowercase English letter

System.out.println('a'<= dc && dc<='z'); 

//Q5. Build a condition that checks whether dc is an English letter

System.out.println('A'<= dc && dc<='Z' || 'a'<= dc && dc<='z'); 

//Q6. Build a condition that checks whether dc is a numeric 'character'

System.out.println('0'<= dc && dc<='9'); 

/* 14. Ternary operator

- Requires three operands

- The condition must evaluate to true or false

- Basic structure

condition ? expression1 (for true) : expression2 (for false) */

int x1 = 10;

int y1 = -10;

int absX = x1>0 ? x1 : -x1;   

int absY = y1>0 ? y1 : -y1;   //absolute value — force the result to always come out positive

System.out.println(absX +"," + absY);

/* 15. Assignment operators (=, op=)

- Used to 'store' a value or the result of an expression into a variable

- Lowest operator precedence

*/

//Q1. Declare and initialize a variable ea with the value 10

int ea = 10;


//Q2. Store the value of ea+20 into ea

ea = ea + 20;

ea +=20; //apply the operation to itself and store back in itself

ea *=2; //apply the operation to itself and store back in itself

ea /=10; //apply the operation to itself and store back in itself

ea <<=3; //2^3

System.out.println(ea);

//Q3. Round at the 4th decimal place and display to the 3rd decimal place 

float fff = 3.141592f;

// fff *=1000;   //3141.592f

// fff += 0.5;   //3142.592f 

// int a = (int)fff; 

// float b1 = a; 

// b1 /= 1000;

// System.out.println(b1);


// VS.

// fff *=1000;  //3141.592f

// fff += 0.5;  //3142.592f 

// (int) //3142

// 1000f //3.142

fff = (int)(fff*1000+0.5)/1000f;

System.out.println(fff);

//Q4. 

//1) Store 523.845636 in variable dddd

float dddd=523.845636f;

//2) Round dddd at the 5th decimal and display to the 4th

dddd = (int)(dddd*10000+0.5)/10000f;   //(int)(double)/(float)

System.out.println(dddd);

double dddd1=523.845636;

//2) Round dddd at the 5th decimal and display to the 4th

dddd1 = (int)(dddd1*10000+0.5)/10000.0;

System.out.println(dddd1);

double dddd2=523.845636;

//2) Round dddd at the 5th decimal and display to the 4th

dddd2 = (int)(dddd2*10000+0.5)/10000.0;

System.out.println((float)dddd2);

}

}



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.