JAVA | Data Processing

Yunny.Log ·2022년 10월 19일
0

JAVA

목록 보기
26/29
post-thumbnail

Math.random

  • 0~1 사이 => 0~0.999999
    -n = (int)(11 Math.random());
    – random() : 0 ~ 0.9999… interval
    – 11
    Math.random()) : 0 ~ 10.9999… interval
    – (int) : get integer part
    – make a random integer between 0 and 10
  • n = (int)(11 * Math.random()) – 5;
    – make a random integer between -5 and 5

Operator

Logical operator : && (AND), || (OR), ! (NOT)

– if ( c >= ‘A’ && c <= ‘Z’) (statement)
• If c is from A to Z, (statement) is executed.
– if ( ! ( c >= ‘A’ && c <= ‘Z’)) (statement)
• If c is not form A to Z, (statement) is executed.
– while ( i < 0 || i > 50 ) (statement)
• If i < 0 or i > 50, (statement) is executed.

Conditional operator

 i:2 j :3

 i/j : 0
 (float)i/j : 0.6666667
 (float)(i/j) : 0.0
 (int)3.789 : 3
 (char)65.789 : A

Assignment

– Arithmetic Assignment

+ =  
– = 
* = 
/ = 
% =

– Bit Assignment

<< = (shift a bit pattern to the left) 
>> = (shift a bit pattern to the right)
| = (bit OR) & = (bit AND) ^ = (bit XOR)

대입 연산 예시 **

Casting

  • casting 결과값
 i:2 j :3

 i/j : 0
 (float)i/j : 0.6666667
 (float)(i/j) : 0.0
 (int)3.789 : 3
 (char)65.789 : A
 i:2 j :3
 (float)i/j : 0.6666667
 (float)(i/j) : 0.0

Bitwise operator

– op1 & op2
• The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.

– op1 | op2
• The OR operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0.

– op1^ op2
• The EXCLUSIVE-OR operator compares two bits and generates a result of 1 if either or both bits are 1; otherwise, it returns 0.

– ~op1
• The COMPLEMENT operator is used to invert all of the bits of the operand.

– op1 >> op2
• The SHIFT RIGHT operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. Each move to the right effectively divides op1 in half.

– op1 << op2
• The SHIFT LEFT operator moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. Each move to the left effectively multiplies op1 by 2.

Operator priority ***

비트 연산 more 예제

(+) 16진수 
16진법은 0~9까는 아라비아 숫자와 똑같이 표기하지만,10 부터는 알파벳을 이용한다.
10 = A,11 = B,12 = C ... 15 = F

연산자 조합 순서

  • 대입이랑 , ++/-- 얘네는 역순으로 수행 **

 a : a7

 a & F0 : a0
 a | F0 : f7
 a ^ F0 : 57
  • (+) 시프트 연산 추가 ( >> , << )

x:2 y:5 z:8

x / y * z : 0
x = y += z : 13

Data type

< Integer (byte, short, int, long) >

< Floating point types (float, double) >

< Boolean >

< The Character type(char) >

– char type can be used integer type.

  • Char typed data is possible doing arithmetic operation.
  • The char type denotes character in the Unicode encoding scheme.
    – char c = ‘D’; c -= 2;
    • c=68 -> c=c-2; -> c=66 -> c=‘B’
  • c = (char)(‘A’ + 3);
    • The Unicode of A is 65. ‘A’+3 is 68. (char) cast from int to char
  • c = (char)(‘A’+’5’);
    • The Unicode of A, 5 are 65, 53. So ‘A’+’5’=65+53=118
    • 소문자 v

  • (+) 65 : 'A'
  • (+) 97 : 'a'
  • (+) '0' : 48

String length, substring


  • length, substring(i,j) - i부터 j전까지 , substring (start index) start index 부터 끝까지

String comparison

– boolean equals(Object other)

  • returns true if the string equals other.
    – boolean s1.equals(s2) ;
  • Return true if the strings s and t are equal, false otherwise.
  • boolean b = “JAVA”.equals(“JAVA”);
    - b is true
    – boolean s1.equalsIgnoreCase;
  • Returns true if the string equals other, except for upper/lowercase distinction.
    – Do not use the == operator to test if two strings are equal! It only determines
    whether or not the strings are stored in the same location. Sure, if strings are in
    the same location, they must be equal. (next page example)

Variable, constant

– must have type : int, long, short, double, float, char, String, boolean, byte, ...
– must have value (assignment before use it)
– name must follow the rules
• (1) Only alphabet, number, under-score _ usable
• (2) cannot start with number
• (3) upper and lower cases must be distinguished
• (4) reserved word (if, while, int, long, …) cannot be used as the variable name
– array name, class name, method name follow the same rule as the variable name
– class variable must be declared within the scope : local or global variables

character constant

– Int n = ‘K’ + ‘M’ + ‘J’ ;
• 75 + 77 + 74 = 226 
• n =226

Type conversion using Casting

  • 자료형 맞춰서 적절하게 써야해

  • 위의 코드는 byte , short에게 감당안돼~숫자 , 결과는 아래와 같아
b : -96
s : -31072

int	boolean	String
int	double	boolean	String
0	0.0	false	null
0	0.0	false	null
100	1.23	true	Java JaBa

Array index out of range

  • Array index out of range : compile will not complain, but execution will

< ArrayIndexOutOfBoundsException 컴파일 에러가 못~ 잡아>

11 105
21 205
31 305
41 405
51 505
0 0
0 0
0 0
0 0
0 0
Exception in thread "main" 
java.lang.ArrayIndexOutOfBoundsException: 
Index 10 out of bounds for length 10
	at lec.op.main(op.java:21)
    (System.out.println(a[i] + " " + b[i]); )

< Array value assignment >

1 1
3 3
5 5
7 7
9 9

< System.arraycopy(srcArray, i, destArray, j, n); >

11 0
13 0
15 13
17 15
19 17
21 0
23 0

Assignment and arraycopy()

  • 배열 값만 복사되는지, 배열 주소 자체가 복사되는지 ( 일케하면 하나 바뀌면 주소 같은 애들 다 바뀌지)
111 111
222 222
333 333
444 444
555 555

30 52 89 16 52 43 20 13 28 90 

<random number count>
10~19 : 2
20~29 : 2
30~39 : 1
40~49 : 1
50~59 : 2
60~69 : 0
70~79 : 0
80~89 : 1
90~99 : 1

Multi-dimensional Array

Three-dimensional Array

0개의 댓글