✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

  1. CH3

Operator

Operators

Type

Operator

산술 연산자

+ - * / %

<< >> (bit operator)

비교 연산자

>< >= <= == !=

논리 연산자

&& || !

& | ^ ~ (bit operator)

대입 연산자

=

기타

(type) : 형변환 연산자

?: 삼항연산자

instanceof (객체지향 개념)

Priority of Operator

  1. 산술 > 비교 > 논리 > 대입

  2. 단항 > 이항 > 삼항

  3. 단항 연산자(부호연산자 등)와 대입 연산자 : 오른쪽에서 왼쪽으로 연산. 이를 제외한 모든 연산의 진행방향은 왼쪽에서 오른쪽이다.

증감 연산자

Type

Description

Example

전위형

값이 참조되기 전에 증가시킨다.

j = ++i;

후위형

값이 참조된 후에 증가시킨다.

j=i++;

증감연산자가 다른 연산자와 함께 식에 사용된다면 전위형과 후위형의 결과는 차이가 있지만, 독립적으로 사용된 경우는 차이가 없다.

//+와 i순서를 유심히 보면 알 수 있다!
//j = ++i;
i = i+1;//먼저 증가시킨다음에 대입한다!
j = i;

//j = i++;
j = i;//먼저 대입하고 증가시킨다!
i = i+1;

Automatic Type Transition(자동 형변환)

기존의 값을 최대한 보존할 수 있는 타입으로 자동 형변환된다.

float f = 1234;//OK. float > int 때문에 형변환하지 않아도 컴파일러가 해준다.
float f = (float)1234;

int i = 3.14f;//int < float. 값 손실이 발생한다. error!
int i = (int)3.14f;//OK. 수동형변환 필요.
//리터럴이기 때문에 가능
byte b = 100;//byte의 범위(-128 ~ 127) 안에 있기 때문에 에러 없이 자동 형변환 가능.

int i = 100;
byte b = i;//error. int형 변수의 값을 모르기 때문에 에러로 발생!
byte b = (byte)i;//수동형변환 필요!

//리터럴이라도 값손실 발생하는 경우, 컴파일러가 자동 형변환해주지 못하기 때문에 직접 형변환 필요!
int i = 1000;
byte b = (byte)1000;//수동형변환은 했지만 값손실 발생하여 b에는 -24가 저장된다.

사칙 연산자, 산술 변환

  1. 두 피연산자의 타입을 같게 일치시킨다.(보다 큰 타입으로 일치)

long(8) + int(4) -> long + long -> long//피연산자 하나라도 long이면 결과도 long
float(4) + int(4) -> float + float -> float//float이 실수형이라 범위가 더 넓기 때문에.
double(8) + float(4) -> double + double -> double

2. 피연산자의 타입이 int보다 작은 타입이면 int로 변환된다.

byte(1) + short(2) -> int + int -> int
char(2) + short(2) -> int + int -> int

char(2) - char(2) -> int - int -> int
'2' - '0' -> 2(int)

실수하기 쉬운 overflow 발생 : int * int = int인데 +_20억을 초과하면 long타입으로 형변환을해줘야한다!

int a = 1_000_000;
int b = 2_000_000;

//int * int 는 int인데 a*b 값은 int범위를 넘기 때문에 overflow발생!
long c = a * b;
//다음과 같이 고쳐져야한다.
long c = (long)a*b;
long c = (long)a * b;//(1번 설명 참고)둘 중 하나에 long형변환 해주면 long으로 되기 때문에 하나만 해주면 된다.

나머지 연산자 %

나누는 피연산자는 0이 아닌 정수만 허용되고 부호는 무시된다.

10%8과 10%-8은 같은 결과를 얻는다.

  • 비교연산자, 문자열의 비교

두 피연산자를 비교해서 참(true) 또는 거짓(false)을 반환한다.

'A' > 'B' => 65 > 66 => false

문자열을 비교할 땐 equals() 메소드를 사용한다.

String str1 = "abc";
String str2 = "abc";
System.out.println(str1 == str2);//true
System.out.println(str1.equals(str2));//true
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(str1 == str2);//false
System.out.println(str1.equals(str2));//true
  • 논리 연산자 && ||

: 조건식을 연결할 때 사용하는 연산자.

i는 2의 배수 또는 3의 배수이지만 6의 배수는 아니다.

(i%2 == 0 || i%3 == 0) && i%6 != 0

사용자로부터 입력된 문자ch가 숫자('0' ~ '9')인지 확인하는 식은 다음과 같이 쓸 수 있다. 자주 쓰이는 조건식이기 때문에 살펴보도록 하자. 유니코드에서 문자 '0'부터 '9'까지 연속적으로 배치되어 있기 때문에 가능한 식이다.

'0' <= ch && ch <= '9'//48 <= ch <= 57

문자

문자코드

'0'

48

'1'

49

'2'

50

'3'

51

'4'

52

'5'

53

'6'

54

'7'

55

'8'

56

'9'

57

문자 ch가 대문자 또는 소문자인지 확인하는 식은 다음과 같이 쓸 수 있다.

('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')

문자 ch가 소문자가 아니다.

//ch < 'a' || ch > 'z'
!('a' <= ch && ch <= 'z')
  • 조건 연산자 ?:

삼항연산자 👉🏻 조건식 ? 식1 : 식2

참이면 식1, 거짓이면 식2가 실행된다.

삼항연산자가 if문보다 간편하기 때문에 사용된다.

result = (x > y) ? x : y;
if(x > y)
    result = x;
else
    result = y;
  • 대입 연산자

lvalue(left value) : 대입 연산자의 왼쪽 피연산자

rvalue(right value) : 대입 연산자의 오른쪽 피연산자

  • 복합 대입 연산자

Operator

Equals

i += 3;

i = i+3;

i -= 3;

i = i-3;

i *= 3;

i = i*3;

i /= 3;

i = i/3;

i %= 3;

i = i%3;

i <<= 3;

i = i<<3;

i >>= 3;

i = i>>3;

i &= 3;

i = i&3;

i ^= 3;

i = i^3;

i |= 3;

i = i|3;

i *= 10 +j;

i = i * (10+j); //괄호 필수!어떤 값을 계속적으로연산할 것인지 인지!

//i에 (10+j)를 계속 곱한다!

//i = i * (10+j);

PreviousObject ArrayNext조건문과 반복문

Last updated 4 years ago

Was this helpful?