2-1. 1광년 거리 계산하기
double 형 변수를 이용하여 빛이 1년 동안 진행하는 거리를 계산하여 보자
package ch02; public class Light { public static void main(String[] args) { final double LIGHT_SPEED = 3e5; double distance; distance = LIGHT_SPEED * 365 * 24 * 60 * 60; System.out.println("빛이 1년 동안 가는 거리 : "+ distance + " km."); } }
결과 값

2-2. 원의 면적 계산하기
반지름이 5.0인 원의 면적을 계산하는 프로그램을 작성해 보자
package ch02; public class AreaTest { public static void main(String[] args) { final double PI = 3.141592; double radius, area; radius = 5.0; area = PI * radius * radius; System.out.println("반지름이 5인 원의 면적은 "+ area); } }
결과 값

2-3. 형변환 실습하기
1.
package ch02; public class TypeConversion { public static void main(String[] args) { int i; double f; f = 1 / 5; // 1/5는 피연산자가 정수이므로 정수 연산으로 계산되어서 값은 0이 된다. System.out.println(f); f = (double) 1 / 5; // double로 형변환을 하여 1이 형변환 되고, 5도 자동으로 형변환 되어 값은 0.2가 된다. System.out.println(f); i = (int) 1.7 + (int) 1.8;// int로 형변환 되어 1.7은 1, 1.8은 1 이 되어 값은 2가 된다. System.out.println(i); } }
결과 값

2.
package ex02; public class CastEx01 { public static void main(String[] args) { int n1 = 1; double d1 = 1.5; n1 = 2; d1 = n1; // 업 캐스팅 System.out.println(d1); n1 = (int) d1; // 다운 캐스팅 System.out.println(n1); } }
결과 값

데몬 프로세스 : 끝나지 않는 프로세스
Share article