OCJP Silver
OCJP Silver
2024 8 28
package test;
import java.time.*;
public class Diary {
private Diary() { }
public static LocalDate getDate() {
return LocalDate.now();
}
}
public class DiaryTest {
public static void main(String[] args) {
System.out.println(Diary.getDate());
}
}
1. import
2. private Diary
3. Diary.class DiaryTest.class 2 .class
4. DiaryTest.class
5.
: 5.
public ( ) 1 public
) LocalDate.now() →
1. 2 8
2. 2 12
3. 2 16
4. 4 8
5. 4 12
6. 4 16
: 1. 2 8
this
private
private (
)
[Item.java]
[Test.java]
: this
this this.price (
) price
Item Test 4
static
line 2 static this (static
)
: 5. UnsupportedOperationException
: Arrays.asList() List
List List
UnsupportedOperationException List ( )
List Arrays.asList List List
List::of ( Java 9 )
List (Unmodifiable List) List
( )
List::removeIf ( Java 8 )
List Predicate true List
java.util.function.Predicate
class Player {
String name;
}
public class Test {
private static void changeName(Player p, String n) {
p.name = n;
}
public static void main(String[] args) {
Player player1 = new Player(); // a
Player player2 = new Player(); // b
Player player3 = new Player(); // c
player2 = player3; // player1 = a, player2 = c, player3 = c
player3 = null; // player1 = a, player2 = c, player3 = null
changeName(player1, "Fernando"); // player1 = a, player2 = c, player3 = null
// line 1
}
}
1. Player GC
2. 1
3. 2
4. 3
: 2. 1
: (GC)
JVM ( ) = GC
if-else 1
[if-else ]
int ans = 0;
if (v <= 0) ans = 10;
else ans = 11;
[ ]
line 3
class Region {
int id;
String name;
public Region(int id, String name) {
this.id = id;
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
var r1 = new Region(3, "Japan");
var r2 = new Region(3, "Japan");
var r3 = r1;
System.out.println(r1 == r2); // false
System.out.println(r1 == r3); // true
System.out.println(r1.id == r2.id); // true
System.out.println(r1.name == r2.name); // true
System.out.println(r1.name.equals(r2.name)); // true
}
}
1. 0
2. 1
3. 2
4. 3
5. 4
6. 5
: 5. 4
: == equals
== equals
String (==)
String ( var s = "abc"; )
( ) new
String == true
1. 123:true 123:true
2. 123:false 123:false
3. 123:false 123:true
4. 123:true 123:false
5.
6. NumberFormatException
7. ClassCastException
: 5.
var var ( )
for for
( null, , )
1. MyException
2. MyException
3. MyException
4. method() java.lang.Exception
5. method() MyException
6. method() MyException
:1 6
1. java.desktop
2. jdk.compiler
3. java.se
4. java.base
5. java.management
6. java.prefs
: 4. java.base
: Modular JDK
JDK Module
for (i : doubleArray) {
System.out.println(i);
}
:3
: for
for Collection
for ( : ) {
//
}
for
1. 1 3 5 2 4 6
2. 1 3 5 2 4
3. 1 3 5 2
4. 1 3 5 2 6
5. ArrayIndexOutoufBoundsException
6.
: 2. 1 3 5 2 4
continue break
for 2 break 6
System.out.println(numArr[0][1]); // 3
1. 21 80.0
2. 35 120.0
3. 49 160.0
4. 63 200.0
5. NumberFormatException
6.
: 3. 49 160.0
print 4
Test.print(7, 20.0f);
: static
static ( )
static showMessage() message
static static
.
.
[Parent.java]
package p1;
public abstract class Parent {
protected abstract void displayValue();
}
[Child.java]
package p2;
import p1.Parent;
public abstract class Child extends Parent {
// line 1
}
:1 2
: abstract
abstract abstract (
)
1. private Child
displayValue()
class Base {
private void print() {
System.out.print(" : ");
}
public void methodX() {
print();
}
}
public class Sub extends Base {
private void print() {
System.out.print("Hello: ");
}
public void methodY() {
print();
}
public static void main(String... args) {
Sub s = new Sub();
s.methodX();
s.methodY();
}
}
1. : Hello:
2. : :
3. Hello: :
4. Hello: Hello:
5.
: 1. : Hello:
: private
private private
private
private
[User.java]
package app.entity;
public class User {
private int no;
public User(int no) { this.no = no; }
public void no() { } // line 1
}
[Test.java]
package app;
import app.entity.*;
public class Test {
public static void main() {
int no = 4; // line 4
User user = new User(no);
}
}
( )
1. java /u01/work/build/app/Test
2. java app.Test
3. java -cp /u01/work/build app.Test
4. java -classpath /u01/work/build/app Test
5. java -p /u01/work/build -m Test
java javac
CLASSPATH -classpath ( -cp )
(FQCN)
javac -d (.class) -d
java
-classpath ( -cp ) -d
class A {
public void test() {
System.out.print("A ");
}
}
class B extends A {
public void test() {
System.out.print("B ");
}
}
public class C extends A {
public void test() {
System.out.print("C ");
}
public static void main(String[] args) {
A b1 = new A();
A b2 = new C();
b1 = (A) b2; //line 1
A b3 = (B) b2; //line 2
b1.test();
b3.test();
}
}
1. A B
2. A C
3. C C
4. line 1 ClassCastException
5. line 2 ClassCastException
: 5. line 2 ClassCastException
line 2
b2 (C ) B C B
ClassCastException
(
)
class Food {
String name;
public Food(String name) { this.name = name; }
public String toString() { return name; }
}
public class Test {
public static void main(String[] args) {
Food[] foods = { new Food("Wine"), new Food("Cheese"), new Food("Meat") };
System.out.print(foods);
System.out.print(" | " + foods[1]);
System.out.print(" | " + foods[1].name);
}
}
( ) 0 foods[0]
toString
toString Object Object
toString Food@28feb3fa ( )
Food toString name
Object toString
Arrays.toString
System.out.print(Arrays.toString(foods));
: 6. OutOfMemoryError
while (true)
myList.add("My String");
OutOfMemoryError (OOME)
JVM GC
java.lang.Error (
)
:1 4
: abstract
public abstract
public ( public
= )
abstract
abstract
abstract abstract (abstrace
)
5 print()
(
)
[Main.java]
package com.acme;
public class Main {
public static void main(String... args) {
System.out.println("Module Test");
}
}
[module-info.java]
module moduleA {
// …
}
java -p --module-path
-m --module
CLASSPATH JAR
requires
exports (=
)
JAR
JAR
[moduleA/module-info.java]
module moduleA {
requires moduleB;
}
[moduleB/module-info.java]
module moduleB {
exports api to moduleA;
}
1. moduleA api
2. moduleA moduleB public
3. moduleA moduleB api public
4. moduleB moduleA api public
5. moduleB moduleA api
6. moduleB api moduleA 2
: module-info.java
module-info.java module-info.java
module-info.class
line1:
1. list.for(System.out.print(s));
2. list.forEach(list -> System.out.print(v));
3. list.forEach( var -> {System.out.print(v);});
4. list.for((String s) -> System.out.print);
5. list.forEach(System.out::print);
6. list.forEach(list -> s);
: 5. list.forEach(System.out::print);
: List
java.util.function.Consumer
void
(Consumer)
( )
class MyClass {
public static void main(String... args) {
MyInterface i = () -> 'a';
System.out.println(" : " + i.getValue());
}
}
1. : a
2. : 'a'
3. : 97
4.
5. NullPointerException
6. NumberFormatException
: 3. : 97
MyInterface ( 1Z0–815
)
1. catch 1 finally
2. catch 1 catch 3 finally
3. catch 2 finally
4. catch 2 catch 3 finally
5. catch 1 catch 2 catch 3 finally
6. catch 3 finally
7.
: 7.
: try-catch
multi-catch
multi-catch catch
interface HelloInterface {
public void greeting();
}
1. Hello World
2.
3.
4. IlleaglAccessException
5. NoSuchMethodException
: 3.
: abstract
[Parent.java]
[Child.java]
1. 10
2. 0
3.
4. IlleagalArgumentException
5. NumberFormatException
: 2. 0
: int Integer
( ) int ( ) java.lang.Integer
modify int
check Child check
1. JRE
2. JDK
3. (IDE)
4. (IDE) JDK
5. (IDE) JRE
6. JDK JRE
: 2. JDK
: Java SE 11 JRE
[A.java]
public class A {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return getName();
}
}
[B.java]
1. John Peter
2. "John Peter "
3. A String setName
: 3. A String setName
String...
( ) 1
a.setName(args);
main args String a.setName(args);
String a A A String
B setName B