국비지원 자바과정 2일

DeokHun KIM·2022년 6월 30일
0

java

목록 보기
3/30

System. 여기서 .의 의미는 시스템(클래스)에 있는~
system.out 시스템에 있는 아웃
소문자로 시작하는 단어옆에 소괄호가 붙어있다면 메소드라고 한다 ex. system.out.println("Hello");

소문자 뒤 소괄호()는 변수 혹은 메소드

println은 라인 단위 출력

[] = 배열 ,args = 변수명

.상수(constant): 값이 변하지 않음
.변수(variable): 값이 변하는 것
.데이터(data)의 종류: 문자,문자열,숫자,날짜
.기본 데이터타입(primitive)-8개 / 참조형 데이터타입(reference)-무한대
8가지
논리형 boolean (true,false)
문자형 char
정수형 byte, short, int, long
(1byte 2byte 3byte 4byte)
실수형 float, double
(4byte 8byte)

"A"; 는 문자열
'A'; 는 문자

ASCII: 미국에서 표준화가 추진된 정보교환용 7비트 부호

02_Primitive

Ex01_boolean.java

public class Ex01_boolean {

	public static void main(String[] args) {
		// boolean : true, false 값을 가짐(논리형)
		// 사용자타입지정 사용변수명 = 초기값;
		// 타입 변수명;
		// 타입 변수명 = 초기값;
		boolean bool1 = true; //변수 선언 + 초기값 설정
		boolean bool2; //변수 선언
		bool2 = false; //초기값 설정
		
		System.out.println("bool1 : "+ bool1);
		System.out.println("bool1 : "+ bool2);
		System.out.println("--------------");
		
		bool1 = false;
		if (bool1) {
			System.out.println("if문 : bool1 참(true)이면 실행");
		} else {
			System.out.println("if문 : bool1 거짓(false)이면 실행");
		}
		
		System.out.println("=== main 끝 ===");
	}

}

bool1 : true
bool1 : false

if문 : bool1 거짓(false)이면 실행
=== main 끝 ===

Ex02_char.java

public class Ex02_char {

	public static void main(String[] args) {
		// 문자형(char) : 2 byte사용, 문자 1글자 저장(65536개)
		// 자바 문자처리방식은 유니코드(unicode) 형태
		char char1 = 'A'; // "A" 는 문자열(String) , 'A'는 문자(char)
		System.out.println("char1 : " + char1);
		System.out.println("char1 정수값 : " + (int)char1); //int 타입으로 형변환
		System.out.println("char1 다음문자 : " + (char)(char1 + 1));
		//65+1=66 -> char타입 형변환을 가리키는 문자가 출력
		
		char char2 = '한';
		System.out.println("char2 : " + char2);
		
		char char3 = '\uAC00'; // 가
		System.out.println("char3 : " + char3);
		
		char3 = '\uAC01'; // 각
		System.out.println("char3 : " + char3);
		
		// 16진수 표기법
		//                             10 11 12 13 14 15
		// 16진수 : 0 1 2 3 4 5 6 7 8 9 A  B  C  D  E  F  
		// 16진수 : 0 1 2 3 4 5 6 7 8 9 a  b  c  d  e  f  (소대문자 상관없음)
		int hex = 0xa; //16진수 표기 0x, 0X 접두어 사용
		System.out.println("> hex : " + hex);
		
		int binary = 0b1010; // 2진수 표기 0b, 0B 접두어 사용
		System.out.println("> binary : " + binary);
		
		int octal = 012; // 8진수 표기 0 접두어 사용
		System.out.println("octal : " + octal);
		
		
		
	}

}

char1 : A
char1 정수값 : 65
char1 다음문자 : B
char2 : 한
char3 : 가
char3 : 각

hex : 10
binary : 10
octal : 10


Ex03_byte

public class Ex03_byte {

	public static void main(String[] args) {
		// 정수형 byte : 정수 숫자 저장 256개(-128 ~ 127)
		byte byte1 = 100;
		byte byte2 = 28;
		
		System.out.println("byte1 : " + byte1);
		System.out.println("byte2 : " + byte2);
		
		//연산 결과값이 표현범위를 벗어나면 예상치 못한 쓰레기 값이 될 수 있음
		byte byte3 = (byte)(byte1 + byte2);	
		System.out.println("byte1 + byte2 : " + byte3);
		
		// =============================
		System.out.println("---- byte 최소, 최대값 ----");
		System.out.println("byte 최소값 : " + Byte.MIN_VALUE); //소괄호가없으니 메소드가 아닌 변수이다
		System.out.println("byte 최소값 : " + Byte.MAX_VALUE); 

	}

}

byte1 : 100
byte2 : 28
byte1 + byte2 : -128
---- byte 최소, 최대값 ----
byte 최소값 : -128
byte 최소값 : 127


Ex04_short

public class Ex04_short {

	public static void main(String[] args) {
		// 정수형 short : 2 byte(갯수: 65536, 범위: -32768 ~ 32767)
		short short1 = 20000;
		short short2 = 30000;
		System.out.println("short1 : " + short1);
		System.out.println("short2 : " + short2);
		
		// 연산 결과 값이 저장범위를 벗어나면 잘못된 데이터 사용가능성 있음
		short sum = (short) (short1 + short2);
		System.out.println("short sum : " + sum);
		
		// 연산결과 값 정상 사용 
		int sumInt = short1 + short2;
		System.out.println("int sumInt : " + sumInt);
		
		// =============================
		System.out.println("---- byte 최소, 최대값 ----");
		System.out.println("byte 최소값 : " + Short.MIN_VALUE);
		System.out.println("byte 최소값 : " + Short.MAX_VALUE); 
	}

}

