데이터 타입 ( 숫자형, 문자형 ), 변수, casting, toString
본문 바로가기
programming/Java

데이터 타입 ( 숫자형, 문자형 ), 변수, casting, toString

by 코딩하는 핑가 2020. 11. 4.
반응형

* 본 포스팅은 생활코딩님의 자바1 수업을 학습하면서 정리한 내용입니다.

* 주소

- opentutorials.org/module/4294

- www.youtube.com/playlist?list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR

 

< File - new - java project > Data_and_operation 패키지 생성

1. < File - new - file > Datatype 생성

public class Datatype{
	public static void main(String[] args) {
		System.out.println(6); // Number
		System.out.println("six"); // String
		
		System.out.println("6"); // String
		
		System.out.println(6+6); // 더하기 연산
		System.out.println("6+6"); // 결합 연산자
		
		System.out.println(6*6);
//		System.out.println("6"*"6");
		
		System.out.println("1111".length());
//		System.out.println(1111.length);
	}
}

- 데이터 타입을 구분하는 이유

사람은 숫자 0과 문자 0을 구분하여 의미에 맞게 사용하지만, 컴퓨터는 0 자체를 구분하지 못하기 때문에 데이터 타입으로 구분해줘야 한다. 그래서 숫자 타입은 " " 존재하지 않게, 문자 타입은 " " 존재하게 약속했다.

 

2. < File - new - class > Number 생성

public class Number {

	public static void main(String[] args) {
		
		// Operator
		System.out.println(6 + 2); // 8
		System.out.println(6 - 1); // 4
		System.out.println(6 * 2); // 12
		System.out.println(6 / 2); // 3
		
		System.out.println(Math.PI); // 3.141592653589793
		System.out.println(Math.floor(Math.PI)); // 3.0
		System.out.println(Math.ceil(Math.PI)); // 4.0

	}

}

- 숫자형 데이터 타입은 + - * / 사칙연산이 가능하다.

- Math에서 수학과 관련된 여러가지 작업을 진행할 수 있다.

- floor : 내림, ceil : 올림

 

3. < File - new - class > StringApp 생성

public class StringApp {

	public static void main(String[] args) {
		
		// Character vs String
		System.out.println("Hello world"); // String
		System.out.println('H'); // Character
		System.out.println("H"); // String
		
		// 줄바꿈
		System.out.println("Hello" 
				+ "World"); // 화면에서 줄바꿈이 됐다고 줄바꿈이 된 게 아님
		
		// new line
		System.out.println("Hello \nWorld");
		
		// escape
		System.out.println("Hello \"World\""); // Hello "World"

	}

}

- 문자열 표현

" " 문자열 (String), ' ' 문자 (Character), \n 줄바꿈 (new Line) \ 뒤따라오는 문자(열)을 분리하여 출력 (escape)

 

4. < File - new - class > StringOperation 생성

public class StringOperation {

	public static void main(String[] args) {
		System.out.println("Hello World".length()); // 11
		System.out.println("Hello, [[name]] ... bye. ".replace("[[name]]", "egoing")); // Hello, egoing ... bye. 

	}

}

- replace - 문자열 바꿔주는 역할을 함

- length - 문자열 길이를 알려줌

 

5. < File - new - class  > Variable 생성

public class Variable {

	public static void main(String[] args) {
		
		int a = 1; // Number -> Integer (정수) ... -2, -1, 0, 1, 2 ...
		System.out.println(a);
		
		double b = 1.1; // real number -> double ... -2.0, -1.0, 0, 1.0, 2.0 ... 
		System.out.println(b);
		
		String c = "Hello World";
		System.out.println(c);

	}

}

- 데이터 타입을 지정해주는 이유

각 변수마다 어떤 데이터가 들어가는지 명시적으로 알기 위함

- 변수 선언 시 데이터 타입의 종류 중 가장 자주 사용되는 것은 int (integer) 정수범위, double 실수범위, String 문자열이 있다.

 

5. < File - new - class > Letter 생성

public class Letter {

	public static void main(String[] args) {
		String name = "egoing";
		System.out.println("Hello, "+name+" ... egoing ... bye");
		
		double VAT = 10.0;
		System.out.println(VAT);

	}

}

- 변수의 효용성

변수를 사용하면 여러 번 반복되는 개념을 효율적으로 수정이 가능하기 때문에 변수 이름은 누가 봐도 알 수 있게 짓는 것이 매우 중요하다.

 

6. < File - new - class > Casting 생성

public class Casting {

	public static void main(String[] args) {
		
		double a = 1.1;
		double b = 1;
		System.out.println(b);
		
		//int c = 1.1; // 1.1은 소숫점, int는 정수형 데이터타입
		double d = 1.1;
		int e = (int) 1.1;
		System.out.println(e);
		
		// 1 to String
		String f = Integer.toString(1);
		System.out.println(f.getClass()); // f .getClass() : 데이터 타입을 알려줌 

	}

}

- 데이터 타입의 변환 (Casting)

실수(double) 타입으로 지정된 변수의 데이터가 정수(int) 일 땐 손실이 일어나지 않으므로 자동적으로 1.0이 되지만,

정수(int) 타입으로 지정된 변수의 데이터가 실수(double) 1.5 같은 데이터 일 때 정수로 바꿔지려면 손실이 일어나 자동적으로 바뀌지 않는다. 그래서 바꾸는 방법은 첫 번째, 데이터 타입을 double로 변경하거나, 두 번째, 데이터 앞에 int를 쓰는 Casting 작업을 해야한다. 

Casting : 손실이 일어나더라도 데이터 타입을 바꿔주는 역할을 함

- 숫자형을 문자형으로 변환 (toString)

 

* 수업 소스

github.com/sso-jeong/JAVA.git

 

sso-jeong/JAVA

Contribute to sso-jeong/JAVA development by creating an account on GitHub.

github.com

반응형

댓글