본문 바로가기
개발/TIL

[TIL]JAVA사전스터디_기본형 변수, 참조형 변수, 형변환

by 반비🥰 2024. 5. 8.
반응형

 


 

 

 

📌 자바 개발 환경


JRE(Java Runtime Environment)

: 자바 실행 환경으로 JRE(JVM)만 있다면 Java 프로그램을 실행만 시킬 수 있음

: .class파일만 실행 가능

 

JDK(Java Development Kit)

: 자바 개발 키트로, JRE(JVM)의 기능을 포함

: java파일 ▶️ class파일로 변환해부는 Java Compiler(javac) 기능

: 코드 디버깅 기능 포함, 디버깅 = 중단점 일시정지 + 코드 라인단위 수행

 

 

📌 변수 


❗️변수 VS 상수
변수 : 변하는 저장공간
상수 : 변하지 않는 저장공간

 

  • 기본형 변수
    • 논리형 변수 : boolean
    • 문자형 변수 : char
    • 정수형 변수 : byte, short, int, long
      • 정수형 변수 표현 범위
각 변수 표현 범위를 넘는 숫자를 넣게 되면 오버플로우가 발생하며, 해당 숫자를 출력해 보면 입력값과 다른 값으로 표현된다.
byte : -128 ~ 127
short(2byte) : -32,768 ~ 32,767
int(4byte) : -21억 ~ 21억 범위
long(8byte) : 9백경 정도 매우 큰 수
byte byteNumber = 127;
short shortNumber = 32767;
int intNumber = 2147483647;
long longNumber = 2147483647L; // int와 long 구분을 위해 리터럴 문자L을 붙임
  • 실수형 변수 : float, double
    • float/double이 long보다 범위가 큼
      ▶️ float는 부동 소수점 방식을 사용하기 때문에 long보다 더 큰 범위의 숫자 표현 가능
float floatNumber = 0.123f; // f : 리터럴 구분값(=데이터값)
double doubleNumber = 0123123123;
  • 참조형 변수
    • 문자열 변수 : String
      • char [] = String과 동일
    • 래퍼클래스 변수
      • 박싱 & 언방식
        • 기본 타입에서 래퍼 클래스 변수로 변수를 감싸는 것 = "박싱"
        • 래퍼 클래스 변수를 기본 타입 변수로 가져오는 것 = "언박싱"
기본 타입 래퍼 클래스
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

 

 

 

📌 참조형 변수 


  • 참조형 변수는 주소값을 저장하는 주소형 변수
  • 기본형 변수는 원본값이 Stack 영역에 존재
  • 참조형 변수는 원본값이 Heap 영역에 존재
    • Stack 영역에 따로 저장해 둔 원본값의 Heap 영역주소를 저장
❗️Stack 영역 VS Heap 영역
Stack : 정적으로 할당된 메모리 영역
➡️ 크기가 정해져 있는 기본형 변수를 저장, 크기가 정해져있는 참조형 변수의 주소값도 저장
Heap : 동적으로 할당된 메모리 영역
➡️ 크기가 계속 늘어날 수 있는 참조형 변수의 원본을 저장

 

 

📌 형변환


❗️자동 형변환 VS 강제 형변환
- 작은 타입 > 큰 타입 형변환 시, 자동 형변환
   - 값의 손실 X
   - 컴파일러가 자동 형변환
- 큰 타입 > 작은 타입 형변환 시, 강제 형변환(=캐스팅)
   - 작은 표현범위로 변환되는 거라 값의 손실 발생

 

 

🤔 항해 플러스 1주 차 과제 🤔


❓문제

[ 입력값 ]

  • 요리 제목 입력
  • 요리 별점 1 ~ 5 사이의 소수점이 있는 실수로 입력
  • 요리 레시피 10 문자 입력

[ 출력값 ]

  • 요리 제목을 괄호로 감싸 출력
  • 요리 별점을 소수점을 제외한 정수로만 출력
  • 정수별점을 5점 만점 퍼센트로 표현했을 때 값을 실수로 출력
  • 요리 레시피를 순서와 함께 차례로 출력

 

👩🏻‍💻 문제 풀이

import java.util.Scanner;

public class WeeklyHomeWork1 {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String foodTitle = sc.nextLine();
        float foodRate=sc.nextFloat();
        sc.nextLine();
        String recipe1 = sc.nextLine();
        String recipe2 = sc.nextLine();
        String recipe3 = sc.nextLine();
        String recipe4 = sc.nextLine();
        String recipe5 = sc.nextLine();
        String recipe6 = sc.nextLine();
        String recipe7 = sc.nextLine();
        String recipe8 = sc.nextLine();
        String recipe9 = sc.nextLine();
        String recipe10 = sc.nextLine();

        int intFoodRate = (int)foodRate;
        float RatePercent =(intFoodRate/5)*100;

        System.out.println("[ "+foodTitle+" ]");
        System.out.println("별점 : "+intFoodRate+" ("+RatePercent+"%)");
        System.out.println("1. "+ recipe1);
        System.out.println("2. "+ recipe2);
        System.out.println("3. "+ recipe3);
        System.out.println("4. "+ recipe4);
        System.out.println("5. "+ recipe5);
        System.out.println("6. "+ recipe6);
        System.out.println("7. "+ recipe7);
        System.out.println("8. "+ recipe8);
        System.out.println("9. "+ recipe9);
        System.out.println("10. "+ recipe10);


    }
}
반응형