웹 프로그래밍

변수 선언 및 초기화, 자료형 - JAVA 본문

Java

변수 선언 및 초기화, 자료형 - JAVA

B. C Choi 2021. 8. 2. 18:35
반응형

 

변수 (Variable)

 

계속 변하는 값이면서, 그 값을 저장하는 공간.

 

대소문자가 구분되며 길이 제한이 없다.

변수는 한 개의 데이터만 보관, 마지막에 대입한 값만 보관한다.


 

 

변수 선언 초기화 방법

 

1. 변수 선언 후 값을 입력하는 경우.

int myValue;
myValue = 100; // 100 = 리터럴

System.out.println(myValue);

//100

2. 변수 선언과 값을 동시에 입력하는 경우.

int myValue2 = 101;

System.out.println(myValue2);

//101

초기화되지 않은 변수를 출력하게 되면??

int myValue3;

System.out.println(myValue3);

//에러 발생

 

 

변수가 선언되지 않는 경우

 

1. 예약어 사용했을 때

  ex) int, double, String, true, false, void, static, class


2. 숫자로 시작했을 때

  ex) int 25 age , String 1 str


3. 일부 특수문자를 사용했을 때(_ 와 $만 사용 가능)

  ex) int @.@

 

 


 

 

변수 선언 요령

1. 대소문자 구별이 명확한 카멜 표기법을 사용한다. 

  ex) numberOne, userName


2. 알아볼 수 있게 선언해야 합니다.

  ex) abc, x, y 금지! 단, 수학계산 또는 알고리즘에서만 사용


3. 영어로 표기해야 한다. 국룰

 

4. 멤버 변수 지역변수 간 명칭 중복은 가능하나 중복해서는 안 된다. 헷갈림 방지


5. 변수 이름의 길이는 무제한으로 선언 가능하나 적절한 길이로 선언해야 한다.


 

 

자료형의 종류와 크기

 

정수형

byte - 1byte

System.out.println("byte의 Bit Size는? : " + Byte.SIZE );
System.out.println("byte의 Byte 수는? : " + Byte.SIZE / 8 );
System.out.println("byte의 Max Size는? : " + Byte.MAX_VALUE );
System.out.println("byte의 Min Size는? : " + Byte.MIN_VALUE);

8
1
127
-128

short - 2byte

System.out.println("short의 Bit Size는? : " + Short.SIZE );
System.out.println("short의 byte 수는? : " + Short.SIZE / 8);
System.out.println("short의 Max Size는? : " + Short.MAX_VALUE);
System.out.println("short의 Min Size는? : " + Short.MIN_VALUE);

16
2
32767
-32768

int - 4byte

System.out.println("int의 Bit Size는? : " + Integer.SIZE );
System.out.println("int의 byte 수는? : " + Integer.SIZE / 8 );
System.out.println("int의 Max Size는? : " + Integer.MAX_VALUE);
System.out.println("int의 Min Size는? : " + Integer.MIN_VALUE);

32
4
2147483647
-2147483648

long - 8byte

System.out.println("long의 Bit Size는? : " + Long.SIZE );
System.out.println("long의 byte 수는? : " + Long.SIZE / 8 );
System.out.println("long의 Max Size는? : " + Long.MAX_VALUE);
System.out.println("long의 Min Size는? : " + Long.MIN_VALUE);

64
8
9223372036854775807
-9223372036854775808


실수형

float - 4byte

System.out.println("float의 Bit Size는? : " + Float.SIZE );
System.out.println("float의 byte 수는? : " + Float.SIZE / 8 );
System.out.println("float의 Max Size는? : " + Float.MAX_VALUE);
System.out.println("float의 Min Size는? : " + Float.MIN_VALUE);

32
4
3.4028235E38
1.4E-45

double - 8byte

System.out.println("double의 Bit Size는? : " + Double.SIZE );
System.out.println("double의 byte 수는? : " + Double.SIZE / 8 );
System.out.println("double의 Max Size는? : " + Double.MAX_VALUE);
System.out.println("double의 Min Size는? : " + Double.MIN_VALUE);

64
8
1.7976931348623157E308
4.9E-324


문자형

char - 2byte

System.out.println("char의 Bit Size는? : " + (int)Character.SIZE );
System.out.println("char의 byte 수는? : " + (int)Character.SIZE / 8 );
System.out.println("char의 Max Size는? : " + (int)Character.MAX_VALUE);
System.out.println("char의 Min Size는? : " + (int)Character.MIN_VALUE);

16
2
65535
0


논리형

boolean - 1byte

 

참조형(문자열)

String


 

 

정수형 선언 연습

byte tempByte = 1;
short userAges = 27; 
int rectHeight = 400;
long bitAccount = 400L;
// long은 반드시 숫자 뒤에 l 또는 L을 붙여야 인식한다.

System.out.println(tempByte);
System.out.println(userAges);
System.out.println(rectHeight);
System.out.println(bitAccount);

1
27
400
400
int tempInt = 0;
double tempDouble = 3.14;
tempInt = (int)tempDouble;
System.out.println(tempInt);
System.out.println(tempDouble);

tempDouble = (int) tempDouble; // 소숫점 사라짐
System.out.println(tempDouble);

3
3.14

3.0

실수형 선언 연습

float floatPie = 3.141592653589793238f; // float는 f를 반드시 붙어야한다.
double doublePie = 3.141592653589793238;
// 오차율이 존재한다. 부동소수점 연산을 정확하게 할때는 double, float을 사용
// 부동수소점 = 실수형 오차를 발생시킨다.

System.out.println(floatPie);
System.out.println(doublePie);

3.1415927
3.141592653589793
float testFloat = 0.1f + 0.2f;
double testDouble = 0.1 + 0.2;

System.out.println(testFloat);
System.out.println(testDouble);

0.3
0.30000000000000004

 

오차를 줄이는 객체

//import java.math.BigDecimal 필요

BigDecimal bigPie = new BigDecimal("3.141592653589793238"); 
// 기본형이 아니다.
// 단점 속도가 많이 느리다 -> 잘 쓰지 않는다.

 

문자형, 참 초형 선언 연습

 

char tempChar = 'A'; // 2byte, java 유니코드를 사용하는 크기
System.out.println(tempChar);
A
		
String tmpsStr = "BCDEFG";
System.out.println(tmpsStr);
BCDEFG
		
String addStr = tempChar + tmpsStr;
System.out.println(addStr);
ABCDEFG
char ch = 'A';
int alpha = ch;
System.out.println(ch);
System.out.println(alpha);
System.out.println((int)ch);
		
System.out.println(ch+1);
System.out.println((char)(ch+1));
System.out.println((char)('A'+1));

A
65
65

66
B
B

논리형 선언 연습

boolean isReasonable = true;
System.out.println(isReasonable);
true

isReasonable = false;
System.out.println(isReasonable);
false

 

 

final

// final은 상수를 선언 할 때 사용하고 바뀌지 않는다.
final int MAX_SIZE = 30;
        
/* MAX_SIZE = 40; 
* System.out.println(MAX_SIZE);
* 에러 발생!
*/
반응형