📖 자바 기초 문법
- 실행 : Shift+f10
- 파일명 == 클래스명
-> 안에 main 함수 포함
public class 클래스명
{
public static void main(String[] args)
{
}
}
📌 출력
System.out.println("");
📖 입력
📌 선언 및 생성
import java.util.Scanner;
# 객체 생성
Scanner sc = new Scanner(System.in);
📌 사용
age = sc.nextInt(); # next + 자료형()
buffer = sc.nextLine(); # 한 줄 전체를 입력 받는다
n = sc.next(); # 공백 전까지 입력 받는다
📌 next vs nextLine
- next : 공백 전까지 입력 받는다
=> ex) 자바 이클립스 -> '자바' 만 저장, 계속 영향을 끼친다
- nextLine : 한 줄 전체를 입력 받는다
📌 주석
📌 자료형
String : 큰 따옴표
char : 작은 따옴표
float 뒤에는 f 또는 F를 붙여야 한다
long 뒤에는 l 또는 L을 붙여야 한다
📌 상수
📖 형 변환
int score;
score = 93 + (int) 98.8;
System.out.println(score);
float score;
score = 93 + 98.8;
System.out.println(score);
- int -> long -> float -> double (자동 형 변환)
- double -> float -> long -> int (수동 형 변환)
📌 문자열 -> 숫자
int i = Integer.parseInt("93");
double d = Double.parseDouble("98.8");
📌 숫자 -> 문자열
String s1 = String.valueOf(93);
s1 = Integer.toString(93);
s1 = Double.toString(93.3);
📌 랜덤 함수
- Math.randon()은 실수형이 기본이라 int형으로 타입 캐스팅을 해줘야 한다
📖 문자열
📌 문자열의 길이
String s = "아정"
System.out.println(s.length());
📌 대소문자 변환
s.toUpperCase()
s.toLowerCase()
📌 포함 관계
s.contains("정")
s.contains("Java")
s.indexOf("정")
s.indexOf(".")
s.lastIndexOf("정")
s.startsWith("주")
s.endsWith(".")
📌 문자열 반환
s.replace("바꾸려는 문자열", "변환 문자열");
s.substring(7, 9)
📌 공백 제거
s.trim();
📌 문자열 결합
s1 + s2
s1.concat(s2)
📖 문자열 비교
s1.equals(s2);
s1.equals("Java");
s1.equalsIgnoreCase("");
s1 = "1234";
s2 = "1234";
System.out.println(s1.equals(s2));
System.out.println(s1 == s2);
s1 = new String("1234");
s2 = new String("1234");
System.out.println(s1.equals(s2));
System.out.println(s1 == s2);
📖 특수 문자
\n : 줄바꿈
\t : 탭
\\ : 역슬래시
\" : 큰따옴표 출력
\' : 작은따옴표 출력
📖 참고자료
정보 감사합니다.