Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 조지 불
- append()
- 부스트캠프
- 귀도 반 로섬
- input()
- del()
- false
- Python
- 그룹 # 그룹 해체 # 단축키 #figma #Figma
- 합집합
- 변수
- 정보를 담을 수 있는 그릇
- pop()
- 변수와 입출력
- 입출력
- 딥러닝
- index()
- 변할 수 있는
- 차집합
- 성적 입력받기
- 1일차
- 파이썬
- null # undefined
- a=1
- 리스트와 차이점
- Java Script # == # === # difference # 차이
- html
- 불리안
- insert()
- 조건문 큰 수부터 입력받아야하는 이유
Archives
- Today
- Total
I about me
[MidTerm] 1. Java Elements 본문
Code Structure
Source File → Class File (파일명과 동일시) → Method 1, 2, 3
Javac // 자바 소스코드를 바이트코드로 바꿔줌
Java, JVM // 바이트 코드를 기계어로 바꿔줌
Java Comments
- Single-line → //
- Multi-line → /* */
예약어(keywords) vs. 식별자(identifier)
예약어 (Key word) | private, public, protected, final, implements, extends, import |
식별자 (Identifier) | Identifier 관례 - 클래스/인터페이스 첫 글자는 대문자로 시작, 각 단어마다 첫 글자 대문자로 (Camel 표기법) ex) MyClass, PersonFamily - 변수/메소드 첫 글자는 소문자로 시작, 각 단어마다 첫 글자 대문자로 (Camel 표기법) ex) myMethod, addVer Identifier 작성규칙
|
Data Types in Java
- 반드시 Data Type을 지정해야함
- 초기화는 필수 아님
- Type
- Primitives(기본 타입)
- 기본 데이터 타입이라하면, int, double, boolean, char 등이 있음
- 이러한 타입은 실제 값에 변수를 저장함
- Object references(객체 참조)
- 메서드 호출하거나 멤버에 접근하기 위해 사용함
- Primitives(기본 타입)
(1) 정수형 : byte, short, int, long
(2) 실수형 : float, double
(3) 문자형 : char
(4) 논리형 : boolean

