일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- css
- 상속
- javaservlet
- list
- GUI
- 파일 입출력
- springframework
- function
- JavaScript
- property
- kotlin #return #jump
- 다형성
- File 클래스
- Java
- jsp
- 윈도우 #단축키
- String
- 자바
- 연락처 프로그램
- array
- 데이터베이스
- Event
- Method
- spring
- 코틀린#클래스#상속
- html
- jQuery
- 설치
- 연산자
- 오라클
- Today
- Total
Binary World
JAVA CLASS : 데이터 타입 본문
<JAVA Data Type>
* Primitive Data type : boolean, Numeric Type
* Numeric Type : Integral Type , Floating-Point Type
* Integral Type : byte, short, int, long, char
* Floating-Point Type : float, double
* Reasoning of classified is different of saving byte. It is necessary in optimization.
- byte : 1 byte =(8 bits) -> -128 ~ 127
- short : 2 byte = (16 bits) -> -32,768 ~ 32,767
- int : 4 byte = (32 bits)
- long : 8 byte
- char : 2 byte
<JAVA Programming practice>
* VariableMain01
public class VariableMain01 { // Start of program : main() method public static void main(String[] args) { System.out.println("variable"); // Ctrl+F11 : Run program
// variable : save data // How to use variable 1 : // variable type variable name = value; int x = 100; System.out.println("x = " + x);
// How to use variable 2 : // variable type variable name; // variable name = value; int y; // declare variable y = 200; System.out.println("y = " + y);
x = 123; // Change value that previously declare variable y = 100; System.out.println("x = " + x); System.out.println("y = " + y);
// z = 111; // Not declare variable can't use
System.out.println("x + y = " + (x + y)); System.out.println("x - y = " + (x - y)); System.out.println("x * y = " + (x * y)); System.out.println("x / y = " + (x / y)); // (integer) / (integer) : calculate share // (integer) % (integer) : calculate remainder }// end main()
} // end class VariableMain01 |
* VariableMain02
public class VariableMain02 { public static void main(String[] args) { System.out.println("declare variable, initialization, practice using");
// korean, english, math // Declare variable that int type int korean, english, math;
// saving score in the each variable korean = 70; english = 80; math = 100;
// print korean/english/math score System.out.println("국어 점수 = " + korean); System.out.println("영어 점수 = " + english); System.out.println("수학 점수 = " + math);
// declare "total" to integer type of variable, saving total score of korean/english/math int total; total = korean + english + math ;
// print total score System.out.println("총 점수 = " + total);
// declare "average" to integer type of variable, saving average of three subject int average; average = total / 3;
// print average System.out.println("평균 점수 = " + average); } // end method() } //end class VariableMain02 |
* VariableMain03
public class VariableMain03 { public static void main(String[] args){ System.out.println("자바의 기본 자료형"); // 자바의 기본 자료형 : // 논리형 : boolean // 정수형: // byte(1바이트), short(2바이트),int(4바이트), // long(8바이트), char(2바이트) // 실수형 : float(4바이트), double(8바이트)
// byte 타입의 변수 n1을 선언하고, 1로 초기화 byte n1 = 1; System.out.println("n1 = " + n1); n1 = -1; System.out.println("n1 = " + n1); // n1 = 128; // byte 타입에 저장할 수 있는 수의 범위를 넘어서는 수는 저장할 수 없음 } //end main() } //end variablemain03 |
* VariableMain05(데이터 타입)
public class VariableMain05 { public static void main(String[] args) { // Ctrl + space bar : 코드 자동 완성 System.out.println("실수 자료형(floating point)");
// float : 4바이트에 저장되는 실수(소수점이 있는 숫자) 타입 // double : 8바이트에 저장되는 실수 타입 // double 타입은 float 타입보다 더 크고, // 더 정밀한(소수점 이하 자리수가 더 큼) 수를 표현할 수 있음. // 자바에서 실수 타입 리터럴(상수)의 기본 타입은 double // ex) 1.23 -> 자바는 double 취급
double x = 3.14; System.out.println("x = " + x);
float y = 3.14F; System.out.println("y = " + y); // 자바에서 실수 리터럴은 double 취급을 하기 때문에, // float 타입이라고 명시하기 위해서는 // 대문자 F 또는 소문자 f를 숫자 뒤에 붙여줌.
double n1 = 123; // int -> double 자동 형 변환 double n2 = 100; //
System.out.println("나눈셈: " + (n1/n2)); System.out.printf("나눗셈: %.5f", (n1/n2));
} } |
* VariableMain06(논리형 자료 타입)
public class VariableMain06 { public static void main(String[] args) { System.out.println("논리형 자료 타입(boolean)"); // boolean: 참(true) 또는 거짓(false) 만을 저장하는 타입
boolean b1 = true; System.out.println("b1 = " + b1);
boolean b2 = false; System.out.println("b2 = " + b2);
boolean b3 = 1 > 0; System.out.println("b3 = " + b3); } // end main() } // end class VariableMain06
|
* VariableMain07(문자 타입)
public class VariableMain07 { public static void main(String[] args) { System.out.println("문자 타입(char)"); // char: 문자 하나를 저장하는 데이터 타입, 2바이트를 사용 // 문자의 유니코드 값(0 또는 양의 정수)을 저장하는 타입 // 0 ~ 65,535 // 문자(character)는 작은 따옴표('')를 사용 // 문자열(string)은 큼 따옴표("")를 사용 // '한': 문자(character), '' // "한글": 문자열(string), "한", ""
char ch1 = 'A'; System.out.println("ch1 = " + ch1); System.out.println("ch1 = " + (int) ch1);
char ch2 = 'B'; System.out.println("ch2 = " + ch2); System.out.println("ch2 = " + (int) ch2);
char ch3 = '1'; System.out.println("ch3 = " + ch3); System.out.println("ch3 = " + (int) ch3);
char ch4 = 1; // int(4바이트) -> char(2바이트) 타입으로 자동 형변환 System.out.println("ch4 = " + ch4); System.out.println("ch4 = " + (int) ch4);
char ch5 = 54620; System.out.println("ch5 = " + ch5);
char ch6 = '한'; System.out.println("ch5 = " + (int) ch6);
boolean b = '가' < '나'; System.out.println("b = " + b);
} } |
* VariableMain08(변수 입력)
import java.util.Scanner; public class VariableMain08 { public static void main(String[] args) { // 키보드로 숫자를 입력 받아서 변수에 저장 // 1. Scanner 타입의 변수를 선언하고 초기화 Scanner sc = new Scanner(System.in);
System.out.println("국어 점수 입력:"); // 국어 점수를 저장할 변수 선언하고, 입력 받은 값으로 저장 int kor = sc.nextInt(); // 입력받은 국어 점수 확인 System.out.println("국어 점수 :" + kor);
System.out.println(); System.out.println("실수 입력:"); double x = sc.nextDouble(); System.out.println("x = " + x);
// 사용이 끝난 Scanner를 close sc.close();
} // end main() } // end class VariableMain08 |
*VariableMain09(간단한 성적처리 코드)
import java.util.Scanner; public class VariableMain09 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("초간단 성적 처리 프로그램"); // 키보드로 국영수 점수를 입력 받아서 평점과 평균을 출력하는 프로그램 Scanner sc = new Scanner(System.in);
// 국영수를 int로 입력 받아서 저장 System.out.println("국어 점수를 입력하시오 :"); int kor = sc.nextInt();
System.out.println("영어 점수를 입력하시오 :"); int eng = sc.nextInt();
System.out.println("수학 점수를 입력하시오 :"); int math = sc.nextInt();
// 국영수 점수를 출력 System.out.println("국어 점수 : " + kor); System.out.println("영어 점수 : " + eng); System.out.println("국어 점수 : " + math);
// 총점을 계산해서 출력 double total = kor + eng + math; System.out.println("총점 : " + total);
// Ctrl+/: 한줄 주석 처리/해제 // Ctrl+Shift+/: 블록주석(/* */) 처리 // Ctrl+Shift+\: 블록주석(/* */) 해제 // 평균을 계산해서 출력 double avr = total / 3; System.out.println("평균 : " + avr);
// Scanner를 종료(close) sc.close();
} } |
* VariableMain10(간단한 계산기)
public class VariableMain10 { public static void main(String[] args){ // double 타입의 숫자 2개를 입력 받아서 저장 // 두 수의 사칙연산(+, -, *, /)의 결과를 출력 Scanner sc = new Scanner(System.in);
System.out.println("첫 번째 수를 입력하세요 :"); double x = sc.nextDouble();
System.out.println("두 번째 수를 입력하세요 :"); double y = sc.nextDouble();
sc.close(); System.out.println(x + " + " + y + " = " + (x + y)); System.out.println(x + " - " + y + " = " + (x - y)); System.out.println(x + " * " + y + " = " + (x * y)); System.out.println(x + " / " + y + " = " + (x / y));
} // end main() } // end class VariableMain10 |
* VariableMain11(문자열 입력)
import java.util.Scanner; public class VariableMain11 { public static void main(String[] args){ String str1 = "+"; System.out.println(str1);
String str2 = "안녕하세요"; System.out.println(str2);
Scanner sc = new Scanner(System.in); System.out.println("문자열 입력:"); String str3 = sc.nextLine(); // nextInt(): 정수를 입력받는 메소드 // nextDouble() : 실수를 입력받는 메소드 // nextLine() : 문자열 한 줄
System.out.println(str3);
sc.close(); } } //end VariableMain11 |
* VariableMain12(Scanner 사용시 주의점)
import java.util.Scanner; public class VariableMain12 { public static void main(String[] args) { // Scanner 사용 시 주의할 점 Scanner sc = new Scanner(System.in);
System.out.println("나이 입력"); int age = sc.nextInt(); System.out.println("age = " + age);
sc.nextLine(); // enter 버퍼를 삭제하기 위한 nextline
System.out.println("이름 입력 :"); String name = sc.nextLine(); System.out.println("name = " + name);
sc.close();
// name 출력 전에 멈추는데, 나이를 입력하고 enter를 누르면 age가 출력되는데 // enter가 입력 버퍼에 저장이 되어 있기 때문에 name이 출력하지 않고 멈춘다. // 출력 화면 // 나이 입력 // 20 // age = 20 // 이름 입력 : // name = } // end main() } // end class VariableMain12 |
'개발자의 길 > JAVA' 카테고리의 다른 글
JAVA CLASS : 조건문(if) (0) | 2016.12.29 |
---|---|
JAVA CLASS : 연산자 (0) | 2016.12.29 |
이클립스(eclipse) 설치 (0) | 2016.12.29 |
이클립스 코딩에 유용한 단축키 모음 (0) | 2016.12.29 |
윈도우에 자바 설치 및 자바 환경 변수 설정 (0) | 2016.12.29 |