Object 클래스
Class 클래스
래퍼(wrapper) 클래스
java.util.Objects 클래스
java.util.Scanner 클래스
java.util.StringTokenizer 클래스
java.math.BigInteger 클래스
java.math.BigDecimal 클래스
package me.whiteship.livestudy.javalangpackage;
public class User {
int id;
String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public static void main(String[] args) {
User user1 = new User(1001,"홍길동");
User user2 = new User(1001,"홍길동");
System.out.println(user1.equals(user2));
}
}
output
false
package me.whiteship.livestudy.javalangpackage;
public class User {
int id;
String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
@Override
public boolean equals(Object obj) {
if( obj instanceof User){
return this.getId() == ((User) obj).getId();
}else{
return false;
}
}
public static void main(String[] args) {
User user1 = new User(1001,"홍길동");
User user2 = new User(1001,"홍길동");
System.out.println(user1.equals(user2));
}
}
output
true
public native int hashCode();
기본동작: JVM이 부여한 코드값. 인스턴스가 저장된 가상머신의 주소를 10진수로 반환
목적: 두 개의 서로 다른 메모리에 위치한 객체가 동일성을 갖기 위해
해시코드란 JVM이 인스턴스를 생성할 때 메모리 주소를 변환해서 부여하는 코드이다.
해시함수는 찾는 값을 입력하면, 그 값이 저장된 위치를 알려주는 해시코드(hash code)를 반환한다.
일반적으로 해시코드가 같은 두 객체가 존재하는 것이 가능하지만, Object 클래스에 정의된 hashCode 메서드는 객체의 주소값을 이용해서 해시코드를 만들어 반환하기 때문에 서로 다른 두 객체는 같은 해시코드를 가질 수 없다.
실제 메모리 주소값과는 별개의 값이며 실제 메모리 주소는 System 클래스의 identityHashCode()로 확인할 수 있다.
자바에서의 동일성은 equals()의 반환값이 true, hashCode() 반환값이 동일함을 의미한다.
그래서 일반적으로 equals() 와 hashCode()는 함께 override한다.
String 클래스는 문자열의 내용이 같으면 같은 해시코드를 반환하도록 hashCode 메서드가 오버라이딩 되어있기 때문에 문자열의 내용이 같은 str1과 str2에 대해 hashCode()를 호출하면 항상 동일한 해시코드 값을 얻는다.
package me.whiteship.livestudy.javalangpackage;
public class User {
int id;
String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
@Override
public boolean equals(Object obj) {
if( obj instanceof User){
return this.getId() == ((User) obj).getId();
}else{
return false;
}
}
public static void main(String[] args) {
User user1 = new User(1001,"홍길동");
User user2 = new User(1001,"홍길동");
System.out.println(user1.equals(user2));
System.out.println("user1.hashCode() = "+ user1.hashCode());
System.out.println("user2.hashCode() = "+ user2.hashCode());
}
}
output
true
user1.hashCode() = 1554874502
user2.hashCode() = 1846274136
package me.whiteship.livestudy.javalangpackage;
public class User {
int id;
String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
@Override
public boolean equals(Object obj) {
if( obj instanceof User){
return this.getId() == ((User) obj).getId();
}else{
return false;
}
}
@Override
public int hashCode() {
return getId();
}
public static void main(String[] args) {
User user1 = new User(1001,"홍길동");
User user2 = new User(1001,"홍길동");
System.out.println(user1.equals(user2));
System.out.println("user1.hashCode() = "+ user1.hashCode());
System.out.println("user2.hashCode() = "+ user2.hashCode());
System.out.println("System.identityHashCode(user1) = " + System.identityHashCode(user1));
System.out.println("System.identityHashCode(user2) = " + System.identityHashCode(user2));
}
}
output
true
user1.hashCode() = 1001
user2.hashCode() = 1001
System.identityHashCode(user1) = 1554874502
System.identityHashCode(user2) = 1846274136
Integer integer1 = 100;
Integer integer2 = 100;
System.out.println("integer1.hashCode() = " + integer1.hashCode());
System.out.println("integer2.hashCode() = " + integer2.hashCode());
output
integer1.hashCode() = 100
integer2.hashCode() = 100
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
package me.whiteship.livestudy.javalangpackage;
public class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public static void main(String[] args) {
Book book = new Book("하하하하", "최고");
System.out.println(book.toString());
}
}
output
me.whiteship.livestudy.javalangpackage.Book@5cad8086
FQCN@해시코드
가 출력된다. String s = "하이하이";
System.out.println(s.toString());
output
하이하이
[참고링크] https://javacan.tistory.com/entry/31
protected native Object clone() throws CloneNotSupportedException;
x.clone()!=x
x.clone().getClass()==x.getClass()
CloneNotSupportedException
예외 객체를 던진다. Cloneable 인터페이스가 구현되어있으면 원본과 같은 객체를 새로 생성하고, 모든 필드 멤버들을 원본의 필드와 완전히 같은 값으로 초기화한다. CloneNotSupportedException
예외를 발생시킨다.예제코드#1
배열을 포함하는 객체의 클로닝
package me.whiteship.livestudy.javalangpackage;
public class MyNumbers implements Cloneable{
private int[] num = null;
public MyNumbers() {
num = new int[10];
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) {
}
}
배열을 포함하는 클래스의 클로닝은 배열의 복사를 따로 제어해주어야 한다.
Object.clone() 메서드는 배열의 레퍼런스만을 복사하므로 배열자체는 복사되지 않는다.
배열의 내용이 변경되지 않는다면 문제가 없지만 배열의 내용이 바뀔 경우 원본과 복사본이 같이 영향을 받는다.
따라서 좀더 깊게 deep cloning
을 시도해야 한다.
위 코드는 이 객체의 clone() 메서드는 MyNumbers 객체의 복사본을 만들지만 배열에 대해서는 새로운 배열을 생성하지 않고 원본에서 생성된 배열을 공유하게 된다.
원본의 배열을 가리키는 num 레퍼런스의 레퍼런스 값만 복사하기 때문이다.
아래 코드는 shallow cloning을 deep cloning으로 바꾼것이다.
@Override
protected MyNumbers clone() throws CloneNotSupportedException {
MyNumbers myObj = (MyNumbers) super.clone();
myObj.num = (int[])num.clone();
return myObj;
}
예제코드#2
객체 레퍼런스를 포함하는 객체의 클로닝
package me.whiteship.livestudy.javalangpackage;
public class MyData implements Cloneable{
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package me.whiteship.livestudy.javalangpackage;
public class MyClass implements Cloneable{
private MyData myData = null;
public MyClass() {
myData = new MyData();
}
@Override
protected MyClass clone() throws CloneNotSupportedException {
MyClass obj = (MyClass)super.clone();
obj.myData = (MyData) myData.clone();
return obj;
}
}
예제코드#3
Clollection 계열의 객체를 포함하는 객체의 클로닝
package me.whiteship.livestudy.javalangpackage;
import java.util.Vector;
public class MyClass implements Cloneable{
private Vector vector = null;
public MyClass() {
vector = new Vector();
}
@Override
protected MyClass clone() throws CloneNotSupportedException {
MyClass obj = (MyClass)super.clone();
obj.vector = (Vector)vector.clone();
return obj;
}
}
public Point clone() { // 반환타입을 Object에서 Point로 변경
Object obj = null;
try {
obj = super.clone();
} catch(CloneNotSupportedException e) {}
return (Point)obj; //Point 타입으로 형변환
package me.whiteship.livestudy.javalangpackage;
public class Point {
@Override
protected Point clone() throws CloneNotSupportedException {
Object obj = null;
obj = super.clone();
return (Point) obj;
}
public static void main(String[] args) throws CloneNotSupportedException {
Point org = new Point();
Point copy = org.clone();
}
}
Point copy = (Point)original.clone(); -> Point copy = original.clone();
[참고]https://joont.tistory.com/165
public final class Class implements ... { // Class 클래스
...
}
package me.whiteship.livestudy.javalangpackage;
public class User {
public String id;
public String pwd;
public String name;
public Integer birthDate;
public User() { }
public User(String id, String pwd, String name, Integer birthDate) {
this.id = id;
this.pwd = pwd;
this.name = name;
this.birthDate = birthDate;
}
@Override
public String toString() {
return super.toString();
}
// getter,setter 생략
}
Class.forName("전체경로") or 클래스이름.class
자신이 속한 클래스의 Class 객체를 반환하는 메서드인데. Class 객체는 이름이 'Class' 인 클래스의 객체이다.
Class 객체는 클래스의 모든 정보를 담고 있으며, 클래스당 1개만 존재한다.
그리고 클래스 파일이 클래스 로더에 의해서 메모리에 올라갈때, 자동으로 생성된다.
클래스 로더는 실행 시에 필요한 클래스를 동적으로 메모리에 로드하는 역할을 한다.
먼저 기존에 생성된 클래스 객체가 메모리에 존재하는지 확인하고, 있으면 객체의 참조를 반환하고 없으면 클래스패스에 지정된 경로를 따라서 클래스 파일을 찾는다.
못찾으면 ClassNotFoundException이 발생하고, 찾으면 해당 클래스 파일을 읽어서 Class 객체로 변환한다.
파일 형태로 저장되어있는 클래스르 읽어서 CLASS 클래스에 정의된 형식으로 변환하는 것이다.
즉, 클래스 파일을 읽어서 사용하기 편한 형태로 저장해 높은것이 클래스 객체이다.(클래스 파일을 메모리에 로드하고 변환하는 일은 클래스 로더가 한다.)
Class 객체를 얻는 방법
Class c1 = new Book().getClass(); // 생성된 객체로부터 얻는 방법
Class c2 = Book.getClass(); // 클래스 리터럴(*.class)로 부터 얻는 방법
Class c3 = Class.forName("Book"); // 클래스이름으로 부터 얻는 방법
package me.whiteship.livestudy.javalangpackage;
import java.lang.reflect.*;
public class User {
public String id;
public String pwd;
public String name;
public Integer birthDate;
public User() { }
public User(String id, String pwd, String name, Integer birthDate) {
this.id = id;
this.pwd = pwd;
this.name = name;
this.birthDate = birthDate;
}
@Override
public String toString() {
return super.toString();
}
public String getId() {
return id;
}
public String getPwd() {
return pwd;
}
public String getName() {
return name;
}
public Integer getBirthDate() {
return birthDate;
}
public static void main(String[] args) throws ClassNotFoundException{
Class user = Class.forName("me.whiteship.livestudy.javalangpackage.User");
System.out.println("Field List");
for (Field field : user.getFields()) {
System.out.println(field.getName());
}
System.out.println("\nConstructor List");
for (Constructor constructor : user.getConstructors()) {
System.out.println("개수 " + constructor.getParameterCount() + "=");
for (Class parameter : constructor.getParameterTypes()) {
System.out.println(parameter.getName());
}
}
System.out.println("\nMethod List");
for (Method method : user.getMethods()) {
System.out.println(method.getName());
}
}
}
output
Field List
id
pwd
name
birthDate
Constructor List
개수 0=
개수 4=
java.lang.String
java.lang.String
java.lang.String
java.lang.Integer
Method List
getPwd
getBirthDate
main
toString
getName
getId
wait
wait
wait
equals
hashCode
getClass
notify
notifyAll
Process finished with exit code 0
기본 타입 | 래퍼 클래스 |
---|---|
byte | Byte |
char | Charactre |
int | Integer |
float | Float |
double | Double |
boolean | Boolean |
long | Long |
short | Short |
Integer num = new Integer(7);// 박싱
int n = num.intValue(); // 언박싱
System.out.println(n);
Integer num = 7;// 자동박싱
int n = num; // 자동언박싱
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(10); // 오토박싱
int value = list.get(0); // 언박싱
String str1 = "10";
String str2 = "10.5";
String str3 = "true";
byte b = Byte.parseByte(str1);
int i = Integer.parseInt(str1);
short s = Short.parseShort(str1);
long l = Long.parseLong(str1);
float f = Float.parseFloat(str2);
double d = Double.parseDouble(str2);
boolean bool = Boolean.parseBoolean(str3);
래퍼 클래스의 주요 용도는 기본 타입의 값을 박싱 해서 포장 객체로 만드는것이지만, 문자열을 기본타입값으로 변환할때에도 사용된다. 대부분의 래퍼 클래스에는 paser+기본타입명으로 되어있는 정적메서드가 있다.
이 메서드는 문자열을 매개 값으로 받아 기본 타입 값으로 변환한다.
값 비교
Integer integer = new Integer(10);
Integer integer1 = new Integer(10);
int i = 10;
System.out.println("래퍼클래스 == 기본타입 : "+(integer == i)); //true
System.out.println("래퍼클래스.equals(기본타입) : "+integer.equals(i)); //true
System.out.println("래퍼클래스 == 래퍼클래스 : "+(integer == integer1)); //false
System.out.println("래퍼클래스.equals(래퍼클래스) : "+integer.equals(integer1)); //true
static
이다. static boolean isNull(Object obj)
static boolean nonNull(Object obj)
static <T> requireNonNull(T obj)
static <T> requireNonNull(T obj, String message)
static int compare(Object a, Object b, Comparator c)
이 메서드는 객체 a와 b를 비교하는데, 두 객체를 비교하는데 사용할 비교기준이 필요하다.
그 역할을 하는 것이 Comparator이다.
Objects 클래스의 equals 메서드는 Object 클래스와는 달리, null 검사를 하지 않아도 된다.
equals() 메서드 내부에서 a와 b의 null 검사를 하기 때문에 따로 null 검사를 위한 조건식을 넣이 않아도 된다.
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
Scanner(String source)
Scanner(File source)
Scanner(InputStream source)
Scanner(Readable source)
Scanner(ReadableByteChannel source)
Scanner(Path source) .. JDK 1.7부터 추가
String str="this-=string-includes=delims";
StringTokenizer stk=new StringTokenizer(str,"-=");
System.out.println(str);
System.out.println();
System.out.println("total tokens:"+stk.countTokens());
System.out.println("================tokens==================");
while(stk.hasMoreTokens()){
System.out.println(stk.nextToken());
}
System.out.println("total tokens:"+stk.countTokens());
output
total tokens:4
================tokens==================
this
string
includes
delims
total tokens:0
정수형으로 표현할 수 있는 값의 한계가 있다.
가장 큰 정수형 타입인 long으로 표현할 수 있는 값은 10진수로 19자리 정도이다.
이 값도 상당히 크지만 과학적 계산에서는 더 큰 값을 다뤄야할 때가 있다.
그럴때 사용하면 좋은것이 BigInteger이다.
BigInteger는 내부적으로 int배열을 사용해서 값을 다룬다.
그래서 long타입보다 훨씬 큰 값을 다룰수 있따.
대신 성능은 long보다 떨어질수밖에 없다
final int signum; // 부호. 1(양수), 0, -1(음수) 셋 중의 하나
final int[] mag; // 값
2의 보수
의 형태로 표현한다.[참고링크]https://ryan-han.com/post/java/java-lang/
[클래스참고]https://jongmin92.github.io/2018/04/07/Java/java-lang-package-and-useful-class/
[wrapper참고]https://coding-factory.tistory.com/547