숫자, 문자, 문자열의 차이를 통해 데이터 타입이 중요한 이유를 설명
package operator;
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"*"8"); //에러남 => 문자열끼리는 연산자로 계산이 불가능
System.out.println("1111".length()); //4 => .length() 는 문자열의 길이를 알려줌
System.out.println(1111.length()); //에러남 => .length()를 쓸 수 있는 타입이 아님
System.out.println('1111'.length()); //에러남 => ''는 문자를 의미, 숫자가 아님
}
}
*String은 문자열을 의미 != 문자
사칙연산 연산자사용과 Math클래스 클래스메소드기능
package operator;
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
//Math.~~ Math클래스의 메소드 기능
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
}
}
package operator;
public class StringApp {
public static void main(String[] args) {
System.out.println("Hello World"); //String " " 를쓰면 문자열
System.out.println('H'); //character ' ' 를쓰면 문자
System.out.println("H"); //String
System.out.println("Hello "
+ "World"); // 문자열과 문자열이 라인이 달라졌다고해서 줄바꿈이 되는게 아님
System.out.println("Hello \nWorld"); //역슬래쉬 \n 줄바꿈기능
}
}
문자열 다루기
package operator;
public class StringOperation {
public static void main(String[] args) {
System.out.println("Hello World".length()); //11
System.out.println("Hello, Claire ... bye.".replace("Claire", "Sully"));
System.out.println("Hello, [Claire] ... bye.".replace("[Claire]", "Sully"));
// 두개의 결과같은 같은데, 이름란을 []로 구분하고있어서 눈으로 보기에 편하니까 [] 했음
}
}
'괴발개발 > Java' 카테고리의 다른 글
Java생활코딩_입력과 출력 (0) | 2021.06.07 |
---|---|
Java생활코딩_디버거 (0) | 2021.06.05 |
Java생활코딩_객체선언, 메소드호출 (0) | 2021.06.03 |
Java생활코딩_데이터타입, 변수, 변수선언, 타입변환 (0) | 2021.06.03 |
Java생활코딩_Hello World 콘솔 출력하기 (0) | 2021.06.02 |