1.
mport java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList myList = new ArrayList();
String[] myArray;
try{
while (true){
myList.add("My string");
}
}catch (RuntimeException e){
System.out.println("catch RunTimeException");
}catch (Exception e){
System.out.println("catch Exception");
}
System.out.println("Ready to use ");
}
}
runtime error is thrown in the thread "main"
- ArrayList myList에 "My String"을 계속 추가하는 while (true) 무한 반복문\
- myList가 점점 커지면서 JVM이 메모리를 다 쓰게 되면, 결국 메모리 부족(OutOfMemoryError) 에러
- 이 OutOfMemoryError는 런타임 오류로, Exception의 서브클래스가 아닌 Error 클래스의 서브클래스이는 RuntimeException도 아니고 Exception도 아니기 때문에, catch 블록에서 잡을 수 X
2.
public class Test {
public static void checkAge(List<Person> personList, Predicate<Person> pearsonPredicate){
for(Person p : personList){
if(pearsonPredicate.test(p)){
System.out.println(p.name + " ");
}
}
}
public static void main(String[] args){
List<Person> personList = Arrays.asList(new Person("Hank", 45),
new Person("Charlie", 40),
new Person("Smith", 38)
);
checkAge(personList, p -> p.getAge() > 40);
}
}
자바의 람다식 기본 구조는 (매개변수) -> { 실행할 코드 } 형태
만약 매개변수의 타입을 알고 있다면 명시적으로 적을 필요가 X
3.
String str2 = " ";
str2.trim(); // if we use str2 = str2.trim(); the answer will be true true
System.out.println(str2.equals("") + " " + str2.isEmpty());
trim() 양쪽 끝의 공백을 제거한 문자열을 반환하지만, 원래 str2 변수의 값을 수정하지는 X >>> 새로 할당해야함
String은 불변(immutable) 이기 때문에, trim() 메서드 호출만으로는 str2의 내용이 바뀌지 않음
4.
String[] strings = new String[2];
int idx = 0;
for(String s : strings){
strings[idx].concat(" element " + idx);
idx++;
}
for (idx = 0; idx < strings.length; idx++){
System.out.println(strings[idx]);
}
- null.concat()을 호출하는 것과 같음
- NullPointerException 발생
- concat()의 결과를 저장하지도 않음
5.
class Vehicle {
int x;
Vehicle() {
this(10); //line n1 - Vehicle(int) 생성자 호출
}
Vehicle(int x) {
this.x = x;
}
}
class Car extends Vehicle {
int y;
Car() {
super(); // Vehicle() 호출
this(20); // line n2 - 에러! this()와 super()는 첫 줄에만 올 수 있음
>>>>>>>>>>>>>>>>수정안
Car() {
super(); // Vehicle() 호출
this.y = 20; // 직접 값 설정
}
}
Car(int y) {
this.y = y;
}
public String toString() {
return super.x + ":" + this.y;
}
}
- this()나 super() 호출은 생성자의 첫 줄에만 가능
- this()와 super()를 같이 사용할 수 없음
>> Compilation fails at line n2
6.
class MyString {
String msg;
MyString(String msg) {
this.msg=msg;
}
}
// Since both classes are in same package
public class Test {
public static void main(String[] args) {
System.out.println("Hello "+ new StringBuilder("Java SE 8"));
System.out.println("Hello "+ new MyString("Java SE 8"));
}
}
StringBuilder의 경우:
- toString() 메소드가 구현되어 있음
- 내부 문자열을 반환
- "Hello Java SE 8" 출력
MyString의 경우:
- toString() 메소드를 오버라이드하지 않음
- Object 클래스의 기본 toString() 사용
- 클래스이름@해시코드 형태로 출력
- "Hello MyString@해시코드" 출력
class Object {
// Object 클래스의 기본 toString() 메소드
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
}
// Object의 toString()을 오버라이딩
@Override
public String toString() {
return msg;
}
7.
byte → short → int → long → float → double
8.
public class B {
private int doStuff() { // 메소드가 private인데 앞에 int도 private
private int x = 100; // 여기서 문제! 메소드 안에서 private 사용 불가
return x++;
}
}
지역변수는 private 불가
'Certification > OCAJP' 카테고리의 다른 글
OCAJP 준비 (Q106 - Q125) (8) | 2024.11.13 |
---|---|
OCAJP 준비 (Q91 - Q105) (1) | 2024.11.07 |
OCAJP 준비 (Q66 - Q90) (2) | 2024.11.07 |
OCAJP 준비 (Q26 - Q44) (0) | 2024.11.05 |
OCAJP 준비 (Q01 - Q25) (0) | 2024.11.04 |