자바 API(System.out.prinln() 뜯어보기🔨)
카테고리:
tag #
2023년 10월 17일
// Links to the libraries needed
public final class System {
private static native void registerNatives();
static {
registerNatives();
}
private System() {
}
public static final InputStream in = null;
public static final PrintStream out = null;
...
}
public class PrintStream extends FilterOutputStream{
...
public void println() {
newLine();
}
/**
* Prints a boolean and then terminates the line. This method behaves as
* though it invokes {@link #print(boolean)} and then
* {@link #println()}.
*
* @param x The {@code boolean} to be printed
*/
public void println(boolean x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints a character and then terminates the line. This method behaves as
* though it invokes {@link #print(char)} and then
* {@link #println()}.
*
* @param x The {@code char} to be printed.
*/
public void println(char x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints an integer and then terminates the line. This method behaves as
* though it invokes {@link #print(int)} and then
* {@link #println()}.
*
* @param x The {@code int} to be printed.
*/
public void println(int x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints a long and then terminates the line. This method behaves as
* though it invokes {@link #print(long)} and then
* {@link #println()}.
*
* @param x a The {@code long} to be printed.
*/
public void println(long x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints a float and then terminates the line. This method behaves as
* though it invokes {@link #print(float)} and then
* {@link #println()}.
*
* @param x The {@code float} to be printed.
*/
public void println(float x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
...
public void println(String x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints an Object and then terminates the line. This method calls
* at first String.valueOf(x) to get the printed object's string value,
* then behaves as
* though it invokes {@link #print(String)} and then
* {@link #println()}.
*
* @param x The {@code Object} to be printed.
*/
public void println(Object x) {
String s = String.valueOf(x);
if (getClass() == PrintStream.class) {
// need to apply String.valueOf again since first invocation
// might return null
writeln(String.valueOf(s));
} else {
synchronized (this) {
print(s);
newLine();
}
}
}
...
}
public void println(int x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
헷갈려서 간단하게 getClass를 사용해보았습니다.
String s = "kimminje";
System.out.println(s.getClass());
-> class java.lang.String
println은 System.out을 사용하니 위에서의 getClass는 System.out 객체의 클래스를 반환하지 않을까? 시도해보겠습니다.
System.out.println(System.out.getClass());
->class java.io.PrintStream
if (System.out.getClass()== PrintStream.class){
System.out.println("true");
}
->true
public void print(int i) {
write(String.valueOf(i));
}