본문 바로가기

개발/웹 개발

풀스택 26일차

Java 

문자형

 

  • char: 한 글자를 표현, 단일 따옴표(') 사용
  • String: 문자열을 표현, 쌍따옴표(") 사용

 

char var1 = '홍';
String var2 = "홍길동";

정수형

byte는 -128 ~ 127까지 가능

-32,768 ~ 32,767까지 숫자 가능

2,147,483,648 ~ 2,147,483,647까지 숫자 가능

byte var3 = -128;
short var4 = -32768;
int var5 = 123456789;
long var6 = 999999;

실수형

float은 f를 붙여야 하는데 붙이지 않으면 기본적으로 double로 간주됨
double에는 d를 붙이지 않아도 된다

float var7 = 0.1f;
double var8 = 0.00005;

Boolean

true or flase를 입력하여 조건식에서 많이 사용한다

boolean isStart = false;

코드 실행

package firstProject;

public class data {

	public static void main(String[] args) {
		char var1 = '홍';
		String var2 = "홍길동";
		
		byte var3 = -128;
		short var4 = -32768;
		int var5 = 123456789;
		long var6 = 999999;
		
		float var7 = 0.1f;
		double var8 = 0.00005; 
		
		boolean isStart = false;
		
		System.out.println(var1);
		System.out.println(var2);		
		System.out.println(var3);
		System.out.println(var4);
		System.out.println(var5);
		System.out.println(var6);
		System.out.println(var7);
		System.out.println(var8);

		System.out.println(isStart);

	}

}

 

코드 실행 결과
홍길동
-128
-32768
123456789
999999
0.1
5.0E-5
false

 


조건문

  • C언어에서의 조건문과 비슷하게 돌아간다

if-else

public class 조건문 {

	public static void main(String[] args) {
		String lsh = "5";
	
	if(lsh == "5") {
		System.out.println("lsh의 값이 맞습니다.");
	} else {
		System.out.print("lsh의 값이 아닙니다.");

		}
	}

}

↓↓코드 결과

더보기

lsh의 값이 맞습니다.

 

if - else if - else

if - else는 단순하게 맞냐 아니냐의 (2가지)조건을 처리하지만

if - else if - else는 3가지 이상의 조건을 처리한다

 

package firstProject;

public class 조건문 {

	public static void main(String[] args) {
        int score = 84;  

        if (score >= 90) {
            System.out.println("학점: A");
        } else if (score >= 80) {
            System.out.println("학점: B");
        } else if (score >= 70) {
            System.out.println("학점: C");
        } else if (score >= 60) {
            System.out.println("학점: D");
        } else {
            System.out.println("학점: F");
        }
    }
}

↓↓코드 결과

학점: B

연산자

&& (AND)

기능: 두 개의 조건이 모두 참일 때만 참이 된다.

형식: 조건1 && 조건2

 

|| (OR)

 

 

기능: 두 개의 조건 중 하나라도 참이면 참이 된다.

형식: 조건1 || 조건2

 

package firstProject;

public class 연산자 {

    public static void main(String[] args) {
        int a = 1;

        if (a != 0 && a != 1) {
            System.out.println("틀림");
        }

        if (a == 0 || a == 1) {
            System.out.print("맞음");
        }
    }

}

 


for 반복문

아래 코드가 실행되면 "lsh"라는 문자열이 총 10번 출력된다.

for문은 i가 0부터 9까지 총 10번 반복된다.

public class forExam {

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.println("lsh");
		}
	}
}

결과

더보기

lsh

lsh

lsh

lsh

lsh

lsh

lsh

lsh

lsh

lsh

 

 

while 반복문

조건에 만족 하는 동안 특정 값을 출력하는 반복문

package firstProject;

public class whileExam {

	public static void main(String[] args) {
		int i = 0;
		while (i < 10) {
			System.out.println(i);
			i++;
		}
	}
}

결과

더보기

1

2

3

4

5

6

7

8

9

10

 

println이 아닌 print로만 하면 12345678910이 출력된다

 

'개발 > 웹 개발' 카테고리의 다른 글

풀스택 28~29일차  (1) 2024.09.06
풀스택 27일차  (0) 2024.09.03
풀스택 25일차  (0) 2024.09.02
풀스택 24일차  (0) 2024.08.30
풀스택 23일차  (1) 2024.08.28