[HackerRank] Java Datatypes

수삼·2023년 3월 28일
0

코딩테스트

목록 보기
3/44

문제

Java has 8 primitive data types; char, boolean, byte, short, int, long, float, and double. For this exercise, we'll work with the primitives used to hold integer values (byte, short, int, and long):

A byte is an 8-bit signed integer.
A short is a 16-bit signed integer.
An int is a 32-bit signed integer.
A long is a 64-bit signed integer.
Given an input integer, you must determine which primitive data types are capable of properly storing that input.

To get you started, a portion of the solution is provided for you in the editor.

풀이

범위를 수동으로 열심히 썼는데
MAX_VALUE, MIN_VALUE 메소드를 통해 간단하게 풀이할 수 있다

if (Byte.MIN_VALUE <= num && Byte.MAX_VALUE >= num) {
                    System.out.println("* byte");
                }
if (Short.MIN_VALUE <= num && Short.MAX_VALUE >= num) {
                    System.out.println("* short");
                }
if (Integer.MIN_VALUE <= num && Integer.MAX_VALUE >= num) {
                    System.out.println("* int");
                }
if (Long.MIN_VALUE <= num && Long.MAX_VALUE >= num) {
                    System.out.println("* long");
                }

0개의 댓글