Jini's Blog

Ing...

  • Home
  • Business
    • Internet
    • Market
    • Stock
  • Parent Category
    • Child Category 1
      • Sub Child Category 1
      • Sub Child Category 2
      • Sub Child Category 3
    • Child Category 2
    • Child Category 3
    • Child Category 4
  • Featured
  • Health
    • Childcare
    • Doctors
  • Home
  • Business
    • Internet
    • Market
    • Stock
  • Downloads
    • Dvd
    • Games
    • Software
      • Office
  • Parent Category
    • Child Category 1
      • Sub Child Category 1
      • Sub Child Category 2
      • Sub Child Category 3
    • Child Category 2
    • Child Category 3
    • Child Category 4
  • Featured
  • Health
    • Childcare
    • Doctors
  • Uncategorized

New Features Java 1.7(JDK 1.7)

 Jini     오전 1:10     JAVA     No comments   

■ The New Objects Class
  1) Objects.requireNonNull(T), Objects.requireNonNull(T, String)
    #. 아래 Person 객체 생성시 newLastName이나 newFirstName에 null이 들어갈 경우 NullPointerException 발생.
    #. Not Null임을 보장하고 있으므로 해당 Property를 사용할 때 Null 검사를 생략할 수 있음.
public class Person {

    private String lastName;
    private String firstName;
 
    public Person(final String newLastName, final String newFirstName) {
        this.lastName = Objects.requireNonNull(newLastName, "Last name cannot be null.");
        this.firstName = Objects.requireNonNull(newFirstName, "First name cannot be null.");
    }
 
    public String getLastName() {
        return lastName;
    }
    
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
    public String getFirstName() {
        return firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}


  2) Objects.toString(Object), Objects.toString(Object, String)
    #. 인자가 하나인 메소드는 전달받은 Object가 Not null인 경우 Object의 toString() 결과를 리턴하며, null인 경우 "null"을 리턴한다.
    #. 인자가 둘인 메소드는 전달받은 Object가 Not null인 경우 Object의 toString() 결과를 리턴하며, null인 경우 전발받은 String 메시지를 리턴한다.
    #. toString() 메소드를 Override할 때 Null 체크를 별도로 하지 않아도 되는 편리함이 있다.
public class Bar {
    private Foo foo;
    private Bar parent;

    @Override
    public String toString(){
        return "Bar {foo = " + (foo == null ? "null" : foo.toString()) + ", parent = " + (parent == null ? "o parent, orphan" : parent.toString()) + "}";
    }
}
위 코드를 아래와 같이 수정할 수 있다.
public class Bar {
    private Foo foo;
    private Bar parent;

    @Override
    public String toString(){
        return "Bar {foo = " + Objects.toString(foo) + ", parent = " + Objects.toString(parent, "no parent, orphan") + "}";
    }
}
소스출처

  3) Objects.hash(Object...), Objects.hashCode(Object)
    #. Objects.hash(Object...) : 입력받은 모든 Object들에 대한 HashCode를 리턴한다.
    #. Objects.hashCode(Object) : Object가 null이면 "0"을 리턴, 그렇지 않으면 Object.hashCode()를 리턴한다.
public class Bar {
    private Foo foo;
    private Bar parent;

    @Override
    public int hashCode(){
        int result = 17;

        result = 31 * result + (foo == null ? 0 : foo.hashCode());
        result = 31 * result + (parent == null ? 0 : parent.hashCode());

        return result;
    }
}
위 코드를 아래와 같이 수정할 수 있다.
public class Bar {
    private Foo foo;
    private Bar parent;

    @Override
    public int hashCode(){
        return Objects.hash(foo, parent);
    }
}
소스출처

  4) Objects.equals(Object,Object), Objects.deepEquals(Object,Object)
    #. Objects.equals(Object,Object) : 두 입력값이 null이면 true를 null이 아니면 Object.equal(Object) 결과를 리턴한다.
    #. Objects.deepEquals(Object,Object) : 두 입력값이 Arrays Type인 것을 제외하고는 Objects.equals() 메소드와 동일하다.
public class Bar {
    private Foo foo;
    private Bar parent;

    @Override
    public boolean equals(Object obj){
        if (obj == this) {
            return true;
        } 

        if (obj instanceof Bar) {
            Bar other = (Bar) obj; 

            if (foo != other.foo) {
                if (foo == null || !foo.equals(other.foo)) {
                    return false;
                }
            } 

            if (parent != other.parent) {
                if (parent == null || !parent.equals(other.parent)) {
                    return false;
                }
            } 

            return true;
        } 

        return false;
    }
}
위 소스를 아래와 같이 변경할 수 있다.
public class Bar {
    private Foo foo;
    private Bar parent;

    @Override
    public boolean equals(Object obj){
        if (obj == this) {
            return true;
        } 

        if (obj instanceof Bar) {
            Bar other = (Bar) obj; 

            return Objects.equals(foo, other.foo) && Objects.equals(parent, other.parent);
        } 

        return false;
    }
}
소스출처

  5) Objects.compare(T,T,Comparator c) : 입력받은 두 값이 null인 경우에도 정상동작하며, true를 리턴해준다. null이 아닌 경우에는 c.compare(T, T) 결과를 리턴한다.

