생활코딩

[생활코딩] 6. 데이터 타입

주니어주니 2022. 8. 8. 15:53
  • 6.2. 데이터 타입
  • 숫자, 문자같은 데이터를 코드로 표현하는 방법
  1. 프로젝트 생성 (Data_and_operation)
  2. 파일 생성 (Datatype.java)
  3. public class 다음에 오는 부분 (ex. Datatype)은 파일명과 똑같아야 함. 안그러면 오류
  4. system.out.println(); 바로 나오게 하는 방법 : sysout 입력 후 ctrl+space
  5. 숫자를 표현하는 방법 ( // Number ) : (6)
    문자열을 표현하는 방법 ( // String) : ("six")
    if) ("6") --> 숫자 6이 아닌, 문자열 6임 !!
💡 데이터 타입을 구분하는 이유는 데이터 타이별로 어울리는 연산방법이 있기 때문에 엄격하게 구분함.
 
* 생각해야할 점
1) 어떤 종류의 데이터 타입이 있나
2) 각각 데이터 타입별로 어떤 연산방법이 존재하나 

 

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);  //12
		System.out.println("6"+"6");  //66
		
		System.out.println(6*6);  //36
//		System.out.println("6"*"6"); // 문자열을 곱하는 기능은 없음
//      at Datatype.main(Datatype.java:12) ->12번째 줄에 오류가 있다 
		
		System.out.println("1111".length()); //4 (문자열의 길이를 나타냄)
//		System.out.println(1111.length());
	}
}

 

  • 6.3. 숫자와 연산
  • 수를 다루는 방법
 💡 파일(class) 간편하게 만드는 꿀팁 :
     프로젝트에 대고 오른쪽 버튼 → New → Class → name: 파일명 (ex. Datatype / Number)
     → method 중 (public static void main(String[] args) 체크

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

수학적 계산이 필요한 경우 .math 를 이용해 여러가지 작업이 가능하다.

파이를 알고싶을 경우: (Math.PI)

소수점 내림을 하고싶을 경우: (Math.floor(Math.PI))

소수점 올림을 하고싶을 경우: (Math.ceil(Math.PI))

public class Number {

	public static void main(String[] args) {
		// Operator(연산자)
		System.out.println(6 + 2);  // 8
		System.out.println(6 - 2);  // 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 (floor:내림), 이클립스에만 있는 기능
		System.out.println(Math.ceil(Math.PI)); // 4.0 (ceil:올림)
		
		System.out.println(Math.random()); // 랜덤값
		System.out.println(Math.addExact(98, 360)); // (a+b)의 값
		
	}

}

 

  • 6.4. 문자열의 표현
  • String vs Character

"Hello World" // String 문자열 (character들이 모여있는 것)

'Hello World' // Character 문자 (한글자를 표현하는 특수한 데이터타입). 쓰지 마라

1) 줄바꿈을 하고싶을 때
"Hello"
+ "World" 이렇게 하면 줄바꿈이 안됨. 그냥 문자열을 두개로 나누는 것

—> "Hello |nWorld"     // |n(new line) 줄바꿈 기호

 

2) Hello "World" 를 표현하고 싶을 때
"Hello "World""
: ""문자열의 끝과 끝이므로 "안의 "를 인식 못함

"Hello |"World|""
뒤에 따라오는 기호를 일반 문자열로 만들어주는 것 ( | 역슬래쉬)
→ escape: 뒤에 따라오는 문자의 임무를 일시적으로 해방시키는것

 

public class StringApp {

	public static void main(String[] args) {
	
		System.out.println("Hello World"); //String 문자열 
		System.out.println('H'); // Character 문자 ('는 특수한 데이터타입(한글자) 가리킴)
		System.out.println("H");
		
		System.out.println("Hello"
				+ " World");    // 이렇게 하면 줄바꿈이 안됨
		
		// new line
		System.out.println("Hello \nWorld");  //\n: new line 줄바꿈 기호
		
		// escape 
		System.out.println("Hello \"World\""); // "안의 "를 인식 못함 \"로 해줘야 함

	}

}

 

* 재밌는거 발견 

public class StringApp {
	public static void main(String[] args) {
		System.out.println("*      *\n *    *\n  *  *\n   **");
	}
}

 

  • 6.5. 문자열 다루기

1) 문자열의 개수

"Hello World" .length()

2) 예를 들어, 받는 사람의 이름만 바꿔서 똑같은 내용의 메시지를 보내고 싶을 때

"Hello, [[[name]]] blah blah bye." .replace("[[[name]]]", "egoing"))

 

∴문자열과 관련하여 자바에 내장되어 있는 여러가지 기능들을 활용하여 다양한 문제를 해결할 수 있다.

쉣! 이런것도 자바로 할 수 있다니 ㅜ 미친...

public class StringOperation {

	public static void main(String[] args) {
		
		System.out.println("Hello World".length()); //11
		
		System.out.println("Hello, [[[name]]]. this is coding company.[[[name]]] got our job." .replace("[[[name]]]", "junhee"));
		//Hello, junhee. this is coding company.junhee got our job.

		System.out.println("Ross and Rachel met and Ross and Rachel are engaged" .replaceFirst("Ross", "Joy"));
		//Joy and Rachel met and Ross and Rachel are engaged (변경하고자 하는 문자열이 처음 해당할 때만 치환)

		System.out.println("We were on a break!" .toUpperCase());
		//WE WERE ON A BREAK! (문자 전체를 대문자 % 소문자(toLowerCase)) 
		
	}
}

 

System.out.println("they don't know that we know that they know");  
// they don't know that we know that they know

System.out.println("they don't know that we know that they know".replace("they","pheebe");
// pheebe don't know that we know that pheebe know

System.out.println("they don't know that we know that they know".replace("they","pheebe").replace("we","monica"));
// pheebe don't know that monica know that pheebe know

'생활코딩' 카테고리의 다른 글

[생활코딩] 10. 디버거  (0) 2022.08.08
[생활코딩] 9. 프로그래밍  (0) 2022.08.08
[생활코딩] 8. 변수  (0) 2022.08.08
[생활코딩] 5. Hello Java World  (0) 2022.08.08
[생활코딩] 4. 실행  (0) 2022.08.08