- final (c에서 constant(상수취급))
ex) final double width = 2.34; - enum type: 서로 관련 있는 상수들의 집합을 나타내는 자료형
-
더보기
public class EnumExample { // 과일 열거형 정의 public enum Fruit { APPLE, BANANA, ORANGE } public static void main(String[] args) { // 과일 변수 선언 Fruit fruit1 = Fruit.APPLE; Fruit fruit2 = Fruit.BANANA; Fruit fruit3 = Fruit.ORANGE; // 과일 변수 출력 System.out.println("Fruit 1: " + fruit1); // 출력: Fruit 1: APPLE System.out.println("Fruit 2: " + fruit2); // 출력: Fruit 2: BANANA System.out.println("Fruit 3: " + fruit3); // 출력: Fruit 3: ORANGE } }
-
- Type casting: 한 데이터 유형을 다른 데이터 유형으로 변환하는 것
- 자동 형 변환(implicit casting)
-
더보기
public class ImplicitCastingExample { public static void main(String[] args) { int intValue = 10; double doubleValue = intValue; // int가 double로 자동 형 변환됨 System.out.println("intValue: " + intValue); // 출력: intValue: 10 System.out.println("doubleValue: " + doubleValue); // 출력: doubleValue: 10.0 } }
-
- 강제 형 변환(explicit casting)
-
더보기
public class ExplicitCastingExample { public static void main(String[] args) { double doubleValue = 10.5; int intValue = (int) doubleValue; // double이 int로 강제 형 변환됨 System.out.println("doubleValue: " + doubleValue); // 출력: doubleValue: 10.5 System.out.println("intValue: " + intValue); // 출력: intValue: 10 } }
-
- 자동 형 변환(implicit casting)
Operators in Java
- Arithmetic Operations : 산술 연산자로 사용됩니다. (+, -, x, %)
- Logical Operators : 논리 연산자 (&&, !, ||)
- Relational Operators : 관계 연산자 (==. !=, >=, <=)을 위해서 사용됨
- Bitwise Operators : 비트 단위로 연산을 수행함
-
더보기
- AND(&): 두 비트가 모두 1이면 결과가 1이 되고, 그 외의 경우에는 0이 됨.
int a = 5; // 이진수로 0000 0101 int b = 3; // 이진수로 0000 0011 int resultAnd = a & b; // 이진수로 0000 0001 (결과: 1) System.out.println("AND 결과: " + resultAnd); // 출력: AND 결과: 1
- OR(|): 두 비트 중 하나 이상이 1이면 결과가 1이 되고, 그 외의 경우에는 0이 됨.
int a = 5; // 이진수로 0000 0101 int b = 3; // 이진수로 0000 0011 int resultOr = a | b; // 이진수로 0000 0111 (결과: 7) System.out.println("OR 결과: " + resultOr); // 출력: OR 결과: 7
- 비트 XOR(^): 두 비트가 서로 다르면 결과가 1이 되고, 같으면 결과가 0이 됨.
int a = 5; // 이진수로 0000 0101 int b = 3; // 이진수로 0000 0011 int resultXor = a ^ b; // 이진수로 0000 0110 (결과: 6) System.out.println("XOR 결과: " + resultXor); // 출력: XOR 결과: 6
- NOT(~): 비트를 반전시킵니다. 0은 1로, 1은 0으로 바뀜.
int a = 5; // 이진수로 0000 0101 int resultNot = ~a; // 이진수로 1111 1010 (결과: -6) System.out.println("NOT 결과: " + resultNot); // 출력: NOT 결과: -6
- >>: 오른쪽으로 x칸 이동, <<: 왼쪽으로 x칸 이동
public class LeftShiftExample { public static void main(String[] args) { int num = 5; // 이진수로 0000 0101 // 왼쪽으로 2비트 이동 int result = num << 2; // 이진수로 0001 0100 (20) System.out.println("결과: " + result); // 출력: 결과: 20 } }
- AND(&): 두 비트가 모두 1이면 결과가 1이 되고, 그 외의 경우에는 0이 됨.
-
Strings in Java
- 불변성
- 객체 참조
-
더보기
public class ImmutabilityExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = str1; // str2가 str1을 가리킴 System.out.println("str1: " + str1); // 출력: str1: Hello System.out.println("str2: " + str2); // 출력: str2: Hello str1 = "Hi"; // str1의 값을 변경 System.out.println("str1: " + str1); // 출력: str1: Hi System.out.println("str2: " + str2); // 출력: str2: Hello (str2는 여전히 이전 값을 가리킴) } }
-
- 다양한 방법
- Substring : 문자열의 일부분을 호출, 시작 인덱스, 끝 인덱스를 입력함.
- Concatenation : 연결을 활용하여, 두개 이상 결합하여, Concat으로 처리
- Equality : 객체의 참고를 통하여 동일한 것을 가리키는지 확인
-
더보기
public class StringOperationsExample { public static void main(String[] args) { // Substring 예제 String str = "Hello, World!"; String subStr = str.substring(0, 7); // Hello, String subStr = str.substring(7); // World! // Concatenation 예제 String str1 = "Hello"; String str2 = "World"; String combinedStr = str1.concat(", ").concat(str2); // Hello, World // Equality 예제 String s1 = "Hello"; String s2 = "Hello"; String s3 = new String("Hello"); System.out.println(s1 == s2); // true System.out.println(s1 == s3); // false (새로운 객체 생성으로 인해 참조가 다름) System.out.println(s1.equals(s3)); // true (문자열 내용이 같음) } }
'Java' 카테고리의 다른 글
[Final] Interface (0) | 2024.06.10 |
---|---|
[Final] Abstract Classes (0) | 2024.05.07 |
[MidTerm] 0. Introduction to Java (0) | 2024.04.22 |
TypeCasting (1) | 2024.02.08 |
Constants (0) | 2024.02.08 |