변수
: 하나의 값을 저장할 수 있는 메모리 공간 / 어떤 값을 받으려면 메모리공간이 필요함
ex. 주스컵
타입: 변수의 모양 정함
변수값 : literal ex. 20, 20.5
선언된 블록(메소드, if, while,..) 내에서만 사용 가능
로컬 변수: 메소드 블록 내에서 선언된 변수
- 변수 이름
개발자가 작성
가독성있게 만들기
연산자 때문에 특수문자는 불가 ( $ , _ 는 가능 )
똑같은 이름의 변수 X
숫자로 시작할 수 없음 ex. 1num
- 따옴표
public class VariableEx1 {
public static void main(String[] args) {
System.out.println(20); //숫자는 따옴표 불필요
System.out.println(5+2);
System.out.println("5+2"); //문자열 인식
}}
//
20
7
5+2
- 연산자
public class VariableEx1 {
public static void main(String[] args) {
System.out.println(5+2);
System.out.println(5-2);
System.out.println(5*2);
System.out.println(5/2); //몫
System.out.println(5%2); //나머지
}}
- 정의 형식
public class VariableEx1 {
public static void main(String[] args) {
//타입 변수이름 = 값;
int num1 = 5;
int num2 = 2;
System.out.println(num1+num2);
System.out.println(num1-num2);
System.out.println(num1*num2);
System.out.println(num1/num2);
}}
- 변수
public class Hello {
public static void main(String[] args) {
int 안녕하세요 = 50; //한글 변수 지양하기
System.out.println(안녕하세요); //따옴표 없으면 기억장소(변수)로 인식함
System.out.println("반갑습니다");
}}
//
50
반갑습니다
- 16진수 연산
public static void main(String[] args) {
int num1 = 0x36A; //16진수 (3*16^2 + 6*16^1 + 10*16^0) = 874
int num2 = 2;
System.out.println(num1+num2);
System.out.println(num1-num2);
System.out.println(num1*num2);
System.out.println(num1/num2);
}}
//
876
872
1748
437
- 8진수 연산
public static void main(String[] args) {
int num1 = 056; //앞에 0 붙으면 => 8진수 (5*8^1 + 6*8^0)
int num2 = 2;
System.out.println(num1+num2);
System.out.println(num1-num2);
System.out.println(num1*num2);
System.out.println(num1/num2);
}}
//
48
44
92
23
- 선언, 대입
public class VariableEx2 {
public static void main(String[] args) {
int num; //선언문 => 정수 만들겠다고 컴퓨터에 알림
num = 20; //대입문
}}
- 초기화
public static void main(String[] args) {
int num = 20; //초기화 : 선언과 동시에 대입
=> 변수는 초기화 되어야 읽을 수 있음
데이터 타입
8bit = 1byte
- 기본 타입 (primitive)
1. 정수(소수점 없는 수) => 고정소수점수(맨 오른쪽에 고정돼있는 것으로 가정)
byte (1byte) : 0~127 (~ -128)
short (2byte) : 0~32767
char (2byte, ★특수 용도)
★int (4byte, 多) : 0~약 21억
long (8byte) : 0~조, 경 수준 - 변수값 뒤에 L붙임
2. 실수(소수점 있는 수) => 부동소수점수(자릿수 다른 연산 시 소수점 이동시켜 계산)
float(4byte) : 지수 8bit, 가수 24bit - 변수값 뒤에 f붙임
double(8byte) : 지수 11bit, 가수 53bit
3. 논리(참 거짓 따지는 논리값)
boolean (1byte) : true / false
- 정수 - 실수 비교
정수 : 1000000000
실수(지수도 보관) : 10^9 => 실수는 같은 값으로 더 작은 칸 사용
/
정수 : 보관형식 1가지 => int num = 2.5
실수 : 보관형식 2가지(지수, 가수)
public class VariableEx2 {
public static void main(String[] args) { byte num = 128; //오류 => 오버플로우
double dnum = 20.5;
boolean bool = false; //false는 문자 아닌 특수논리값
System.out.println(num);
System.out.println(dnum);
System.out.println(bool);
}}
- 오류
public class VariableEx3 {
public static void main(String[] args) {
byte num1 = 30;
byte num2 = 20; byte result = num1 + num2; //오류 => 오버플로우 아님 => 연산 기본값이 int 타입으로 되어있음
System.out.println("두 수의 합=" + result);
}}
- 해결
public static void main(String[] args) {
byte num1 = 100;
byte num2 = 100;
int result = num1 + num2;
System.out.println("두 수의 합=" + result);
}}
//
두 수의 합=200
- char
public static void main(String[] args) {
char ch = 'A'; //숫자임 (A=65)
) char cha = 44032; //'가' 입력도 가능
System.out.println(ch);
System.out.println(cha);
}}
//
A
가
- 부동소수점수
public static void main(String[] args) {
double num = 2.53+31.7;
System.out.println(num);
}}
//
34.23
=> 0.317 * 10^2 + 0.0253*10^2 => 0.3423 * 10^2 => 34.23 (부동소수점수식 계산)
- float
public static void main(String[] args) {
double num1 = 2.5;
float num2 = 2.5f;
System.out.println(num2);
}}
//
2.5 => float 형식이 출력될 때엔 f 미출력
- 실수 double
public static void main(String[] args) {
double num3 = 5E15; //실수는 지수 이용 가능
System.out.println(num3);
}}
//
5.0E15 => 5 * 10^15
형변환
- 종류
1. 자동타입변환 : 큰 크기타입 <= 작은 크기타입
2. 강제타입변환 : 작은 크기타입 <= 큰 크기타입
=> 큰 크기를 작은 크기에 넣을 수 없으므로 강제로 변환해 넣어야 함
=> 작은 크기타입 = (작은 크기타입) 큰 크기타입
cf. char : 음수 X
=> byte->char은 강제형변환만 가능
- 자동타입변환
public static void main(String[] args) {
int num = 20;
double dnum = num; //큰 크기타입에 자동으로 더 작은 크기타입 넣어짐
System.out.println(dnum);
}}
//
20.0
- 강제타입변환
public static void main(String[] args) {
int num =320; => 0001 0100 0000 (32bit 사용 가능하나 해당 숫자로는 12bit만 사용)
byte num1;
num1 = (byte) num; //강제형변환 => 0100 0000 (byte는 8bit만 사용가능하므로 위 int의 마지막 8bit만 가져옴)
System.out.println(num1);
//
64
연산자
다른 타입 연산 시 크기 큰 타입으로 자동 변환됨 ex. double result = int value + double value2
- 실수 연산
public static void main(String[] args) {
double num = 5/2;
System.out.println(num);
}}
//
2.0
=> 연산 머신은 정수 연산 시 int를 기본값으로 -> 소수 누락되어 출력
- 실수 연산2
public static void main(String[] args) {
double num = 5/2.0; => 값에 미리 .0 붙여 실수화 -> 그 결과 실수 전체 출력됨
System.out.println(num);
}}
//
2.5
- 입력값 받아 연산
import java.util.Scanner;
public class OperatorEx1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //콘솔로 입력받는 객체 생성
int amount = sc.nextInt(); // nextInt에서 정수값 받음 -> amount로 전달
int price = 2000;
int salesPrice = amount*price;
System.out.println("총 판매가 = "+salesPrice);
}}
//
7 (내가 입력한 값)
총 판매가 = 14000
- 출력값 추가
import java.util.Scanner;
public class OperatorEx1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("구입수량은?"); //ln없애서 커서가 바로 옆으로 감
int amount = sc.nextInt();
int price = 2000;
int salesPrice = amount*price;
System.out.println("총 판매가 = "+salesPrice);
}}
//
구입수량은? 10 ( 10은 내가 입력한 값 )
총 판매가 = 20000
'Programming > 국비학원' 카테고리의 다른 글
| 220425 - do while, 참조, 비트 이동 연산자, 삼항 연산자, 배열, 향상된 for문, 정렬 (0) | 2022.04.26 |
|---|---|
| 220422 - 반복문 while, continue, break, 중첩반복문, 라벨링 (0) | 2022.04.23 |
| 220421 - 연산자, 조건문, 반복문 (0) | 2022.04.22 |
| 220420 - 변수, 데이터타입, 형변환, 연산자 2 (0) | 2022.04.21 |
| 220419 - OT 및 개발 환경 구축 (0) | 2022.04.20 |