short1 : 20000
short2 : 30000
short sum : -15536
int sumInt : 50000
---- byte 최소, 최대값 ----
byte 최소값 : -32768
byte 최소값 : 32767

Ex05_int

public class Ex05_int {

	public static void main(String[] args) {
		// 정수형 int : 4 byte, 범위 (-2,147,483,648 ~ 2,147,483,647)
		System.out.println("int 최소값 : " + Integer.MIN_VALUE);
		System.out.println("int 최소값 : " + Integer.MAX_VALUE);
		
		int num1 = 70000;
		int num2 = 30000;
		int sum = num1 + num2;
		
		System.out.println("num1 : " + num1);
		System.out.println("num2 : " + num2);
		
		//사칙연산 부호 : +, -, *, /
		//나머지 구하기 : %
		System.out.println("num1 + num2 = " + sum);
		System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
		System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
		System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
		System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
		
		// % 연산자 : 나누기 연산 후 남은 나머지 값을 구함
		System.out.println(num1 + " % " + num2 + " = " + (num1 % num2));
		
		//-----------------------
		// 0으로 나누는 경우 (100 / 0) : 예외발생(오류)
		int num3 = 10;
		
		System.out.println(num1 / num3);
		
		System.out.println("=== 프로그램의 끝 ===");
		
	}

}

int 최소값 : -2147483648
int 최소값 : 2147483647
num1 : 70000
num2 : 30000
num1 + num2 = 100000
70000 + 30000 = 100000
70000 - 30000 = 40000
70000 * 30000 = 2100000000
70000 / 30000 = 2
70000 % 30000 = 10000
7000
=== 프로그램의 끝 ===

Ex06_long

public class Ex06_long {

	public static void main(String[] args) {
		// 정수형 long : 8 byte
		// 범위 : -9223372036854775808 ~ 9223372036854775807
		// long 타입값 명시적 표현 : 숫자 + L 또는 l(소문자 엘)
		System.out.println("long 최소값 : " + Long.MIN_VALUE);
		System.out.println("long 최소값 : " + Long.MAX_VALUE);
		long num1 = 20000000000L; // long타입 접미어 사용
		long num2 = 30000000000L;
		long sum = num1 + num2;
		
		System.out.println(num1 + " + " + num2 + " = " + sum);
		System.out.println("=================================");
		
		byte numByte = 100;
		short numShort = 3000;
		int numInt = 3000000;
		long numLong = 5000000000000000L;
		
		numByte = (byte) numShort; //손실발생 - 쓰레기값 발생
		System.out.println("numByte : " + numByte);
		
		numLong = numInt; // long 타입 <- int 타입 : 큰타입에 작은 타입 저장
		
	}

}

long 최소값 : -9223372036854775808
long 최소값 : 9223372036854775807
20000000000 + 30000000000 = 50000000000

numByte : -72


Ex07_float

public class Ex07_float {

	public static void main(String[] args) {
		// 실수형 float : 4 byte (범위 : 1.4E-45 ~ 3.4028235E38)
		// 소수점 이하 데이터 저장 가능
		// 표시형식 : 숫자.숫자 + f 또는 F
		// 단, 소수점 이하 6자리까지만 정확도 보장
		float f1 = 3.4f;
		float f2 = 3.2F;
		System.out.println("float 3.4f : " + f1);
		System.out.println("float 3.2f : " + f2);
		System.out.println("3.4f + 3.2f : " + (f1 + f2)); // 6.6000004 오차 약간 발생
		System.out.println("3.4f + 3.2f : " + (3.4f + 3.2f)); // 6.6000004
		System.out.println("3.4f + 3.2f : " + (3.4f + 3.1f)); // 6.5
		
		System.out.println("float 최소값: " + Float.MIN_VALUE);
		System.out.println("float 최대값: " + Float.MAX_VALUE);
	}

}

float 3.4f : 3.4
float 3.2f : 3.2
3.4f + 3.2f : 6.6000004
3.4f + 3.2f : 6.6000004
3.4f + 3.2f : 6.5
float 최소값: 1.4E-45
float 최대값: 3.4028235E38


Ex08_double

public class Ex08_double {

	public static void main(String[] args) {
		
		// 실수형 double : 8 byte(범위 : 4.9E-324 ~ 1.7976931348623157E308)
		// 표시형식 : 숫자.숫자 / 숫자.숫자 + D 또는 d
 		System.out.println("double 최소값 : " + Double.MIN_VALUE);
		System.out.println("double 최대값 : " + Double.MAX_VALUE);
		
		double d1 = 1.1;
		double d2 = 1.12345678901234567890d;
		System.out.println("double 1.1 : " + d1);
		System.out.println("double 1.12345678901234567890d : " + d2); // 소수점 15번째 이후로 정확도 보장 할 수없다
	
		double sum = d1 + d2;
		System.out.println("d1 + d2 : " + sum);
		
		System.out.println("3.4d + 3.2d : " + (3.4d + 3.2d)); //float과 다르게 double은 오차없이 나옴
		//=====================================
		System.out.println("===================");
		float f1 = 3.4f;
		d1 = f1; //double타입에 float타입을 넣는 경우 자동형변환
		
	}

}

double 최소값 : 4.9E-324
double 최대값 : 1.7976931348623157E308
double 1.1 : 1.1
double 1.12345678901234567890d : 1.1234567890123457
d1 + d2 : 2.223456789012346
3.4d + 3.2d : 6.6


1개의 댓글

comment-user-thumbnail
2022년 7월 4일

int는 4바이트, long은 8바이트입니다
3바이트, 4바이트가 아니에요~

답글 달기