자바 - 데이터타입/기본문법
Java

자바 - 데이터타입/기본문법

반응형

자바 - 데이터타입/기본문법





1) primitive type 기본형
  1. 논리형 : boolean (참/거짓)

  2. 숫자형

    a. 정수형

    • byte(1)

    • short(2)

    • int(4)

    • long(8)

    • char(2) : 글자 한개 저장 목적

    b. 실수형

    • float(4)

    • double(8)


public class DatatypeTest {

public static void main(String[] args) {
//논리형
boolean flag = true;
// byte < short < int < long
// float < double

//정수형
byte b = 1; // 바이트는 -128~127까지만 저장 가능
int b2 = b + 1; // 자바에서 정수리터럴을 사용해서 연산할 때 그 값은 4byte에 저장됨
b = (byte) (b+1); // type 변환
short s = 123;
int i = 88888;
long l = 12345678;
char c = 'x';

//실수형
float f = 2.1f;
double d = 3.14; // 자바에서 실수리터럴을 사용할 때 그 값은 8byte에 저장됨
d = f;

System.out.println("b = " + b);
System.out.println("s = " + s);
System.out.println("i = " + i);
System.out.println("l = " + l);
System.out.println("c = " + c);
System.out.println("f = " + f);
System.out.println("d = " + d);
}

}

2) reference type 참조형

class(String 포함), interface, array, enum 등등

  • 연산자

public class OperatorTest {

public static void main(String[] args) {
boolean a = true, b = false, c = false;
int x = 10, y = 3;
System.out.println("result = " + (x + y));

int result = x / y;
System.out.println(result);
result = x % y;
System.out.println(result);

if (a && b) // and
System.out.println("a && b");
if(a || c) // or
System.out.println("a || c");

}
}

  • 조건문 If

public class IfTest {

public static void main(String[] args) {
int a = 88, b = 85;

System.out.println(a > b ? "ok" : "cancel");

// 삼항
if(a==b)
System.out.println("level 3");
else
System.out.println(a > b ? "level 1" : "level 2");

System.out.println((a == b)?"level 3" : (a>b)? "level 1":"level 2");
// if else
if ( a > b ) {
System.out.println("level 1");
} else if (a < b) {
System.out.println("level 2");
} else {
System.out.println("level 3");
}
}

}

  • Switch

public class SwitchTest {

public static void main(String[] args) {
int score = 93;
char c = 'a'; // byte, short, int, char, string

/*
switch (score) {
case 100:
System.out.println("A");
break;
case 90:
System.out.println("B");
break;
case 80:
System.out.println("C");
break;
case 70:
System.out.println("D");
break;

default:
System.out.println("F");
break;
}
*/

switch (c) {
case 'a':
case 'A':
System.out.println("A");
break;
case 'b':
case 'B':
System.out.println("B");
break;
case 'c':
case 'C':
System.out.println("C");
break;
case 'd':
case 'D':
System.out.println("D");
break;

default:
System.out.println("F");
break;
}
}

}

  • 반복문(for, while)

public class LoopTest {

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

int j = 0;
while(j < 5) {
System.out.println("welcome, \t");
j++;
}
System.out.println();

String[] names = {"tommy", "bill", "jane"};
for(String name : names) {
System.out.println(name + " ");
}
}
}

별찍기

for (int i = 0; i < 5; i++) {
for(int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}

for (int i = 1; i <= 5; i++) {
for(int j = 5; j >= 1; j--) {
if(i < j) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}


반응형

'Java' 카테고리의 다른 글

[Java] 직렬화(Serialization)  (0) 2021.07.04
[Java] 컴파일 과정  (1) 2019.06.10
[Java] GUI를 활용한 직원 관리 프로그램  (1) 2019.02.17
[자바(java)/스프링(spring)] 면접 질문 모음  (1) 2019.02.13
[Java] 필수 개념 정리  (1) 2019.01.20