JAVA 기초 _2

정혜지·2023년 4월 18일
0

자료형, Data Type


  • 원시 자료형 : boolean, char, byte, short, int, long, float, double
  • 비원시 자료형: String, Array, etc

JAVA의 자료형과 C언어의 차이점
-> boolean(명제), String(문자열)의 존재
C언어는 String형이 없어, char의 배열로써 표현하는 경우가 많음


예약어자료형크기(Byte)
byte정수형1
short정수형2
int정수형4
long정수형8
char문자형2
float실수형4
double실수형8
  • Q. 정수를 나타내는 자료형이 많은 이유 ?
    • A. 자료형마다 차지하는 메모리 공간의 크기가 다르기 때문


Variable Data Types

  • Variable Data Types
    • Integer
    • Character
    • Float
    • Double

▪ Double

- 평균 구하기

public class Main {
  
  public static void main(String[] args) {
    
    double a = 10.3;
    double b = 9.6;
    double c = 10.1;
    
    System.out.println((a + b + c) / 3);		//10.0
    
  }

}


▪ Character

- a to z 출력

public class Main {

  public static void main(String[] args) {
  
    for (char i = 'a'; i <= 'z'; i++) 
    {
      System.out.print(i + " ");
    }
  
  }
  
}


▶ 실행결과: a b c d e f g h i j k l m n o p q r s t u v w x y z

char : 아스키코드 기반의 문자 자료형
메모리크기: 2byte / 표현범위 : 0 to 65535

ASCII CODE
하나의 문자형이 담을 수 있는 모든 문자에 대한 내용을 보여주는 코드표



▪ Integer

10진수를 8진수/16진수로 변경

public class Main{
  
  public static void main(String[] args){
    
    int a = 200;
    
    System.out.println("10진수 : " + a);
    System.out.printf("8진수 : %o\n", a);
    System.out.printf("16진수 : %x\n", a);
    
    System.out.format("8진수 : %o\n", a);
    System.out.format("16진수 : %x\n", a);
       
  }
  
}

▶ 실행결과
10진수 : 200
8진수 : 310
16진수 : c8


▪ String

substring

public class Main{

  public static void main(String[] args){
    
    String name = "John Doe";
    System.out.println(name);		// John Doe
    System.out.println(name.substring(0, 1));		// J
    System.out.println(name.substring(3, 6));		// n D
    System.out.println(name.substring(5, 8));		// Doe
    
  }

}

Stirng 자료형이 비원시 자료형이 이유

String 변수에 substring()과 같은 내부적인 함수가 존재
-> 클래스기반의 비원시적 자료형
-> 자바에서 제공하는 특징적이고 특수한 자료형

  • substring(시작위치, 마지막위치);

  • Q. char의 배열로 되어있는 String 자료형의 최대 크기는 무엇일까

    • A. 약 4GB 만큼의 문자를 담을 수 있다~! -> 거의 무한에 가깝다





▪ byte 자료형

byte
자료형: 정수형 / 메모리크기: 1byte
1byte(2⁸)만큼만 표현할 수 있음 // 1byte = 8bit => 2⁸
표현범위 : -2⁷ to 2⁷-1 (-128 to 127)

8bit : 0과 1을 표현할 수 있는 단위가 8개 => 2⁸


profile
오히려 좋아

0개의 댓글