3 I public class Sum Test public static
3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 public class Sum. Test { public static void main(String a 1[]) { int a, b, sum; a = Integer. parse. Int(a 1[0]); b = Integer. parse. Int(a 1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System. out. println("두수의 "); System. out. println("두수의 합은 " + sum + "입니다"); } } 1
3 식별자와 예약어 l 자바의 예약어 abstract const finally interface short transient assert continue float long static try boolean default for native strictfp* void break do goto new super volatile byte double if package switch while case else implements private synchronized catch enum import protected this char extend instanceof public throw class final int return throws 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 17
4 변수와 자료형 4 -2 자료형 l 실습예제 - 다음 프로그램은 4개의 정수형이 나타낼 수 있는 가장 큰 값을 출력하는 프로그램이다. 각 각의 정수형 변수에는 프로그램에서 저장된 값보다 큰 값을 저장하면 구문 오류가 발생한 다. Numeric. Type. Test. java 01 public class Numeric. Type. Test { 02 public static void main(String args[]) 03 { 04 byte 05 System. out. println("127을 저장한 byte 값은 " + a); 06 short b = 32767; 07 System. out. println("32767을 저장한 short 값은 : " + b); 08 int 09 System. out. println("2147483647을 저장한 int 값은 : " + c); 10 long 11 System. out. println("9223372036854775807을 저장한 long 값은 : " + d); 12 13 a = 127; c = 2147483647; d = 9223372036854775807 L; byte형 short형 int형 long형 } } 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 28
4 변수와 자료형 4 -2 자료형 l 실습예제 - 실수형이 나타낼 수 있는 값의 범위를 나타내는 프로그램이다. 자바에서 실수는 묵시적으로 double형으로 나타낸다. Numeric. Type. Test. java 01 public class Float. Double. Test { 02 public static void main(String args[]) 03 { 04 foat a = 0. 1234567890 f; 05 double b = 0. 1234567890; 06 System. out. println("foat 변수 a의 값은 : " + a); 07 System. out. println("double 변수 b의 값은 : " + b); 08 foat c = 1. 0 f / 3. 0 f; 09 double d = 1. 0 / 3. 0; 10 System. out. println("foat 변수 c의 값은 : " + c); 11 System. out. println("double 변수 d의 값은 : " + d); 12 13 실수 값 } } 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 36
4 변수와 자료형 4 -2 자료형 l 실습예제 - 다음 프로그램은 논리형 변수를 사용하는 프로그램이다. 논리형 변수에 직접 “true” 또는 “false” 값을 저장할 수 있으며, 논리식의 결과도 저장할 수 있다. Numeric. Type. Test. java 01 public class Boolean. Type. Test { 02 public static void main(String args[]) 03 { 04 boolean a = true; 05 System. out. println("boolean 변수 a의 값은 : " + a); 06 boolean b = 10 > 20; 07 System. out. println("boolean 변수 b의 값은 : " + b); 08 boolean c = a; 09 System. out. println("boolean 변수 c의 값은 : " + c); 10 11 boolean 변수에 값 저장 } } 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 39
6 연산자와 수식 6 -1 산술 연산자 l 실습예제 - 다음 프로그램은 다양한 산술 연산자를 사용하는 프로그램이다. Casting. Test 1. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 public class Arithmetic. OPTest 1 { public static void main(String args[]) { int a=5, b=2 ; int sum=a+b; System. out. println("a+b=" + sum); int sub=a-b; System. out. println("a-b=" + sub); int mul=a*b; System. out. println("a*b=" + mul); int div=a/b; System. out. println("a/b=" + div); int mod=a%b; System. out. println("a%b=" + mod); int c = ++a; System. out. println("a의 전위증가연산(prefx)="+c); 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 두 값을 나눈 나머지를 구하는 연산자 단항 연산자 58
6 연산자와 수식 6 -3 비트 연산자 l 실습예제 - 비트 연산자는 비트 단위의 연산을 수행한다. Relational. OPTest. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 public”class Bit. OPTest 1 { public static void main(String args[]) { int a = 3; int b = 5; int c = a | b; 비트 단위의 논리합(|) int d = a & b; 비트 단위의 논리곱(&) int e = a ^ b; 비트 단위의 배타적 논리합(^) System. out. println(" a = " + a); System. out. println(" b = " + b); System. out. println(" a|b = " + c); System. out. println(" a&b = " + d); System. out. println(" a^b = " + e); } } 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 75
6 연산자와 수식 6 -3 비트 연산자 l 실습예제 - 다음은 비트 단위로 시프트를 수행하는 프로그램의 예이다. Bit. OPTest 2. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 public class Bit. OPTest 2 { public static void main(String args[]) { int a = 4; int b = a << 2; int c = a >> 2; int d = -4; int e = d << 2; int f = d >> 2; int g = d >>> 2; System. out. println(" a = " + a); System. out. println(" a<<2 = " + b); System. out. println(" a>>2 = " + c); System. out. println(" d<<2 = " + e); System. out. println(" d>>2 = " + f); System. out. println(" d>>>2 = " + g); } } 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 왼쪽으로 2비트 시프트 오른쪽으로 2비트 시프트(왼쪽에는 0으로 채워짐) 78
7 문자열(String) 6 -6 연산자의 우선순위 l 실습예제 - 다음 프로그램은 문자열을 사용하는 예이다. String. Test. java 01 02 03 04 05 06 07 08 09 10 11 12 public class String. Test { public static void main(String args[]) { String str 1 = "아! 대한민국 "; 문자열 변수에 값 저장 String str 2 = new String("Korea"); System. out. println(str 1 + str 2); 문자열 출력 int a = 1000; int b = 2000; System. out. println(str 1 + a + b + "리 금수강산 "); 정수가 문자열로 변환되어 출력 System. out. println(str 1 + (a + b) + "리 금수강산 "); } } 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 95
- Slides: 100