Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

dearbeany

[Java] 자바 기초 02 | 연산자, 비교와 Boolean, 조건문, 논리 연산자 본문

Java

[Java] 자바 기초 02 | 연산자, 비교와 Boolean, 조건문, 논리 연산자

dearbeany 2022. 7. 5. 08:58

연산자 - 형변환

int a = 10;
int b = 3;

float c = 10.0F;
float d = 3.0F;
		
System.out.println(a/b);	// 3 		 정수/정수 -> 10/3
System.out.println(c/d); 	// 3.3333333 실수/실수 -> 10.0/3.0
        
// 자바는 보다 넓은 수를 표현할 수 있는 데이터 타입으로 변환됨
// 즉, int 정수형 보다 float인 실수형으로 통일 
System.out.println(a/d);	// 3.3333333 정수/실수 -> 10/3.0

 

 

단항연산자

int i = 3;
i++;
System.out.println(i); // 4 출력

++i;
System.out.println(i); // 5 출력
System.out.println(++i); // 6 출력
System.out.println(i++); // 6 출력
System.out.println(i); // 7 출력

 

Boolean

- 무수한 값을 가지는 다른 데이터 타입들과 다르게 값 2개(true, false)만을 가지는 데이터 타입

 

 

.equlas

- 문자열을 비교할 때 사용하는 메소드

String s1 = "Hello world";
String s2 = new String("Hello world");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true

 

 

조건문 - switch

- break를 만나면 switch 문 실행이 즉시 중지

- 만약 break; 문이 없다면 모두 실행

System.out.println("switch(1)");

switch(1){
case 1:
	System.out.println("one");
    // break;
case 2:
	System.out.println("two");
    // break;
case 3:
	System.out.println("three");
    // break;
}

// switch(1)
// one
// two
// three

 

- 가장 마지막은 default로 끝난다. 즉 주어진 케이스가 없는 경우 default 문이 실행

 

 

 

논리연산자

- and, or, not