Final Exam

자바프로그래밍1 기말고사
범위: 중간고사문제 포함 – 중간고사 이후 끝까지 (강의노트, 숙제, 수업블로그, 예제 중심으로)
일시: 2024/06/12(수) 오후 5:30-7:00
장소: 인문관 209호

HW5 보고서 출력물은 기말고사때 제출합니다

Exceptions

ArrayIndexOutOfBoundsException 배열의 범위를 벗어난 접근할 때 발생

ArithmeticException 산술 연산 오류 에 의해 발생 (예를 들어, 0으로 정수를 나누는 경우)

NullPointerException null 객체를 사용하려고 시도할 때 발생

ClassCastException 변환할 수 없는 클래스로 객체 변환을 시도할 때 발생

NumberFormatException String을 숫자(number)로 변환할 수 없을 때 발생 (예를 들어, “1.23”을 Integer 변환을 시도할 때)

IllegalArgumentException 메소드 인자(argument) 유형을 잘못 사용할 경우

InputMismatchException (Scanner 클래스) 잘못된 입력일 때 발생

IOException 입출력(IO) 오류에 의해 발생

FileNotFoundException 지정된 경로에서 파일을 찾지 못할 때 발생

java.io.InputStream.read(byte[] b, int off, int len)

https://www.tutorialspoint.com/java/io/inputstream_read_byte_len.htm

The java.io.InputStream.read(byte[] b, int off, int len) method reads upto len bytes of data from the input stream into an array of bytes. If the parameter len is zero, then no bytes are read and 0 is returned; else there is an attempt to read at least one byte. If the stream is at the end of the file, the value returned is -1.

Parameters

  • b − The destination byte array.
  • off − The start offset in array b at which the data is written.
  • len − The number of bytes to read.

Return Value

The method returns the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Exception

  • IOException − If an I/O error occurs.
  • NullPointerException − If b is null.
  • IndexOutOfBoundsException − If off is negative, len is negative, or len is greater than b.length – off.

 

Difference between a module and a package

https://learnjava.co.in/what-is-the-difference-between-a-module-and-a-package/

Package Module
A package cannot be deployed by itself A module can be deployed by itself
A package groups together related classes A module groups together related packages
Packages are present in Java right from the beginning Modules were added by Java 9
Packages were added to keep related classes together and to allow developers to have a class with the same name in a different packages Modules were added for security reasons and to reduce the size of the JDK
Classes defined within a package are accessible via reflection even if they are private Classes defined within a module are not accessible outside the module via reflection
Packages do not require a package descriptor Modules require a module descriptor which is a file called module-info.java

 

String immutable vs StringBuilder mutable


// String immutable vs. StringBuilder mutable
// + operator (new StringBuilder(String.valueOf(str1)).append(str2).toString();
String a = "Hello";
String b = "Hello";
String c = a;
String d = "Hel" + "lo";
String e = "Hello" + "";
String f = a + ""; // String + StringLiteral => new String
String g = "";
String h = a + g; // String + String -> new String
String s = new String("Hello"); // new String
String t = new String("Hello"); // new String
String u = s + new String(""); // String + String -> new String
String v = s + new String(""); // String + String -> new String
String x = s.concat("~"); // concat creates new String
String y = s.concat("~"); // concat creates new String
System.out.println(getReference(a) + " a=" + a + " hashCode=" + a.hashCode());
System.out.println(getReference(b) + " b=" + b + " hashCode=" + b.hashCode());
System.out.println(getReference(c) + " c=" + c + " hashCode=" + c.hashCode());
System.out.println(getReference(d) + " d=" + d + " hashCode=" + d.hashCode());
System.out.println(getReference(e) + " e=" + e + " hashCode=" + e.hashCode());
System.out.println("a==e " + (a == e));
System.out.println(getReference(f) + " f=" + f + " hashCode=" + f.hashCode());
System.out.println("a==f " + (a == f));
System.out.println(getReference(g) + " g=" + g + " hashCode=" + g.hashCode());
System.out.println(getReference(h) + " h=" + h + " hashCode=" + h.hashCode());
System.out.println("a==h " + (a == h));
System.out.println(getReference(s) + " s=" + s + " hashCode=" + s.hashCode());
System.out.println("a==s " + (a == s));
System.out.println(getReference(t) + " t=" + t + " hashCode=" + t.hashCode());
System.out.println("s==t " + (s == t));
System.out.println(getReference(u) + " u=" + u + " hashCode=" + u.hashCode());
System.out.println(getReference(v) + " v=" + v + " hashCode=" + v.hashCode());
System.out.println("u==v " + (u == v));
System.out.println(getReference(x) + " x=" + x + " hashCode=" + x.hashCode());
System.out.println(getReference(y) + " y=" + y + " hashCode=" + y.hashCode());
System.out.println("x==y " + (x == y));

String str1 = "P";
String str2 = "P";
String str3 = str1 + str2;
String str4 = str1.concat(str2);
System.out.println(getReference(str1) + " str1=" + str1 + " hashCode=" + str1.hashCode());
System.out.println(getReference(str2) + " str2=" + str2 + " hashCode=" + str2.hashCode());
System.out.println(getReference(str3) + " str3=" + str3 + " hashCode=" + str3.hashCode());
System.out.println(getReference(str4) + " str4=" + str4 + " hashCode=" + str4.hashCode());
System.out.println("str3==str4 " + (str3 == str4));

 

 
java.lang.String@5a39699c a=Hello hashCode=69609650
java.lang.String@5a39699c b=Hello hashCode=69609650
java.lang.String@5a39699c c=Hello hashCode=69609650
java.lang.String@5a39699c d=Hello hashCode=69609650
java.lang.String@5a39699c e=Hello hashCode=69609650
a==e true
java.lang.String@3cb5cdba f=Hello hashCode=69609650
a==f false
java.lang.String@56cbfb61 g= hashCode=0
java.lang.String@1134affc h=Hello hashCode=69609650
a==h false
java.lang.String@d041cf s=Hello hashCode=69609650
a==s false
java.lang.String@129a8472 t=Hello hashCode=69609650
s==t false
java.lang.String@1b0375b3 u=Hello hashCode=69609650
java.lang.String@2f7c7260 v=Hello hashCode=69609650
u==v false
java.lang.String@2d209079 x=Hello~ hashCode=-2137068020
java.lang.String@6bdf28bb y=Hello~ hashCode=-2137068020
x==y false
java.lang.String@6b71769e str1=P hashCode=80
java.lang.String@6b71769e str2=P hashCode=80
java.lang.String@2752f6e2 str3=PP hashCode=2560
java.lang.String@e580929 str4=PP hashCode=2560
str3==str4 false