■ Collections class에 아래 3개의 method 추가 : Iterator를 만들 때 Null인 경우 아래의 Method를 사용할 경우 Null 처리가 간편해짐
  1) Collections.emptyIterator
  2) Collections.emptyEnumeration
  3) Collections.emptyListIterator
import java.util.Collections;
import java.util.Iterator;
public class Main {

  public static void main(String[] args) {
  
  }
  public static Iterator getMy(){
    String nullFlag = null;
    if(nullFlag == null){
      return Collections.emptyIterator();
    }
    return null;
  }
}
소스출처

참고자료 Post

계속...

정리하기 전에 링크부터

http://www.rapidprogramming.com/technology/differences-between-java-16-java-17-jdk-16-jdk-17-934
http://radar.oreilly.com/2011/09/java7-features.html
http://marxsoftware.blogspot.kr/2011/03/jdk-7-new-interfaces-classes-enums-and.html
http://eclipse.or.kr/wiki/Java_1.7c
http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html
http://en.wikipedia.org/wiki/Java_version_history#Java_SE_7_.28July_28.2C_2011.29
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
최근 게시물 이전 게시물 홈

Popular Posts

  • Redmine Text Format
    1. 글자색상 변경 %{color:red}dev% 2. 음영색상 변경 %{background:lightgreen} lightgreen% 3. 문단 넘버링(띄어쓰기 주의) # 큰 제목 ## 큰제목의 하위 제목 # 두번째 큰 제목 # ...
  • 오라클 한글깨짐 현상해결
    1. 레지스트리 편집기 실행 : 시작 -> 실행 -> regedit 2. HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE 하위 경로 폴더중 이름이 "NLS_LANG"인 속성의 데이터 확인 3. "...
  • 오브젝트 : 코드로 이해하는 객체지향 설계
    오브젝트 - 코드로 이해하는 객체지향 설계 / 조영호 지음 / 위키북스 객체지향에 대해 그 동안 잊고 있었던 것들을 상기시켜주고 새로운 인사이트를 줬으며 그 동안의 설계에 대해 돌이켜 보게 해준 유익한 책. 객체 사이의 의존성을 완전히 없애는 것이 정...
  • New Features Java 1.7(JDK 1.7)
    ■ The New Objects Class   1) Objects.requireNonNull(T), Objects.requireNonNull(T, String)     #. 아래 Person 객체 생성시 newLastName이나 newFirstNa...
  • MongoDB Array Query(With MongoTemplate)
    Mongo Collection 구조 컬렉션명 : bookstore { "_id": "1234567890", "books": [ { "bookId": ...

Recent Posts

Recent Posts Widget

Blog Archive

  • ►  2023 (4)
    • ►  3월 (1)
    • ►  2월 (1)
    • ►  1월 (2)
  • ►  2022 (1)
    • ►  2월 (1)
  • ►  2020 (8)
    • ►  7월 (1)
    • ►  4월 (3)
    • ►  3월 (4)
  • ►  2018 (1)
    • ►  7월 (1)
  • ►  2015 (1)
    • ►  5월 (1)
  • ▼  2014 (5)
    • ▼  8월 (1)
      • New Features Java 1.7(JDK 1.7)
    • ►  7월 (1)
    • ►  6월 (1)
    • ►  5월 (1)
    • ►  1월 (1)
  • ►  2013 (10)
    • ►  12월 (1)
    • ►  11월 (1)
    • ►  9월 (2)
    • ►  8월 (3)
    • ►  7월 (3)
  • ►  2012 (1)
    • ►  3월 (1)

Categories

  • 객체지향 (1)
  • 도서요약 (1)
  • 문법에서 문장까지 (2)
  • 일상 (1)
  • 자기계발 (1)
  • 책 (1)
  • 키보드 (1)
  • 키크론 (1)
  • blogspot (2)
  • error (1)
  • exception (1)
  • GIT (1)
  • JAVA (6)
  • JUNIT (1)
  • K8 PRO RGB (1)
  • kafka (1)
  • markdown (1)
  • mongodb (2)
  • mongotemplate (1)
  • optional (1)
  • Oracle (4)
  • Redmine (1)
  • spring (1)
  • stackedit (1)
  • troubleshooting (1)
  • Visual Studio (1)
  • Webflux (1)

Unordered List

Pages

  • 홈

Text Widget

Categories

Tags

Facebook

  • Home
  • Features
  • _Multi DropDown
  • __DropDown 1
  • __DropDown 2
  • __DropDown 3
  • _ShortCodes
  • _SiteMap
  • _Error Page
  • Documentation
  • Video Documentation
  • Download This Template

Footer Menu Widget

  • Home
  • About
  • Contact Us

Social Plugin

JINI. Powered by Blogger.

Copyright © Jini's Blog | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates