본문 바로가기
java

쉽게 배우는 자바 8-2~8-3 변수, 데이터 타입 변환

by Gyona 2023. 1. 21.

8-2 변수의 효용

 

public class Letter {

public static void main(String[] args) {

String name = "leezche";

System.out.println("Hello, "+name+" ... "+name+" ... egoing ..bye");

 

double VAT = 10.0;

System.out.println(VAT);

}

}

 

namer과 double을 선언해주면서 쉽게 코딩을 할수 있다.

변수만 변경해주면 결과값이 달라진다.독잡하고 어려운 코드일수록 변수를 잘 선언해주면 코드변환이 쉽다.

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ8-3 데이터 타입 변환 - casting

public static void main(String[] args)  체크하면 자동으로 작성되는 편리함을 잊지말자 ㅎㅎ

public class Casting {

public static void main(String[] args) {

 

double a =1.1;

double b = 1; //정수지만 double이 오류가 나지 않는다.

System.out.println(b);

 

// int c = 1.1; //오류가난다!

double d = 1.1;

int e = (int) 1.1; //강제로 int로 만들어줌

System.out.println(e);

}

}

강제로 int로 만들었기 때문에 1이라는 결과값이 나옴

 

java int to string casting  검색하면

String strI = Integer.toString(i);

String f = Integer.toString(1); //f를 int 1로 바꿔주는 코드

System.out.println(f.getClass()); //f가 어떤 타입인지 알려줌

결과는    class java.lang.String