java/개념

식별자

unhyepnhj 2024. 4. 29. 15:27

식별자(identifier): 클래스, 변수, 상수, 메소드 등에 붙이는 이름

 

식별자 이름 규칙

1. 특수문자, 공백 사용 불가('_', '$'는 예외로 사용 가능)

2. 한글 사용 가능(유니코드 문자 사용 가능하므로)

3. 자바 키워드(if, while, class 등) 식별자로 사용 불가

더보기

자바 키워드

자바에서 이미 용도가 정해진 단어(=예약어, reserved word)

abstract continue for new switch
assert default if package synchronized
boolean do goto private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

4. 숫자로 시작 불가

5. 대소문자 구별

6. 길이 제한 없음

 

좋은 이름 붙이는 관습

1. 목적에 맞는 이름: s보다는 sum을 사용하는 식

2. 충분히 긴 이름: AVM보다는 AutoVendingMachine

3. 관습 따르기

더보기

클래스 이름

- 대문자로 시작

- 각 단어의 첫 번째 문자만 대문자

public class HelloWorld{}
class AutoVendingMachine{}

 

변수, 메소드 이름

- 첫 단어는 소문자로 표기

- 이후 각 단어의 첫 번째 문자만 대문자

int myAge;
boolean isSingle;
public int getAge(){return 20;}

 

상수 이름

- 전체를 대문자로 표기

final double PI=3.141592;