항해99 TIL 24일차_060122

열공이·2022년 6월 1일
0

자바 공부 시작

Intellij를 다운받아 시작했다. Hello World를 프린트 하려는데 add config가 제대로 작동 안 해서 계속 에러가 났다. Cmd + , 눌러서 Java complier를 설정해주고 (터미널에서 java -version 치면 깐 자바 어플리케이션 정보를 볼 수 있다) run시도해봤는데 안 풀렀다.

그렇게 헤매다가 조언을 구했는데 새로운 프로젝트를 만들어 그 곳에 자바 파일을 만들고 실행하니 잘 작동하며 Hello, world가 찍혔다. 그런데 파이썬을 실행시킬 때보다 느려서 걱정이 된다. Intellij에서 자바를 실행해서 느린 건지 내 컴퓨터가 이것저것 다운받느라고 느려진 건지 모르지만 너무 느렸다.

일단 기본적인 문법을 조금 배웠다.
*psvm을 치면 그 밑에 public static main void(String[] args){}가 나온다.

  • static means that the method belongs to the Main class and not an object of the Main class.
  • void means that this method does not have a return value. You will learn more about return values later in this chapter

*sout이라고 치면 System.out.println()이 나와 shortcut같이 원하는 내용을 빨리 넣을 수 있다.

자바 데이터 타입

Primitive data type
int, boolean, float (특수해서 float num1 = 6.99f라고 해야함), char (작은 따음표 씀 ''), String (큰 따음표 씀 ""), double (비슷하게 특수해서 double num2 = 99.99d)

int myNum = 100000;
long myNum = 15000000000L;
float myNum = 5.75f;
double myNum = 19.99d;

자바 타입 캐스팅

In Java, there are two types of casting:

Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double

Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte

Narrowing Casting
그리고 double에서 int로 변환시킬 때 변환하고 싶은 타입을 감싸서 써야한다 (ex. (int)):
double myDouble = 9.78d;
int myInt = (int) myDouble;

Widening Casting
int myInt = 9;
double myDouble = myInt;

profile
프로그래머가 되자! 열공!

0개의 댓글