'JAVA'에 해당되는 글 43건

  1. 2016.01.14 javafx 예제 모음
  2. 2016.01.14 자바 멀티쓰레딩 관련 예제 링크
  3. 2011.05.13 문자열 중 숫자 또는 특정 패턴의 문자열 찾기
  4. 2010.05.20 Static Initialization in Java(tm)
  5. 2010.05.20 Singleton 패턴
  6. 2010.05.07 java.io.File을 이용한 디렉토리와 파일 검색(call back)
  7. 2010.02.03 Annotations - 어노테이션 1
  8. 2010.01.29 추상클래스(Abstract Class)와 인터페이스(Interface)
  9. 2010.01.13 struts2 를 사용하기 위한 jar 목록
  10. 2010.01.06 ValueStack & ActionContext
  11. 2010.01.06 인터셉터, Action Result 관계도
  12. 2010.01.04 Struts2의 개요
  13. 2009.11.24 Virtual Word Wrap-자동줄바꿈
  14. 2009.11.24 Oracle JDBC 드라이브 연결
  15. 2009.11.24 JAVA - 개발자가 놓치기 쉬운 자바의 개념, 기본원리
  16. 2009.11.24 clone()메소드
  17. 2009.11.24 equals() 메소드와 오버라이딩 instanceof 사용예제
  18. 2009.11.24 toString사용의 간단한 예제
  19. 2009.11.24 String->int, int->String
  20. 2009.11.24 클래스 인스턴스화
  21. 2009.11.24 Calendar, GregorianCalendar, dateFormat, SimpleDateFormat 을 이용한 시간 출력
  22. 2009.11.24 String, StringBuilder, StringTokenizer 클래스의 메소드 사용 예제
  23. 2009.11.21 Java Memory Model
  24. 2009.11.21 클레스간 상속 (객체 생성 타입 케스팅 예제)
  25. 2009.11.21 enum 열거 상수
  26. 2009.11.21 실습5 패키지 컴파일 cmd창
  27. 2009.11.21 성적표 출력 실습
  28. 2009.11.21 점과 점 사이의 거리 출력 실습
  29. 2009.11.21 간단한 성적 출력
  30. 2009.11.21 Random을 이용한 난수 발생
JAVA2016. 1. 14. 11:12

javafx 예제 모음 : https://gist.github.com/james-d

Posted by Tiwaz
JAVA2016. 1. 14. 11:03

자바 멀티쓰레딩 관련 예제 링크

http://www3.ntu.edu.sg/home/ehchua/programming/java/j5e_multithreading.html

Posted by Tiwaz
JAVA2011. 5. 13. 14:27


package string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PatternTest {
 public static void main(String[] args) {
  
  System.out.println("생성자로 초기화 : " + new Integer("2"));
  System.out.println("Object를 기본 int type으로 변환 : " + Integer.parseInt("20"));
  
  //아래는 java.lang.NumberFormatException 발생
  //System.out.println(new Integer("a1"));
  
  // 예문
  String str = "학0교abc 1종2e이 땡f34땡g땡5!6! hi7어서 8모이9자!@ ";
  String result = "";
  
  // ASCII 값 47~58은 숫자 0~10
  int i = 0;
  while(i<str.length()) {
   if(str.charAt(i)>=47 &&  str.charAt(i)<=58) {
    result += "\t" + str.charAt(i);
   }
   i++;
  }
  System.out.println("ASCII 로 찾은 숫자 : " + result);
  
  
  // reference으로 확인
  int j = 0;
  result = "";
  while(j<str.length()) {
   if(Character.isDigit(str.charAt(j))) {
    result += "\t" + str.charAt(j);
   }
   j++;
  }
  System.out.println("Character의 isDigit(ch)을 이용한 찾기 : " + result);
  
  
  // Pattern을 이용한 숫자 찾기
  result = "";
  String patternStr = "[\\d]";
  Pattern pattern = Pattern.compile(patternStr);
  Matcher matcher = pattern.matcher(str);
  while(matcher.find()) {
   result += "\t" + matcher.group(0);
  }
  System.out.println("Pattern 을 이용한 숫자 찾기 : " + result);
  
  // Pattern을 이용한 a~z 찾기
  result = "";
  patternStr = "[a-z]";
  pattern = Pattern.compile(patternStr);
  matcher = pattern.matcher(str);
  while(matcher.find()) {
   result += "\t" + matcher.group(0);
  }
  System.out.println("Pattern 을 이용한 a~z 찾기 : " + result);
  
 }
}

'JAVA' 카테고리의 다른 글

javafx 예제 모음  (0) 2016.01.14
자바 멀티쓰레딩 관련 예제 링크  (0) 2016.01.14
Static Initialization in Java(tm)  (0) 2010.05.20
Singleton 패턴  (0) 2010.05.20
java.io.File을 이용한 디렉토리와 파일 검색(call back)  (0) 2010.05.07
Posted by Tiwaz
JAVA2010. 5. 20. 16:29

이번 프로젝트를 진행하면서..

앞전에 들었던 적이 있었는데 막상 써먹지 않으면 내것이 아니란것을 절실히 느끼며..

찾았다-_-

static 한정자의 사용법..

아래 사이트에서 찾았다.. 해석은 간단하니 알아서-_-

//In Java(tm), you can use static initialization blocks in classes to do one-time per-class test for the class:

public class GlobalInstance {

 private static GlobalInstance instance;

 static {
  instance = new GlobalInstance();
 }
 
 private GlobalInstance() {
  
 }
 
 
 
 public static GlobalInstance getInstance() {
  return instance;
 }
}



http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html

Posted by Tiwaz
JAVA2010. 5. 20. 16:19

원본 - java 로 배우는 디자인 패턴 입문

Singleton 패턴
# 하나의 인스턴스만 생성되어야 하는 클래스 구현 패턴

# 사용 목적
프로그램 실행 시 하나의 클래스에 대한 인스턴스가 보통 여러 개 생성 및 사용 된다.하지만 반드시 하나의 인스턴스만 생성되어야 하는 클래스도 있다.
(예: 컴퓨터 자체를 표현한 클래스, 윈도 시스템을 표현한 클래스 등)

일반적으로 인스턴스 생성시 new MyClass( )를 한번만 실행하면 된다.
그러나, 1개의 인스턴스만 생성되도록 프로그램에 표현하고 싶을 때 Singleton 패턴을 사용한다.

# 규칙
private 맴버 변수
private 생성자
public 호출자

# 간단한 예제

## Main.calss
## Singleton Class 의 인스턴스를 호출하여 동일한 객체인지 유무 확인

package singleton;

public class Main {
 
 /**
  * main method
  * @param args
  */
 public static void main(String[] args) {
  
  Singleton obj1 = Singleton.getInstance();
  Singleton obj2 = Singleton.getInstance();
  
  if(obj1 == obj2) {
   System.out.println("같은 객체");
  } else {
   System.out.println("다른 객체");
  }
 }
}


## Singleton.class
## 1. private 맴버변수 선언
## 2. private 생성자 선언
## 3. public get 호출자 선언
## static 으로 선언하여 VM에서 전역 변수로 사용
## private 변수와 생성자를 사용하여 외부에서 변경 못하도록 지정.

package singleton;

public class Singleton {

 private static Singleton instance;

 /*
  * 정적 필드 - 최초 로드 시 한번만 초기화를 한다.
  */
 static {
  instance = new Singleton();
 }
 

 /**
  * 생성자
  */
 private Singleton() {
  
 }
 
 
 /**
  * get property
  */
 public static Singleton getInstance() {
  return instance;
 }
 
}

## 실행 결과 : 같은 객체

# 한정자가 필요한 이유
인스턴스가 하나만 존재한다는 것이 보증되면, 인스턴스 상호 간에 영향을 주어 생각지 못한 버그가 발생할 가능성이 없어진다.

# 유일한 하나의 인스턴스는 언제 생성되는가
프로그램 실행 후, 처음으로 Singleton.getInstance( ) 메소드가 호출되면, Singleton 클래스가 초기화되고, 이 때 static 필드인 singleton 필드가 초기화된다.

* synchronized

Posted by Tiwaz
JAVA2010. 5. 7. 01:42

프로젝트 수행중 간단하게 workspace 안의 파일 목록이 필요하여 간단하게 파일 검색하는 콜백 함수를 만들어 보았다;; 검색 및 출력은 제대로 되는데 검증은... ?



import java.io.File;

class FileSearch
{
    public static void main(String[] args)
    {
        printFiles(new File("C:/"));
    }

 

     // 파일을 매개 변수로 받는 메소드
     public static void printFiles(File f) 
    {
        // 매개변수가 디렉토리 일경우
        if(f.isDirectory()) 
        {
            String[] s = f.list();  // 디렉토리 내의 모든 파일의 list를 확인
   
            // 파일 목록에서 디렉토리일 경우
            //printFiles 메소드 호출 그리고 파일일 경우 단순 출력을 수행
            for(int i=0; i<s.length;i++) 
            {
                File f1 = new File(f.getPath() + "/" + s[i]);
    
                if(f1.isDirectory())
                {
                      System.out.println(f1);
                      printFiles(f1);
                 }
                 else
                 {
                       System.out.println("isFile " + f1.getPath() + "\t" + f1.getName());
                 }
            }
       }
      else
      {
           System.out.println("파일이지요~ " + f.getName());
       }
  
    }
}

'JAVA' 카테고리의 다른 글

Static Initialization in Java(tm)  (0) 2010.05.20
Singleton 패턴  (0) 2010.05.20
Annotations - 어노테이션  (1) 2010.02.03
추상클래스(Abstract Class)와 인터페이스(Interface)  (0) 2010.01.29
struts2 를 사용하기 위한 jar 목록  (0) 2010.01.13
Posted by Tiwaz
JAVA2010. 2. 3. 16:41
원본 : http://bioportal.weizmann.ac.il/course/prog2/tutorial/java/javaOO/annotations.html

Annotations

Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program, such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs.

Annotations use the form @annotation and may be applied to a program's declarations: its classes, fields, methods, and so on. The annotation appears first and often (by convention) on its own line, and may include optional arguments:

@Author("MyName")
class myClass() { }
or
@SuppressWarnings("unchecked")
void MyMethod() { }

Defining your own annotation is an advanced technique that won't be described here, but there are three built-in annotations that every Java programmer should know: @Deprecated, @Override, and @SuppressWarnings. The following example illustrates all three annotation types, applied to methods:

import java.util.List;

class Food {}
class Hay extends Food {}
class Animal {
    Food getPreferredFood() {
        return null;
    }
    /**
     * @deprecated document why the method was deprecated
     */
    @Deprecated
    static void deprecatedMethod() { }
}
class Horse extends Animal {
    Horse() {
        return;
    }
    @Override 
    Hay getPreferredFood() {
        return new Hay();
    }
    @SuppressWarnings("deprecation")
    void useDeprecatedMethod() {
        Animal.deprecateMethod(); //deprecation warning - suppressed
    }
}

@Deprecated

The @Deprecated (in the API reference documentation) annotation indicates that the marked method should no longer be used. The compiler generates a warning whenever a program uses a deprecated method, class, or variable. When an element is deprecated, it should be documented using the corresponding @deprecated tag, as shown in the preceding example. Notice that the tag starts with a lowercase "d" and the annotation starts with an uppercase "D". In general, you should avoid using deprecated methods — consult the documentation to see what to use instead.

@Override

The @Override (in the API reference documentation) annotation informs the compiler that the element is meant to override an element declared in a superclass. In the preceding example, the override annotation is used to indicate that the getPreferredFood method in the Horse class overrides the same method in the Animal class. If a method marked with @Override fails to override a method in one of its superclasses, the compiler generates an error.

While it's not required to use this annotation when overriding a method, it can be useful to call the fact out explicitly, especially when the method returns a subtype of the return type of the overridden method. This practice, called covariant return types, is used in the previous example: Animal.getPreferredFood returns a Food instance. Horse.getPreferredFood (Horse is a subclass of Animal) returns an instance of Hay (a subclass of Food). For more information, see Overriding and Hiding Methods (in the Learning the Java Language trail).

@SuppressWarnings

The @SuppressWarnings (in the API reference documentation) annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the previous example, the useDeprecatedMethod calls a deprecated method of Animal. Normally, the compiler generates a warning but, in this case, it is suppressed.

Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked". The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics. To suppress more than one category of warnings, use the following syntax:

@SuppressWarnings({"unchecked", "deprecation"})
Consult your compiler's documentation for a complete list of supported warning categories.

The more advanced uses of annotations includes writing a program that can read a Java program and process its annotations. To facilitate this task, release 5.0 of the JDK includes an annotation processing tool, called apt. In the next release of the JDK (codename Mustang) the functionality of apt will be a standard part of the Java compiler. For more information, see Getting Started with the Annotation Processing Tool (outside of the tutorial). For more information on the work-in-progress for Mustang, see the Language Model API (outside of the tutorial) and JSR 269: Pluggable Annotation Processing API (outside of the tutorial).

Posted by Tiwaz
JAVA2010. 1. 29. 09:33


추상클래스(Abstract Class)
 
-abstract 키워드를 사용

-몸체없는 추상메서드를 하나라도 포함하고 있으면 추상클래스가 된다.

-추상클래스가 되면 반드시 abstract 키워드를 명시해야 한다.

-클래스를 선언할 때  클래스 앞에 abstract 키워드를 사용하면 추상메서드를 포함하고 있지 않아도 추상클래스 된다.

-추상클래스는 객체(인스턴스)를 생성할 수 없다.

-추상메서드는 묵시적으로 가상메서드(Virtual Method)가 된다.

-추상메서드는 virtual 키워드를 사용할 수 없다.
 


인터페이스(Interface)

-클래스의 뼈대만을 가지고 있는 것

-인터페이스 내의 멤버는 모두 몸체가 없다.

-인테페이스의 멤버는 디폴트로 전부 public이다.

-내부에 필드를 가질 수 없다.

-멤버에 어떠한 접근자, 한정자도 붙이지 않는다.

Posted by Tiwaz
JAVA2010. 1. 13. 00:57

antlr
commons-beanutils  
common-chain
commons-logging
commons-logging-api
commons-validator
freemarker  
ognl   
oro
struts2-core  
struts-core  
xwork

'JAVA' 카테고리의 다른 글

Annotations - 어노테이션  (1) 2010.02.03
추상클래스(Abstract Class)와 인터페이스(Interface)  (0) 2010.01.29
ValueStack & ActionContext  (0) 2010.01.06
인터셉터, Action Result 관계도  (0) 2010.01.06
Struts2의 개요  (0) 2010.01.04
Posted by Tiwaz
JAVA2010. 1. 6. 00:27

★ ValueStack
☞ Xwork와 Struts2의 동적컨텍스트 구성의 핵심

☞ ActionContext맵의 내장맵으로 request, session, parameters 맵들과 기타 여러 맵들과 함꼐 ActionContext를 구성

☞ Action이 실행되는 동안 액션을 ValueStack에 저장하므로 ValueStack을 통해 Action프라퍼티의 값들에 엑세스가 가능

☞ 요청파라미터와 Action프라퍼티의 매핑에 사용됨


★ ActionContext
☞ Action이 실행되는 실행 환경

☞ 어플리케이션, 세션, 액션인보케이션, 파라미터, 로케일 정보와 같이 미리 정의된 값을
  얻거나 저장하기 위한 Thread Local Map

☞ 각각의 Action객체는 각각의 ActionContext에서 수행 됨

☞ 액션프락시가 액션인보케이션을 호출할때 생성되고 액션인보케이션 수행이 끝나면 제거됨

☞ 액션인보케이션이 수행되는 동안 쓰레드로 구성됨

☞ 인터셉터, 액션, 리절트가 실행시 사용자의 코드에서 액션컨텍스트를 얻기 위해서
  ActionContext.getContext()를 호출하면 됨
 

Posted by Tiwaz
JAVA2010. 1. 6. 00:24
☞ 클라이언트가 /webapp/my.action을 요청하면 인터셉터를 거쳐 액션에 요청이 전달되고 액션은 모델영역에 작업을 위임한다. 액션은 모델영역에서 작업의 결과를 받아 리절트를 호출하고 다시 인터셉터를 통해 클라이언트에게 응답한다.

'JAVA' 카테고리의 다른 글

struts2 를 사용하기 위한 jar 목록  (0) 2010.01.13
ValueStack & ActionContext  (0) 2010.01.06
Struts2의 개요  (0) 2010.01.04
Virtual Word Wrap-자동줄바꿈  (0) 2009.11.24
Oracle JDBC 드라이브 연결  (0) 2009.11.24
Posted by Tiwaz
JAVA2010. 1. 4. 22:46

★Struts2의 개요
☞ Struts 프레임워크는 http://struts.apache.org에서 배포하는
   MVC기반의 모델2를 이용한 웹어플리케이션 개발을 돕는 프레임워크

☞ 웹어플리케이션에서 MVC패턴의 Controller부분을 담당하며, Controller의 기능을
   계층화하여 제공

☞ XML기반의 환경설정 파일을 이용하여 코드의 직접 수정을 최소화

☞ 다양한 태그라이브러리 지원으로 View에서의 데이터표현 작업을 효율적으로 처리 가능

☞ 리소스번들을 이용한 다양한 언어의 표현으로 웹어플리케이션의 국제화를 지원

☞ 현재 Struts 1 , Struts 2 , Struts 2.1 버전이 공개되어 있음


★Struts1의 단점
☞ 프레임워크의 핵심객체들이 POJO기반이 아님으로 객체들이 프레임워크에 종속적

☞ 객체들의 프레임워크 종속성으로 인해 모듈별 테스트가 힘듦

☞ ActionForm의 사용으로 인한 불필요한 작업과 코드량 증가

☞ 태그라이브러리의 부족으로 다양한 활용이 용이치 않음

☞ 과다한 XML설정으로 설정 작업이 많음


** POJO (wiki
-POJO is an acronym for Plain Old Java Object. The name is used to emphasize that the object in question is an ordinary Java Object, not a special object, and in particular not an Enterprise JavaBean (especially before EJB 3).


★ Struts2의 특징 및 장점
☞ Struts1과는 달리 WebWork2에 기반한 MVC모델 채택
☞ POJO(Plain Old Java Object) 기반의 Action
☞ Zero Configuration 지향
☞ Annotation을 이용한 설정
☞ 변경된 환경설정파일이 웹컨테이너 재시작없이 반영됨으로 개발편이성 증대
☞ 별도의 ActionForm이 필요치 않음
☞ 액션실행 전/후에 실행할 코드를 인터셉터를 이용해 정의
☞ 테마와 템플릿을 포함한 강력한 태그라이브러리 제공
☞ AJAX구현의 편이성 증대 (Dojo프레임워크 포함, ajax테마)
☞ OGNL(Object Graph Navigation Language) 지원
☞ 뷰와 값의 연결을 위해 ValueStack 사용
☞ 다양한 플러그인 적용 가능 (JFreeChart, SiteMesh, JasperReport, Spring …)
☞ 구글쥬스(Google Guice)프레임워크를 채택한 DI(Dependancy Injection) 제공

★Struts2에서 주로 개발해야할 영역
☞ Action
  클라이언트의 요청과 Model, 또는 Model의 응답과 클라이언트 View의 연결 역할

☞ Result
  클라이언트에게 서버의 응답을 보여주기 위한 방법 제공

☞ Configuration
  주요 기본제공 설정 외에는 개발자가 직접 설정


★ Action
☞ 클라이언트의 요청에 대한 비즈니스 로직을 실행하기 위한 창구
☞ Struts2에서 액션은 Singleton이 아님
   - 싱글턴이 아니므로 액션객체에 프라퍼티를 이용해 상태정보를 저장하는 것이 가능
☞ POJO
☞ 기본 엔트리포인트로 execute() 메소드 사용
☞ Struts1이 .do URL을 사용하는데 반해 .action URL을 사용

<action name=“myAction” class=“mypackage.MyActionClass”/>


★ Result
☞ 액션을 통해 수행된 Model의 처리결과를 출력하는데 사용됨

☞ 출력의 형태에 따라 다양한 Result타입 제공


<result name=“success” type=“dispatcher”>/dir/result.jsp</result>


★ Configuration
☞ struts.xml
   - 액션과 리절트 페이지 정의
   - 프레임워크의 사용자가 설정해야할 대부분의 작업 설정
   - /WEB-INF/classes 디렉토리에 위치함

☞ FilterDispatcher
   - struts2의 요청진입점
   - web.xml에 필터로 등록함

   <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   </filter>
   <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
   </filter-mapping>

Posted by Tiwaz
JAVA2009. 11. 24. 22:25
Virtual Word Wrap-자동줄바꿈 Java

2009/03/31 11:13

작성자: 베레(lsj403)

출처:http://brajeshwar.com/2006/word-wrap-in-eclipse/

 

Word Wrap in Eclipse

 

It is still very buggy but we can live with it; you can enable Word Wrap in Eclipse. It screws up the line number, line height, etc but they will get back to their normal state once …

<SCRIPT type=text/javascript></SCRIPT> <SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </SCRIPT> <SCRIPT src="http://pagead2.googlesyndication.com/pagead/expansion_embed.js"></SCRIPT> <SCRIPT src="http://googleads.g.doubleclick.net/pagead/test_domain.js"></SCRIPT> <SCRIPT src="http://pagead2.googlesyndication.com/pagead/render_ads.js"></SCRIPT> <SCRIPT>window.google_render_ad();</SCRIPT>

It is still very buggy but we can live with it; you can enable Word Wrap in Eclipse. It screws up the line number, line height, etc but they will get back to their normal state once you turn off Virtual Word Wrap.

How to install?

  1. Open Eclipse
  2. Help > Software Updates > Find and Install
  3. Search for New Features to Install
  4. New Remote Site
  5. Enter the url - http://ahtik.com/eclipse-update/
  6. Install and Enjoy

It is available


'JAVA' 카테고리의 다른 글

인터셉터, Action Result 관계도  (0) 2010.01.06
Struts2의 개요  (0) 2010.01.04
Oracle JDBC 드라이브 연결  (0) 2009.11.24
JAVA - 개발자가 놓치기 쉬운 자바의 개념, 기본원리  (0) 2009.11.24
clone()메소드  (0) 2009.11.24
Posted by Tiwaz
JAVA2009. 11. 24. 22:24
Oracle JDBC 드라이브 연결 Java

2009/02/18 15:34

작성자: 베레(lsj403)

복사 http://blog.naver.com/lsj403/memo/77722322

첨부파일 (1)

package oracle;

import java.sql.*;
public class DriverTest {
    
    public static void main(String[] args) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.10.102:1521:orcl", "scott", "password");
            
            if(con!=null)
                System.out.println("오라클 접속에 성공했습니다.");
            else
                System.out.println("오라클 접속에 실패하였습니다.");
            
            stmt = con.createStatement();
            rs = stmt.executeQuery
                            (" select count(*) cnt from tab ");
            if(rs.next()) {
                int result = rs.getInt("cnt");
                System.out.println("테이블 총 개수 : "+result);
            }
            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } finally {
            if(con!=null)
                try {
                    rs.close();
                    stmt.close();
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
        }
        
    }//main
}//class

Posted by Tiwaz
JAVA2009. 11. 24. 22:23
본문스크랩 JAVA - 개발자가 놓치기 쉬운 자바의 개념, 기본원리 서로이웃공개 Java

2009/02/15 01:08

작성자: 베레(lsj403)

출처 카페 > kosta15 | realchoky
원문 http://cafe.naver.com/kosta15/369


 JAVA를 배우는 사람, 또는 프로그래밍에 종사하시는 개발자 분들이 다시한번 봐도 괜찮을거 같은
마치 이론 시험 직전 보는 컨닝페이퍼 같이 정리 되어있는 자바의 기본원리 에 관한 자료입니다.
특히 각 주제에 맞는 목차에는 예제가 있어 JAVA를 입문하시는 초급프로그래머 들에게는 좋은 자료 같습니다.
그럼 길고긴 스크롤 내려서 꼭 보시길..

목차

1 객체지향의 구멍 static
1.1 Java는 객체지향 언어이다?
1.2 전역변수
2 Java는 Pointer언어이다? (Java에는 Pointer밖에 없다?)
2.1 Java는 primitive형을 제외하곤 모두 Pointer이다
2.2 null은 객체인가?
2.3 String에 대하여
2.4 객체지향의 캡슐화 파괴 주의
2.5 배열에 대하여
2.5.1 배열은 object 인가?
2.5.2 배열의 length는 왜 field(member variable)인가?
2.5.3 final과 배열에 대하여...
2.5.4 "Java에서의 다차원 배열은 존재하지 않는다."
2.6 인수(parameter/argument)전달의 개념
2.6.1 "Java에서 parameter(argument) 전달은 무조건 'call by value' 이다"
2.6.2 "C와 같은 언어는 static linking이지만, Java는 dynamic linking이다."
2.7 GC 에 대하여 잠깐!
2.7.1 "Garbage Collection은 만능이 아니다."
2.8 Java Pointer 결론
2.8.1 "결국 Java에는 pointer가 있는 것인가, 없는 것인가?"
3 상속과 interface의 문제점
3.1 상속
3.1.1 상속에 있어서의 생성자(constructor)
3.1.2 "down cast는 본질적으로 매우 위험하다"
3.1.3 "추상클래스에 final이 있으면 compile error이다"
3.2 interface
3.2.1 "interface는 interface일뿐 다중 상속의 대용품이 아니다."
3.3 상속 제대로 사용하기
4 package와 access 제어에 관한 이해
4.1 package
4.1.1 "package는 '계층구조' 인가?"
4.1.2 "compiler 가 인식하는 class검색 순서(소스코드내 클래스가 발견될 경우 그 클래스의 위치를 찾는 순서)"
4.2 access 제어
4.2.1 "interfacde member의 access 제어"
4.2.2 그렇다면 interface를 다른 package에 대하여 숨기고 싶으면 어떻게 하는가?
5 기타 Java 기능
5.1 Thread
5.1.1 "Multi Thread에서는 흐름은 복수이지만 data는 공유될 수 있다."
5.1.2 "Thread는 객체와 직교하는 개념이다."
5.1.3 "Synchronized 의 이해"
5.1.4 "Thread 사용법의 정석은?"
5.2 Exception
5.2.1 "finally 절은 반드시 어떠한 경우에도 실행되는가?"
5.2.2 "예외의 종류 3가지 (Error, RuntimeException, 그밖의 Exception)"
5.2.2.1 Error
5.2.2.2 RuntimeException
5.2.2.3 그밖의 Exception
5.2.3 "OutOfMemoryError는 어떻게 처리해야 하는가?"
5.3 Object Serialize
5.3.1 "Serialize를 위해서는 marker interface인 java.io.Serializable interface를 implements해야한다."
5.3.2 "super class는 Serializable이 아닌데 sub class만 Serializable인 경우의 문제점"
5.3.3 "transient field의 복원(?)관련"
5.3.4 "Stack Overflow에 주의하라!"
5.4 "nested class / inner class / 중첩클래스"
5.4.1 "중첩클래스의 개념"
5.4.2 "내부클래스는 부모의 참조를 몰래 보유하고 있다."
5.4.3 "local inner class에 대하여"
5.4.4 "anonymous class(무명클래스)에 대하여"
6 이래도 Java가 간단한가?
6.1 method overload 에서의 혼란?
6.1.1 "overload란 이름이 가고 인수가 다른 method에 compiler가 다른 이름을 붙이는 기능"
6.1.2 "그렇다면 overload에서 실제로 혼동되는 부분은 무엇인가?"
6.1.3 (참고) 또다른 혼동, overload한 method를 override 하면?
6.2 상속/override/은폐 에서의 복잡함
6.2.1 "Java class의 member 4 종류"
6.2.2 "override시 method 이름에 대한 함정"
6.2.3 "또다른 나의(?) 실수 - 말도 안되는 오타"
6.2.4 "static member를 instance를 경유하여 참조해서는 안 된다."
6.2.5 "super keyword는 부모의 this"
6.3 상속에 관한 또 다른 문제
6.4 그밖의 함정
6.4.1 "생성자에 void 를 붙인다면?"
6.4.2 "if / switch 의 함정"
7 Java 기능 적용 몇가지
7.1 대규모 개발에서 interface 분리하기
7.1.1 "interface 분리의 필요성"
7.2 Java에서의 열거형
7.3 Debug write
8 Java 5.0 Tiger 에 대하여
8.1 Working with java.util.Arrays
8.2 Using java.util.Queue interface
8.3 java.lang.StringBuilder 사용하기
8.4 Using Type-Safe Lists
8.5 Writing Generic Types
8.6 새로운 static final enum
8.7 Using java.util.EnumMap
8.8 Using java.util.EnumSet
8.9 Convert Primitives to Wrapper Types
8.10 Method Overload resolution in AutoBoxing
8.11 가변적인 argument 개수 ...
8.12 The Three Standard Annotation
8.13 Creating Custom Annotation Types
9 The for/in Statement
9.1 for/in 의 자주 사용되는 형태
10 Static Import
10.1 static member/method import
11 References




===============================================================

1 객체지향의 구멍 static #

1.1 Java는 객체지향 언어이다? #

"Java는 완전한 객체지향 언어이다" 라는 주장을 자주 접하게 된다. 만일 이것이 사실이라면 Java를 사용하는 한 "기존의 절차지향 프로그래밍을 전혀 할수 없을것 같지만 그렇지 않다. 빠져나갈 구멍이 있는 것이다. static을 이용하면 비 객체지향 언어처럼 코딩할 수 있다.

static method는 instance가 아닌 클래스에 속하는 method로, class method라고 부른다. 반대로 static이 아닌 method는 instance method라고 부른다.

static method는 this가 없다. instance method에는 숨겨진 파라미터로 this가 건네진다. (아래 "객체지향에 흔희 있는 오해" 참고) 하지만 static method는 절차지향의 함수와 동일하므로 숨겨진 파라미터 this는 없다. 그래서 static method에서는 전달한 this가 없으므로 instance method를 호출하거나 instance field를 참조할 수 없는 것이다.

(참고) 객체지향에 흔히 있는 오해

  • 오해1. "객체지향에서는 객체끼리 서로 메세지를 주고 받으며 동작한다." 라는 말을 듣고 다음과 같이 생각할 수 있다. "객체지향에서는 객체가 각각 독립하여 움직인다는 것인가, 그러면 각 객체에 독립된 thread가 할당되어 있단 말인가?" 그렇지 않다. "메세지를 보낸다"라는 것은 단순히 각 객체의 함수 호출에 불과하다.

  • 오해2. "객체지향에서는 method가 class에 부속되어 있다"는 말을 듣고 다음과 같이 생각할 수 있다. "그러면 instance별로 method의 실행코드가 복제되고 있는 것이 아닌가?" 물론 이것도 오해다. method의 실행코드는 종래의 함수와 동일한 어딘가 다른곳(JVM의 class area)에 존재하며 그 첫번째 파라미터로 객체의 포인터 this가 건네질 뿐이다.

  • 오해3. "그렇다면 각 instance가 method의 실행코드를 통째로 갖고 있지 않는 것은 확실하지만, method의 실행 코드의 포인터는 각 instance별로 보관하고 있는것이 아닌가?" 이것은 약가 애매한 오해이긴 하다. JVM 스펙에서는 class영역에 실행코드를 갖고 있으며, method 호출시 별도의 stack frame이 생성되어 실행되고 실행 완료시 복귀 주소를 전달한다.

1.2 전역변수 #

static에서 public field는 전역변수(global variable, 글로벌 변수)이다. 여기서 "글로벌 변수는 왜 안 되는가"에 대해 잠깐 생각해 본다. 우리는 흔히 "글로벌 변수는 될수있는한 사용하지 않는 것이 좋다"라고 한다. 그 이유는 글로벌 변수는 어디서든 참조할 수 있고 값을 변경할 수 있기 때문이다.

또한 파라미터나 리턴값으로 교환해야 할 정보를 글로별 변수를 경유(사용)하여 건네주면 함수의 역할이 불분명 해지고 흐름도 애매해 진다. 마지막 이유로는 "글로벌 변수는 하나밖에 없다"는 것이다. 이는 어디서 이값을 변경했는지 알 수 없게 하는 지름길이고 실무에서도 간혹 발생하긴 하지만, 이 하나밖에 없는 변수가 버전업으로 두개가 필요하게 되었을때 확장도 대형 프로젝트에서는 힘들어진다.

따라서 static에서 public은 final을 붙여 상수로 사용해야지 그 외의 용도는 자제하는 것이 좋을 것이다.

  • (참고) final 초기화에서의 주의점. 예를 들어 다음과 같은 코드를 보았을때 우려되는 점은 무엇인가?

<PRE class=wiki>public final static Color WHITE = new Color(255, 255, 255);</PRE>

위의 코드는 java.awt.Color에서 발췌한 것인데, final 변수는 한번 초기화 되면 변경이 불가능한데 object로 초기화 할 경우 WHITE라는 필드가 변경될 수 없는 것이지 그것이 가리키는 객체는 아니라는 점이다.

과거 신규 서비스 개발시 final 변수 필드에 설정파일을 읽어 cache하는 singleton class의 특정 member 를 이용하여 초기화 할 경우 이 멤버값이 변경되면 final 변수의 값이 변경되었는데 프로그램에서는 이상한 짓을 하는 원인을 찾기가 상당히 어려웠던 경험을 하고 난 후 부터 이런 코드는 냄새나는 코드로 여겨지게 되었다.

static은 글로벌변수와 동일하므로 남발해서는 안된다. static을 사용할 경우 다음 두가지는 최소한 기억한다.

  1. static field는 final의 경우와 달리 정말 "하나여도 되는지" 여부를 잘 생각해야 한다.
  2. static method는 주저하지 말고 쓰되 다음 두가지의 경우 매우 활용적이다.
    1. 다른 많은 클래스에서 사용하는 Utility Method 군을 만드는 경우. (주로 Utility Class의 method)
    2. 클래스 안에서만 사용하는 "하청 메소드(private method)". 이유를 예를 들어 설명하면, 아래와 같은 조금은 과장된 클래스가 있다고 하자.

<PRE class=wiki> public class T .. private int a; private int b; private int c; private int calc(){ c = a + b; return c * c; } ....other method or getter/setter...</PRE>

위의 클래스 T의 경우 내부에서 calc라는 instance 함수를 사용하게 되면 c 의 값이 매번 변하게 된다. 이는 무심코 하는 실수로 클래스내에서 private method는 모든 멤버 instance 변수에 접근 가능하게 되면서 발생한다. c의 값이 변하지 않기를 바랄수 있다. 이때 안전한 방법은 다음과 같이 calc 하청 메소드를 static method로 수정하면 안전하다.

<PRE class=wiki> private static int calc(int a, int b){ int c = a + b; return c * c; }</PRE>

여기서 a와 b는 멤버 변수를 접근할수 없어 전달해야한다.(static에는 this가 없어 instance field를 참조할 수 없다는 것은 이미 위에서 설명했다.) 또한 c도 같은 이유로 사용할 수 없어 로컬 변수로 선언하고 사용하고 있다. 이럴 경우 메소드가 약간 커질수 있지만 instance member 변수를 안전하게 사용할 수 있다는 장점이 있다. 이것은 static을 다시한번 생각하게 하는 좋은 예가 되었을 것이다.

2 Java는 Pointer언어이다? (Java에는 Pointer밖에 없다?) #

2.1 Java는 primitive형을 제외하곤 모두 Pointer이다 #

"Java에는 포인터가 없다" 라고 Java의 장점 이라고 생각하는 것은 입문자도 외우고 있다. 하지만 이 부분은 의외로 Java를 혼란스럽게 하는 주범이라고 생각한다. Java에 포인터가 없기는 커녕 primitive(int, short, char, long...등 JVM의 Heap에 object로 생성되지 않는것들)를 제외하면 "포인터 밖에 없는 언어이다"라는 명제가 성립되게 된다. 사실 여기서 포인터라고 함은 C 의 그것과는 조금 다른 reference(참조)이긴 하지만...

"즉, 자바의 클래스형의 변수는 모두 포인터이다."

2.2 null은 객체인가? #

Java에서 공참조(힙에 실제로 참조되는 object가 없는 참조)의 경우는 당연히 객체가 붙어 있지 않다. 그러나, Java API 레퍼런스의 NullPointerException 항에는 다음과 같이 기술되어 있다.

"object가 필요한 경우 application이 null을 사용하려고 하면 throw된다. 가령 다음과 같은 경우이다."
  • null object의 instance method 호출
  • null object의 field(member variables)에 대한 액세스 또는 그 값의 변경
  • null의 길이를 배열처럼 취득할 경우
  • null의 slot을 배열처럼 액세스 또는 수정
  • null을 Throwable처럼 throw 할 경우

위에서 null object라는 말이 등장하는데 이는 공참조에 객체가 붙어 있지 않은 것이 아니라 null을 가리키는 객체라고 볼수 있다. 즉, 공참조라는 것은 JVM에서 봤을때 아무것도 참조하지 않는것이 아니라 null이라고 하는 object를 참조하고 있는것이다. 그러나 JSL 4.3.1에서는 다음과 같이 나와있다.

"참조값(reference)은 이러한 객체의 포인터나 어떤 객체도 참조하지 않는 특수한 null참조가 된다"

즉, 공참조는 어떤 객체도 참조하지 않는다고 단정하고 있다. 하지만 '==' 연산에 있어 두개의 객체가 모두 null이거나 동일한 객체 또는 배열 참조의 경우 true라고 되어있는것으로 봐서 서로 다른 두 객체가 동일한 null을 참조하고 있으므로 true가 된것이 아닌가 하는 생각을 할 수 있다.

즉, null이 Object의 instance 형태는 아니지만 개념적으로 봤을때 null도 object라고 봐야 하지 않을까?

2.3 String에 대하여 #

String Object에 대한 생각.

<PRE class=wiki> String str = "111222"; String a = "111"; String b = "222"; String c = "111"; String d = b; String t = str.substring(0,3); //111</PRE>

위의 소스를 보고 다음이 참인지 생각해 보자. (==연산자는 포인터의 비교이지 값의 비교가 아님)

  1. str == (a + b) ==> 이것은 두개의 참조와 하나의 참조를 비교했으므로 당연히 false이다.
  2. a == b ==> 이것은 당연히 false
  3. d == b ==> 이것은 동일한 reference이므로 true
  4. a == t ==> a 와 t 는 둘다 값이 "111"이다. 하지만 이것은 서로 다른 참조를 가져 false이다. 그렇다면 다음 5번도 false일까?
  5. a == c ==> 이것은 true이다. 아.. 4번과 혼란스럽다. 이것이 참인 이유는? ==> 이것의 해답은 JSR 3.10.5에 다음과 같이 나와 있기 때문이다.

"동일한 내용의 문자열 리터럴에 관해서는 인스턴스를 하나밖에 생성하지 않는다."

즉, 위의 a와 c 는 '=' 를 이용하여 문자열 리터럴을 생성하게 되는데 a 에서 이미 만들어 놓았으므로 c에서는 그것을 참조한다.

2.4 객체지향의 캡슐화 파괴 주의 #

"object pointer를 반환하는 getter method는 객체지향의 캡슐화가 파괴될 가능성이 있다." 이는 object형의 field(member variable)의 getter에서 이 object를 그냥 반환하면 이 object를 받은쪽이나 참조하고 있는 다른쪽에서 이 object의 내용을 변경하게 되므로 사실 캡슐화(은닉)는 이루어 지지 않았다고 봐야한다.

"이럴 경우 object를 clone(복제) 하여 반환하지 않아도 되는지를 반드시 생각해 본다."

object의 복사에는 shallow copy와 deep copy가 있다.

<PRE class=wiki> //(참고)Member에는 두개의 field(Identity Class 형의 ID와 Family Class 형의 family)가 있다. /** shallow copy */ Member shallowCopy(){ Member newer = new Member(); newer.id = this.id; newer.family = this.family; return newer; } /** deep copy */ Member deepCopy(){ Member newer = new Member(); newer.id = new Idetity(this.id.getId(), this.id.getName()); newer.family = new Family(this.family.getFamilyName(), this.family.getFamilyInfo()); return newer; } </PRE>

위 소스에서 보듯이 shallowCopy 는 object를 복사하여 반환한것 처럼 보이지만, 사실은 Member object만 새로 생성되었을뿐 Member의 field는 newer와 this 둘다 서로같은 힙의 id와 family를 참조한다. 하지만 두번째 method인 deepCopy의 경우 Member의 field를 새로 생성하여 복사하므로 서로 다른 id와 family이다.

"Java에서는 clone이라는 method가 준비되어 사용되는데 이는 기본이 shallow copy임을 명심해야 한다. deep copy를 사용하기 위해서는 clone method를 overload하거나 따로 만들어 직접 기술해야 한다."
(참고) object를 immutable(변하지 않는, 불변의 객체)로 만드는 요령
  1. 모든 field(member variable)를 생성자(constructor)를 이용하여 초기화 한다.
  2. 모든 field는 private으로 선언하고, getter method는 만들되 setter는 기술하지 않는다.

즉, 값을 변경하기 위해서는 object를 다시 만들어야만 하는 불편은 있지만 안전하게 사용하려 할때 유용하다.

2.5 배열에 대하여 #

2.5.1 배열은 object 인가? #

JVM에서 배열은 object로 취급되어 object와 같이 aload, astore와 같이 bytecode로 기술되어 진다. int[] iarr = new int10; 에서 보는것과 같이 new로 Heap 영역에 object를 생성하므로 object임을 알 수 있다.

2.5.2 배열의 length는 왜 field(member variable)인가? #

String의 길이를 구할때는 length()와 같이 method를 이용하는데 배열은 object임에도 불구하고 legth와 같이 필드로 되어있다. '이래도 Java가 완전한 객체지향 언어인가' 라는 의심이 들게 한다. 그렇다면 length가 public이므로 array.length = 100; 과 같이 하면 배열 크기가 변경되나?

이것은 컴파일 오류가 난다. length는 final이라 값을 변경 할 수 없다는 것이다. 그렇다면 final field로 한 이유는 무엇이냐는 Java News Group에 찾아보면 대부분이 "효율을 위해서"라고 되어 있다. JIT compiler를 사용하지 않는한은 method보다는 field가 빠른건 당연한 것이다.

그런데 정말 알수 없는것은 byte code에서는 arraylength라는 전용명령으로 컴파일 된다. 즉, length는 Java의 문법이 어찌되었든 JVM레벨에서는 field가 아닌것이 분명하다. 그렇다면 효율을 위해서 field로 했다는 것은 도데체 무슨 소리인가?

전문가들의 대답에는 이것은 Java의 수수께끼 중 하나라고 대답하는 사람이 많다고 한다.^^;

2.5.3 final과 배열에 대하여... #

우리가 흔희 앞에서도 나온바 있지만 final은 값을 변경할 수 없는 것이라고만 생각하지 object로 되어 있을 경우 그 object는 변경 가능하다는 것을 잊곤한다. 배열도 object이므로 마찬가지다.

final int[] iarr = new int[5]; 일경우 iarr = null; 은 에러가 나지만 iarr[3] = 5; 는 에러가 나지 않는다. 즉, final이 지정되어 있는것은 iarr이지 iarr이 가리키는 곳 배열의 요소가 아닌 것이다.

2.5.4 "Java에서의 다차원 배열은 존재하지 않는다." #

가령 2차원 배열 처럼 보이는 int[][] iarr 또는 int[] iarr[] 은 일차원 배열 두개이지 2차원 행열 구조가 아닌것이다. 즉, 두개의 배열은 각각이 배열로 되어 있는 것이지 테이블(행열)형태가 아니다.

2.6 인수(parameter/argument)전달의 개념 #

2.6.1 "Java에서 parameter(argument) 전달은 무조건 'call by value' 이다" #

primitive type의 경우 호출한 쪽의 변수값은 호출 받은 method내에서 값이 변경되어도 변경되지 않는다. reference type의 경우도 reference되는 object에 대해서는 함께 변경되지만 reference pointer는 call by value이다. object를 가리키는 pointer는 call by value로 변경되지만 Heap의 실제 object내용은 변경되지 않는다.

2.6.2 "C와 같은 언어는 static linking이지만, Java는 dynamic linking이다." #

따라서 Java는 Class 파일이 처음에 한꺼번에 memory에 읽혀지는 것이 아니라 런타임시에 그것이 필요해 졌을때 읽혀지고 링킹된다. static field의 영역도 Class가 읽혀지는 시점에 비로서 확보된다. 이렇게 되면 최초 가동시간이 단축되고 끝까지 사용하지 않는 Class의 경우 신경쓸 필요가 없어지게 된다.

따라서 static field는 프로그램이 시작되어 해당 Class가 필요해 졌을때 JVM이 알아서 load/link 해 준다. 즉, static field는 프로그램이 실행되기 시작할 때부터 끝날때까지 계속해서 존재하는 것이라고 보면 된다.
(참고) 링킹(linking)의 의미

link된다는 것은 Class가 memory에 loading될 때 특정 메모리 번지에 loading되는데 이 메모리 번지는 loading될때 마다 다른 번지수에 loading된다. 이때의 메모리 주소값(Java에서는 실제 메모리 값이 아닐 수 있다)을 현재 실행중인 프로그램에서 알 수 있도록 하여 해당 Class에 대한 참조가 가능하도록 연결하는 과정이다.

정적(static) link라는 것은 이러한 메모리에 대한 주소 정보를 컴파일 시에 compiler가 미리 결정하는 것이고, 동적(dynamic) link라는 것은 프로그램 수행 중 결정되는 것을 의미한다. 정적인 link의 경우 직접적으로 메모리의 번지값이 할당 되는 것이 아니라 offset값(기준위치로 부터의 index값)으로 연결시킨다.

2.7 GC 에 대하여 잠깐! #

2.7.1 "Garbage Collection은 만능이 아니다." #

Java에는 free가 없다. GC가 알아서 해준다. 하지만 GC 수행중에는 프로그램이 멈추는 것과 동일한 증상이 나타나기 때문에 GC가 자주 발생하지 않도록 프로그램 해야 한다. 서비스 되고 있는 시스템에서도 가끔 시스템이 응답이 늦어지는 시점이 있는데, 이는 GC가 수행되고 있는 중이 대부분이다.

그렇다면 GC가 자주 발생하지 않도록 해야 하는데 가장좋은 방법은 무엇일까? 그것은 바로 불필요한 객체를 생성하지 않는 것이 아닐까?

개인적으로 Java에 free가 없는것이 너무나 든든하게 느껴진다. 이유는 두개의 변수가 Heap내의 하나의 object를 reference하고 있을 경우 실수로 하나의 변수만 free해 버리면 나머지 하나는 dangling pointer라하여 reference pointer가 모르는 사이데 사라져 버려 곤경에 처하는 것을 예방해 주기 때문이다.

참고로 Object class에는 finalizer라는 method가 있어 GC 수행시점에 호출되는 method가 있지만 이것은 GC가 언제 수행될지 알 수 없으므로 과신하지 말아야 할 것이다.

2.8 Java Pointer 결론 #

2.8.1 "결국 Java에는 pointer가 있는 것인가, 없는 것인가?" #

Java는 Heap내의 Object를 참조(reference)하고 있고, 참조는 결국 개념이 포인터와 같은 것이므로, "Java에는 pointer가 없다"는 것은 어불성설이다.
// 이부분에 대해 Object를 이해하시면 족히 이런 문제는 사라질것으로 봅니다.
// 클래스에 대한 인스턴스(object)들은 reference로 밖에 가질(참조될)수 없기 때문입니다.
// 컴파일러 입장이 아닌 언어 자체의 사상을 가지고 쉽게 이해시키는 것이 좋을것 같습니다.

JSR 4.3.1을 보면 다음과 같은 말이 나온다.

"참조값(reference)은 객체의 pointer이거나, 또는 어떠한 객체도 참조하지 않는 특수한 null 참조가 된다"

또한 java.sun.com의 Java programmer's FAQ에 "Java는 pointer가 없다고 하는데, linked list는 어떻게 만들어야 하나?"라는 질문에 다음과 같이 답변이 나와있다.

(답변) Java에 관한 많은 오해중에서 이것이 가장 심각한 것이다. 포인터가 없기는 커녕 Java에 있어 객체지향 프로그래밍은 오로지 pointer에 의해 행해진다. 다시 말새 객체는 항상 포인터를 경유해서만 access되며 결코 직접적으로 access되지 않는다. pointer는 reference(참조)라고 불리며 당신을 위해 자동으로 참조된다.

"Java에는 pointer가 없고 주장하는 모든 서적과 글들은 Java의 reference사양에 모순된다고 봐야 할 것이다."

3 상속과 interface의 문제점 #

3.1 상속 #

3.1.1 상속에 있어서의 생성자(constructor) #

"child의 default 생성자가 있으면 그 생성자에는 parent의 생성자(super()) 호출이 compile시 자동 삽입된다." 따라서 super() 이전에 다른 코드가 있으면 object가 생성되기 이전이므로 오류를 발생하게 된다.

3.1.2 "down cast는 본질적으로 매우 위험하다" #

down cast - child의 type으로 parent를 casting - 는 parent 형태의 type이 정말 child type인지 compile시에는 알 수 없다. 실행시에 type check가 이루어 지므로 runtime시에 ClassCastException이 발생할 가능성이 커진다.

"프로그래밍시 오류는 가능한한 compile시에 처리하는것이 좋다."

3.1.3 "추상클래스에 final이 있으면 compile error이다" #

abstract method가 있는 클래스는 추상 클래스이고 추상클래스는 상속되지 않으면 아무런 의미가 없기 때문이다.

3.2 interface #

3.2.1 "interface는 interface일뿐 다중 상속의 대용품이 아니다." #

interface를 method signature - 추상클래스와 같이 구현부는 없이 선언부만 있는 method - 의 용도로 생각하는것이 Java에서는 옳다. 즉, interface는 final field와 abstract method가 있는 클래스와 동일하긴 하지만 상속의 의미와는 그 용도가 다르다. 공통된 type을 정의하는것으로 보는것이 맞는 의미일 것이다.

또한 interface는 클래스를 재이용하기 위해 상속을 사용하여 캡슐화의 파괴를 수반하는 것을 방지하는 기능이있다. 상속을 사용하면 모두 구현후 마치 소스 코드가 여기저기 천 조각을 주워 모아 만든 '누더기'같이 보이는 것에 한숨을 쉰 경험이 있을 것이다. 이 부분을 interface로 구현하면 보다 깔끔한 코드가 나오게 된다. 물론 public과 protected를 적절히 잘 사용해도 되긴 하지만 말이다.

하지만 상속은 메소드 오버라이드한 경우 클래스를 마음대로 개조해 버린 셈이 되므로 어디선가 묘한 모순이 발생하게 될 가능성도 높아질뿐 아니라 추상클래스의 경우 실제 구현부가 어디에 위치하는지도 에매하게 느껴질 수 있어 불안한 코드가 되고 만다.

3.3 상속 제대로 사용하기 #

"그렇다면 제대로 된 상속은 어떻게 판단할 수 있을까?"

상속은 'is a'관계가 성립해야 올바르다. 즉 '서브클래스(자식) is a 슈퍼클래스(부모)'가 성립해야 한다. 예를 들면 Red is a Color는 올바른 명제이지만 Engine is a Car는 'has a'관계이므로 상속이라고 볼 수 없다.
"따라서 'has a'관계는 상속이 아니므로 composition과 delegation을 이용하면 된다."

composition은 '객체를 field가 갖게 하는 방법'을 의하므로 'has a'관계가 정확히 성립한다.
"상속 대신 composition과 delegation(조작이나 처리를 다른 객체에 위임)을 사용하면 다음과 같은 이점이 있다."

  1. 상속에서는 슈퍼클래스가 허용하고 있는 조작을 서브클래스에서 모두 허용해야 하지만, composition과 delegation에서는 조작을 제한할 수 있다.
  2. 클래스는 결코 변경할 수 없지만, composition하고 있는 객체는 자유롭게 변경할 수 있다. 예를 들면 학생 클래스가 영원이 학생이 아니라 나중에 취직을 하여 직장인 클래스가 될수 있다.
상속을 composition과 delegation으로 변경하는 요령은? 여기서 Shape를 상속한 Polyline과 Circle을 변경한다면 다음과 같다.
  1. Shape(부모)의 공통된 내용을 구현한 구현 클래스(ShapeImpl)를 만든다.
  2. Polyline과 Circle 클래스에서 ShapeImpl을 composition하고 부모와 공통되지 않는 method를 각각 위임 받는다.
  3. ShapeImpl 클래스의 method를 추출한 ShapeIF interface를 작성하고 Polyline과 Circle에서는 implements 한다.

4 package와 access 제어에 관한 이해 #

4.1 package #

4.1.1 "package는 '계층구조' 인가?" #

처음 Java를 접하면서 package에 대해 이해할때 마치 파일시스템과 같은 계층구조라고 이해하게 되어 ' import /test/*.class '는 왜 안되는지 의아해 했던 기억이 있다. 그리고 부모 directory에 있는 클래스에서 왜 자식 directory에 있는 Class를 import없이 사용할 수 없는지도 이상했다.

즉, package에서 동일 부모라도 서로 다른 package는 완전히 별개의 package였던 것이다. 이 부분에 관해서는 JLS 7.1 에서 다음과 같이 기술되어 있다고 한다.

"package가 계층적인 이름 구조로 되어 있는 것은 관련된 package를 일정 규약에 따라 체계화하기 위해서이고, package 안에서 선언되어 있는 top-level형과 동일한 이름을 가진 서브 package를 갖는 것이 금지되어 있는 점을 제외하면 특별한 의미는 없다."

즉, Java에서는 package이름을 계층적으로 명명할 수 있을뿐 package구조 자체에는 어떤 계층적인 의미 부여도 할 수 없는 것이다. 다시 말해서 Java에서는 package이릉을 계층적으로 명명할 수 있을 뿐 구조자체는 평평한 것이다. 실제로 바이트 코드의 내용을 보면 깨어진 내용중에 java/lang/String과 같이 완전한 한정이름을 class이름으로 사용됨을 알 수 있다.

4.1.2 "compiler 가 인식하는 class검색 순서(소스코드내 클래스가 발견될 경우 그 클래스의 위치를 찾는 순서)" #

  1. 그 class자신
  2. 단일형식으로 임포트된 class
  3. 동일한 패키지에 존재하는 다른 class
  4. 온디멘드 형식(..* 형태) 임포트 선언된 class

4.2 access 제어 #

public은 다른 package에서 참조 가능하고, 무지정할 경우 동일한 package내에서만 참조 가능하다.

4.2.1 "interfacde member의 access 제어" #

interface의 member field/method는 모두 public이다. interface member에 protected나 private을 지정할 수는 없다. 또한 public을 지정할 필요도 없다. 지정해도 상관없지만 JLS 9.4에서는 다음과 같이 명시되어 있다.

"interface의 method에 대하여 public 수식자를 지정하는 것이 허용되고 있지만, style로서는 전혀 권장할 수 없다."

즉, interface member는 모두 public이라 되어 있는 것이다. 또한 James Gosling도 집필에 참가한 '프로그래밍 언어 Java 3판'에서는 다음과 같이 기술되어 있다고 한다.

"public이 아닌 member를 interface에게 갖게 하는 것은 아무런 의미가 없다. interface의 member에 대한 access제어에 interface 자신의 access 제한을 적용하는 것이므로 이것은 아무런 의미가 없다."

4.2.2 그렇다면 interface를 다른 package에 대하여 숨기고 싶으면 어떻게 하는가? #

그것은 interface 자체 선언에 public이 아닌 것을 적용하면 되는 것이다. member별로 제어할 수 없어 불편한 면도 있지만, 나름대로 그럴 듯한 규칙이다. 하지만 이것은 정말 이상한 논리가 아닐수 없다. public이 아닌 interface에 public method가 무슨 의미가 있는지 알 수 없기 때문이다. 이 interface를 구현한 클래스에서도 method는 모두 public이 되어야 하는데, 이것도 아무래도 이상하다.

5 기타 Java 기능 #

5.1 Thread #

5.1.1 "Multi Thread에서는 흐름은 복수이지만 data는 공유될 수 있다." #

Multi processing에서는 흐름은 복수이지만 data는 독립되어 있다. 하지만 Multi Thread에서는 Heap과 static영역에 관한 한 2개 이상의 Thread 사이에 공유가 이루어 진다. 따라서 2개 이상의 Thread에서는 동일한 static field를 참조할 수 있고, 동일한 객체에 접근할 수도 있다. 그러나 stack만은 Thread별로 독립되어 있다. stack은 method에 들어가는 시점에 확보되고 빠져 나오는 시점에 확보되고 빠져 나오는 시점에 Free 되므로 2개 이상의 Thread에서 공유할 수는 없는 것이다.

5.1.2 "Thread는 객체와 직교하는 개념이다." #

Multi Thread는 어디까지나 Thread라는 처리 흐름이 여러 개 존재할 수 있다는 의미이다. 요약하면 다음 3가지 이다.
  1. Multi Thread에서는 Thread라는 처리 흐름이 2개 이상 존재할 수 있다.
  2. 어떤 Thread에서 움직이기 시작한 method가 다른 method를 호출 했을때 호출된 측의 method는 호출한 측의 method와 동일한 Thread에서 동작한다.
  3. Thread의 경계와 객체의 경계는 전혀 관계가 없다. 즉, Thread와 객체는 직교하고 있다.

5.1.3 "Synchronized 의 이해" #

Multi Thread 기반의 programming시에 synchronized를 놓쳐 자주는 일어나지 않으나 뭔가 잘못되어 가는것을 경험한 적이 있다. 즉, 이것이 원인이 되어 버그가 발생한 경우 그 버그는 재현성이 지극히 낮아지기 때문에 꽤 고생을 하게 된다. 이런 사태가 발생하게 되면 버그의 원인을 찾기가 어렵게 되고 해당 application은 언제 발생할지도 모르는 오류가 있는 상태 그대로 운영되기 때문에 심각성이 내포되어 있다고 할 수 있다.

이러한 사태를 방지하기 위해서는 critical section을 2개 이상의 Thread가 동시에 실행되지 않도록 '배타 제어'를 해야한다. 그 키워드가 바로 synchronized이다.

synchronized에는 synchronized(obj){} 형태와 method에 synchronized 를 붙이는 두가지 방법이 있는데, 이 둘은 범위만 같다면 같은 의미이다. 예를 들어 설명하면, 아래의 소스에서 method1()과 method2()는 동일하다.

<PRE class=wiki> synchronized void method1(){ ... } void method2(){ synchronized(this){ ... } }</PRE>

이렇게 동일한 의미를 두가지로 만든것은 method단위로 synchronized를 걸 일이 그만큼 많다는 것을 의미한다. 많이들 오해하고 있는 부분이 위의 소스에서 알수 있듯이 method에 synchronized를 사용한다는 것은 '그 객체에 해한 조작은 동시에 하나의 Thread라는 것이지 method 호출이 하나의 Thread가 아닌것이다'

그렇다면, Thread A가 obj의 Lock을 설정하고 있는 상태에서 다시 한번 Thread A 자신이 obj의 Lock을 설정하면 어떻게 될까? 이 경우 Thread A는 이미 이 obj에 대하여 Lock을 보유하고 있으므로 기다리지는 않아도 된다. 위의 소스에서 method1에서 method2를 호출한다면?

method1에서 이미 obj의 Lock을 보유 했으므로 method2의 synchronized(this) 부분에서는 Lock을 기다리지 않아도 된다.

즉, Lock의 기준이 특정Thread에 있어서 Lock의 기준이 method가 아닌 object인 것이다. 이 규칙 덕분에 synchronized method도 재귀호출이 가능해지고, synchronized method가 동일한 instance의 synchronized method를 호출할 수 있는 것이다.

주의할 점은 static method에 synchronized가 있다면 static은 this참조가 없다고 위에서 설명하였으므로, 이 클래스의 Class 객체를 Lock하게 된다. 기준이 xx.Class가 되는 것이다.

5.1.4 "Thread 사용법의 정석은?" #

Thread 사용법에는 다음 두가지의 정석이 있다.
  1. Runnable을 implements하고 Thread의 참조를 보유(composition) 하는 방법. 이경우는 단지 Runnable만 implement함으로서 해결되는 경우가 대부분이긴 하지만, 그 class 내에서 해당 class의 Thread를 조작하게 된다면 composition한 Thread 객체에 delegation하면 된기 때문이다.
  2. Thread class를 상속하는 방법. JDK의 소스를 보면 Thread class에는 Runnable을 implements 하고 있다. 그리고 run method는 native method이다. 따라서 Thread를 상속한 모든 클래스는 사실 Runnable을 implements하고 있는 것이다. run method는 abstract가 아니므로 구현되어 있고 우리는 이를 오버라이드하여 사용하고 있다. 이 방식을 사용하면 Thread의 method를 안팍으로 자유롭게 호출할 수 이지만, 이미 다른 class를 상속하고 있다면 이 방식을 사용할 수는 없다.
JDK API Reference의 Runnable에 과한 설명중에 다음과 같은 내용이 있다.

"Thread class의 method중 run method만을 오버라이드하여 사용하는 경우는 Runnable interface만 implements하여 사용하면 된다. 왜냐하면, class의 기본적인 동작을 수정 또는 확장하지 않는한 그 class를 sub class화 하는 것은 바람직하지 않기 때문이다."

그렇다면 위에서 언제나 1)번 방식을 사용하면 되는 것 아닌가 라는 의문이 생기게 된다. 왜 귀찮게 2)의 방법을 고민하는 것인가, 극단적이긴 하지만 만일에 사태에 이 클래스가 다른 클래스를 상속받게 되는 경우도 있을수 있는데.

하지만 이것은 아닐것이다. 만약 이렇다면 Thread class가 Runnable을 implements할 필요가 없었을 것이기 때문이다. 또한 Thread는 생성자의 인수로 Runnable의 reference를 취득한 후 계속해서 그것을 보유한다는 것도 이상하다. Thread에 있어 Runnable이 필요한 것은 start() 때 뿐이므로 start()의 인수로 Runnable을 건네줘도 좋을 것이다.

그럼에도 불구하고 굳이 Thread에서 계속적으로 Runnable을 보유하고 있는 것은 Runnable객체와 Thread를 강하게 결합시키려는 의도 때문이다. 이것은 의도적으로 위의 2)의 방법을 권장하는 듯한 느낌을 받게 하는듯 하다.

그렇다면 API Reference의 말은 단지 상속을 피하라는 의미만 있는 것인가? 마지막으로 한가지 추정이 되는 부분은 Thread에는 suspend()나 stop()등과 같은 method가 현재 모두 deprecate되었다. 또한 sleep()이나 yield()는 모두 static method이므로 굳이 Thread 객체를 보유할 필요가 없다.

그렇다면 위의 1)의 방법에서 Thread객체를 composition할 필요가 없어진다.

"그렇다면 Thread를 아무도 보유하지 않고 Runnable만 implements한 방식이 최선인가?"

무엇이 정답인지 도무지 알길이 없다. ^^;

5.2 Exception #

5.2.1 "finally 절은 반드시 어떠한 경우에도 실행되는가?" #

try ~ catch 문의 finally 절은 'loop라면 break, method라면 return 절'을 만나도 뒤에 있는 finally절은 수행된다. 하지만 다음의 경우는 그렇지 않다.

<PRE class=wiki> try{ ... System.exit(1); }catch(...){ }finally{ ... //이 부분은 실행되지 않는다. }</PRE>

5.2.2 "예외의 종류 3가지 (Error, RuntimeException, 그밖의 Exception)" #

5.2.2.1 Error #
이에 관해선 JLS 11.2.1에 다음과 같이 기술되어 있다. "체크되지 않는 예외 클래스(Error와 그 Sub class)는 프로그램안의 다양한 위치에서 발생할 가능성이 있으며, 회복이 불가능하기 때문에 컴파일시 체크되지 않는 것이다. 이러한 예외를 프로그램에서 선언한다고 해도 난잡하고 무의미한 것이 될 뿐이다."

Java의 클래스 librury에서 Error의 sub class를 살펴봐도 AWTError, LinkageError, ThreadDeath, VirtualMachineError 등 'catch해도 소용 없을 것' 들 뿐이다. (OutOfMemoryError는 VirtualMachineError 아래에 위치한다.)
5.2.2.2 RuntimeException #
위의 Error 이외의 Exception들은 application에서 catch할 가능성이 있는 예외들이다.(버그가 없으면 발생하지 않는 예외들) 그리고 RuntimeException은 '어디서든 발생할 가능성이 있는 예외'이다. RuntimeException의 sub class로는 NullPointerException, ArrayIndexOutOfBoundException, ClassCastException 등을 들 수 있다. '이러한 예외는 버그가 없는 한 발생하지 않으므로 일일이 throws 를 작성하지 않아도 된다.'

프로그램에 버그가 없는 한 발생할 수 없는 예외가 발생한 경우 C 언어와 같이 영역 파괴가 일어나기 쉬운 언어라면 프로그램 전체를 종료시키는 것이 정답이겠지만, Java와 같이 영역파괴가 일어나지 않도록 실행시 체크(JVM Classloader의 formal verification process)를 하고 동적으로 프로그램을 load하는 언어에서는 국소적인 NullPointerException 때문에 프로그램 전체를 중지시켜서는 안 될 것이다.

따라서, RuntimeException은 catch하지 않는 것이 바람직하다고 볼 수 있다. 버그가 있는 프로그램은 신속히 종료시키는 것이 대부분의 경우 최선의 방책이라 생각하기 때문이다.
5.2.2.3 그밖의 Exception #
위의 RuntimeException이외의 Exception의 sub class는 사용자의 잘못된 조작 등으로 인해 프로그램에 버그가 없어도 발생할 가능성이 있고 그에 대하여 프로그램이 확실히 대응해야 하는 경우에 사용된다. 예를 들면 FileNotFoundException등이다.

그런데 개발하다 보면 이상하고 의아한 것이 하나 있다. 숫자 부분에 문자를 넣었을때 발생하는 NumberFormatException이다. 이것은 이상하게도 RuntimeException의 sub class이다. 이것은 RuntimeException이 아니었으면 하는데 NumberFormat체크는 Runtime시에만 가능한 모양이다.

5.2.3 "OutOfMemoryError는 어떻게 처리해야 하는가?" #

예전에 Swing에서 Tree구조를 이용하는 프로젝트를 한적이 있다. 이때 Tree에 branch와 node가 무수히 생기자 JVM은 OutOfMemoryError를 내뱉었다. 이에 급한 마음에 OutOfMemoryError를 catch하여 사용자에게 재시작을 요청하는 Dialog를 띄우도록 수정하였다면 이 Dialog가 과연 떳을까? 현재 메모리가 부족한 판에 Dialog를 띄울 메모리가 남아있질 않았던 것이다. 다행히 Dialog가 떴어도 작업은 계속되지 못했을 것이다. NullPointerException가 나기 때문이다.

원인은 나중에 찾았는데, Tree구조에서 부모부터 자식들을 붙이는 순으로 Tree를 구성하는데 자식들을 줄줄이 붙여나가다가 메모리 부족현상이 발생하였고 NullPointerException은 자식이 없으니 클릭하는 순간 null을 반환하여 발생하였던 것이다.

OutOfMemoryError의 가장 좋은 해결책은 불필요한 객체를 만들지 않는 것이었다. 그리고 Tree생성시에도 자식부터 만들고 부모를 만드는 순서로 프로그램을 수정하여 프로젝트를 정상적으로 마칠수 있었다.

마지막에 드는 심정은 프로그램이 OutOfMemoryError를 일으키는 원인이 과연 이렇게 구성되어 발생했는지 어떻게 알수 있을까 하는 의문이다.

5.3 Object Serialize #

Java에서는 ObjectOutputStream의 writeObject() method에 데이타 구조 저장소의 참조만 건네주기만 하면 그 안에 있는 모든 객체를 1차원 stream으로 출력해 준다. (파일이나 ByteArrayOutputStream을 이용한 메모리로) 단, static field는 Serialize되지 않는데 이는 Serialize의 대상이 instance 객체뿐이기 때문이다.

5.3.1 "Serialize를 위해서는 marker interface인 java.io.Serializable interface를 implements해야한다." #

여기서 marker interface는 java.lang.Cloneable과 같이 method와 field의 정의는 없지만 객체 Type을 위한 interface이다. 예전에 Serialize를 이용하여 데이타를 유지하는 프로젝트를 한 적이 있는데 그때 생각했던것이 '모든 class들이 기본적으로 Serializable을 implements하고 있으면 편할텐데..'라는 생각이었다. 하지만 이것은 상당히 위험한 발상이었다.

Serializable이 기본으로 implements되어 잇으면 엉뚱한 객체까지 Serialize되고 그것을 알아채지도 못하는 사태가 일어날 가능성이 높다. Serializable이 optional인 이유는 이러한 이유 때문이리라..

5.3.2 "super class는 Serializable이 아닌데 sub class만 Serializable인 경우의 문제점" #

Serialize을 이용하여 프로젝트를 할때 한번쯤 실수할 수 있는 부분이 상속된 class의 Serialize이다. 컴파일 에러도 없고 Deserialize도 잘 되었다. 하지만 키가 되는 값이 null과 0이었다. 영문을 몰라 다른곳을 헤매여도 보다가 결국 찾은 원인은 부모의 field는 Serialize되지 않는다는 것을 알게 되었다. transient와 마찬가지로 형식별 default 값으로 채워졌었다. 이는 컴파일과 실행시 아무런 오류없이 실행되어 나를 힘들게 하였기에 Java가 원망스러웠던 기분좋은 추억이다. ^^;

5.3.3 "transient field의 복원(?)관련" #

Serialize를 이용한 프로젝트를 할때는 writeObject와 readObject를 이용하여 기본적으로 제공하는 Serialize를 customizing할수있다.

Serializable에 대한 API reference에도 다음과 같이 나와있다.

"Serialize와 Deserialize에 대한 특별한 handling을 위해서는 다음 두개의 특별한 메소드를 구현하면 된다."

<PRE class=wiki>private void writeObject(java.io.ObjectOutputStream out) throws IOException;private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;</PRE>

이 두 method가 private으로 되어 있는 것을 보고 처음에는 의아해 했었던 기억이 있다. 이를 protected나 public으로 하면 제대로 동작하지 않는다. 이는 override가 이니기 때문이다. 사실은 속에서 reflectiond을 이용하여 강제적으로 호출되고 있는것이다. reflection에서는 private method까지 찾을 수 있기 때문이다.

또한 private으로 한 가장 큰 이유는 Serialize를 객체자신이 직접 해야 안전하다는 의미도 있지 않을까 하는 생각도 든다. 다시 본론으로 들어가서 transient를 복원하는 것에 얘기를 하자면, 사실 transient는 Serialize대상에서 제외되는 것인데 복원을 할 수 있다는 말이 안된다. 하지만 프로젝트를 진행하다 보면 logic상 가능한 경우가 많이 있다.

즉, 모든 field를 Serialize하지 않고 필요한 것만 하고 특정 field는 Serialize한 field들을 이용하여 복원하는 방법이다. 또한 Serialize당시의 객체 상태와 Deserialize시의 객체상태가 서로 다를 수 있는 field도 그것에 해당된다. cafeid만으로 나머지 field는 DB에서 읽어오게 한다면 나머지 field는 transient로 처리하고 Deserialize시 readObject()에서 복원하는 것이다.

5.3.4 "Stack Overflow에 주의하라!" #

Serialize를 하다보면 참조로 연결된 객체를 recursive하게 거슬러 올라가며 이것이 너무 깊어지면 Stack Overflow가 발생한다. 가령 linked list같은 경우이다. 이것을 Serialize하면 그 요소수만큼 recursive 호출이 발생한다. 과거(JDK1.3.0시절) 프로젝트 당시 JVM이 5111에서 Stack Overflow가 발생했던 기억이 있다.

물론 실행시 java option에 -Xss 를 이용하여 statck 크키를 조절할 수 있지만 이것은 개발자가 아닌 실행하는 사람들에게 부담이었다. JDK의 LinkedList class의 소스를 보면 writeObject()와 readObject()를 다음과 같이 변경하고 있다.

<PRE class=wiki> private synchronized void writeObject(java.io.ObjectOutputStream s) throws IOException { s.defaultWrtieObject(); //이 코드는 무조건 들어가게 되는데 이곳 소스의 System.arraycopy()에서 overflow발생한다. s.writeInt(size); //이부분이 실제 추가되어 Stack Overflow를 예방한다. for(Entry e = ...) s.writeObject(e.element); } ... } //readObject()도 이와 같은 개념으로 변경되어 있다.</PRE>

5.4 "nested class / inner class / 중첩클래스" #

5.4.1 "중첩클래스의 개념" #

개인적으로 중첩클래스를 어떠한 경우는 사용하지 않으려 한다. 사용하기가 만만치 않고 코드 읽기가 힘들어 지기때문이다. 하지만 '어떤 클래스 내에서 은폐할 목적으로 사용하는 클래스가 있다면 이것을 사용해야 한다' 실제로 Java의 AWT 클래스 Event Handler를 비롯하여 많은 클래스에서 중첩클래스를 사용하고 있다. 또한 내부 class는 그것을 둘러싸는 class의 instance(enclosing object라고 하는)의 field를 참조 할수 있는것도 장점이다. 하지만 이는 내부클래스가 아닐경우 부부 클래스를 new해서 사용하는것과 별반 다를께 없지 않은가.

5.4.2 "내부클래스는 부모의 참조를 몰래 보유하고 있다." #

내부 클래스의 instance는 부모의 instance에 대한 참조를 몰래 보유하고 있기 대문에 위에서 얘기한 부모의 field를 참조할 수 있는 것이다. 그러므로 static method에서는 내부클래스를 생성할 수 없다. 다음 예를 보면 바로 알수 있다.

<PRE class=wiki> class Test{ class InnerClass { int i; ... } public static void main(String[] args){ InnerClass icls = new InnerClass(); ... } }</PRE>

이 소스를 compile하면 다음의 오류가 발생한다. "non-static variable this cannot be referenced from a static context..." main method는 static이므로 this를 참조할수 없다는 것이다. 이는 InnerClass가 new 되면서 외부 클래스 Test의 this를 보유해야 하는데 여기서 static을 만나니 오류를 표출시킨것이다. 물론 일반 instance method에서는 오류가 나지 않는다.

5.4.3 "local inner class에 대하여" #

local inner class라 함은 method내에서 선언된 inner class이다.

<PRE class=wiki> public class OuterClass { public int get(){ int i = 9; int id = 99; int id2 = 99; final int id3 = 100000; class LocalInnerClass { int id = 100; LocalInnerClass(){ System.out.println("LocalInnerClass"); } int getId(){ return id3 + id; } } LocalInnerClass lic = new LocalInnerClass(); return id + lic.getId(); } public static void main(String[] args){ OuterClass outer = new OuterClass(); System.out.println("id = " + outer.get()); //결과 값은 "100000(id3) + 100(LocalInnerClass.id) + 99(OuterClass.get())" 인 100199가 나온다. } }</PRE>

위 소스의 LocalInnerClass는 get() 이라는 method에서만 보이는 class이다. 그리고 특이할 만한 부분이 OuterClass의 get() method에서 final로 선언된 id3이 LocalInnerClass에서 참조 가능해 진다. id2를 참조하면 compile error가 나지만 final로 선언된 것은 오류가 나지 않는다.

이는 local variable은 method에서 나오는 순간 사라지는데, local inner class는 local variable보다 수명이 조금더 길기 때문에 final만 허용한 것이다.

5.4.4 "anonymous class(무명클래스)에 대하여" #

무명 클래스는 말그대로 이름이 없는 클래스이다.

<PRE class=wiki> class AnonymousTest { private interface Printable { void print(); } static void doPrint(Printable p){ p.print(); } public static void main(String[] args){ doPrint( new Printable(){ public void print(){ System.out.println("this is new Printable print()"); } }); } }</PRE>

위 소스의 "doPrint( new Printable(){" 부분이 무명클래스 이다. compile을 수행하면 AnonymousTest$Printable.class, AnonymousTest$1.class, AnonymousTest.class 세개의 클래스가 생긴다. 여기서 AnonymousTest$Printable.class는 Printable interface이고 AnonymousTest$1.class이 무명클래스이다.

이 소스를 보면 처음에 드는 의심이 Printable interface를 new 했다는 것이다. 여기서 굳이super class(이 소스에서는 interface)를 저정해야 하는 이유는 아무것도 상속하지 않는 무명 클래스의 instance를 만들어 봐야 의미가 없기 때문에 이렇게 한듯하다.

"무명클래스는 어떤 class나 interface를 상속/구현 해야만 그 instance를 사용할 수 있는 것이다"
이처럼 무명 클래스를 사용하면 어떤 절차(수행)를 다른 method의 인수로 건네줄 수 있게 된다. 하지만 간단한 로직만 구현처리해야 한다.

"무명클래스는 조금만 복잡해져도 급격히 소스의 가독성이 떨어지게 되므로 남용하지 않는 것이 바람직하다"

6 이래도 Java가 간단한가? #

6.1 method overload 에서의 혼란? #

6.1.1 "overload란 이름이 가고 인수가 다른 method에 compiler가 다른 이름을 붙이는 기능" #

overload를 구현하면 bytecode로 변환시 다른 이름으로 method가 변환되어 별개의 method로 처리된다. 이를 JVM에서 method descripter라 하여 Oolong asembler로 변화시 다른 형태의 method가 된다. 예를 들어 "void get(double d, long l)" 은 "get(DJ)V"로 변경된다. 여기서 D는 double, J는 long, V는 void를 의미한다.

그런데 여기서 "get(DJ)" 부분만 method 이름이므로 return type이 다른 동일 method는 overload 할 수 없다. 따라서 overload는 정적(compile시 결정)이라는 명제가 성립니다. 그래서 동적으로 사용되면 compile시 오류를 표출한다. 아래의 소스를 보자. 여기에는 IFS라는 interface와 이를 implements한 Impl1, Impl2 라는 class가 있다.

<PRE class=wiki> //IFS.java interface IFS { public String getName(); } //Impl1.java class Impl1 implements IFS { public String getName(){ return "Impl1"; } } //Impl2.java class Impl2 implements IFS { public String getName(){ return "Impl2"; } } //main이 있는 OverloadTest.java public class OverloadTest { static void pr(int i){ System.out.println("pr_int : " + i); } static void pr(String s){ System.out.println("pr_string : " + s); } static void pr(IFS ifs){ System.out.println("pr_string : " + ifs.getName()); } static void pr_run(Impl1 i1){ System.out.println("pr_run : " + i1.getName()); } static void pr_run(Impl2 i2){ System.out.println("pr_run : " + i2.getName()); } public static void main(String[] args){ OverloadTest test = new OverloadTest(); test.pr(10); test.pr("Jeid"); IFS ifs1 = new Impl1(); test.pr(ifs1); IFS ifs2 = new Impl2(); test.pr(ifs2); //pr_run(ifs1); //pr_run(ifs2); } }</PRE>

위의 소스를 수행하면 정상적으로 compile이 될것인가?

당연히 잘 된다. pr()은 overload를 잘 구현했다. 하지만 소스 하단의 두 주석문을 풀면 어떻게 될까? 이는 compile오류를 낸다.

<PRE class=wiki> OverloadTest.java:36: cannot resolve symbol symbol : method pr_run (IFS) location: class OverloadTest pr_run(ifs1); ^ OverloadTest.java:37: cannot resolve symbol symbol : method pr_run (IFS) location: class OverloadTest pr_run(ifs2); ^ 2 errors</PRE>

실제 위 둘의 pr_run method는 bytecode로 변환시 "pr_run(Lpackage_name.IFS)V"로 동일하게 생성된다. 따라서 compile시에 오류를 표출한다. 이 소스를 보면 알 수 있듯이 "method overload는 정적(compile시)으로 미리 결정되며, 동적(실행시판단)으로 사용할수 없다."

6.1.2 "그렇다면 overload에서 실제로 혼동되는 부분은 무엇인가?" #

다음 소스를 보고 실제로 수행되는 method를 찾아보라.

<PRE class=wiki> class OverloadTest2 { static int base(double a, double b){ ... } //method A static int count(int a, int b){ ... } //method B static int count(double a, double b){ ... } //method C static int sum(int a, double b){ ... } //method D static int sum(double a, int b){ ... } //method E }</PRE>

  • base(3,4) 를 호출했을때 수행되는 method는? => 당연히 method A (3과 4는 정수라도 double이 되므로 정상적으로 수행됨)

  • count(3,4) 를 호출했을때 수행되는 method는? => B와 C중 갈등이 생긴다. 이럴경우 JVM은 가장 한정적(more specific)한 method를 찾는다. 여기서 3과 4는 정수형에 가까우므로 method B 가 호출된다.

  • count(3, 4.0) 을 호출했을때 수행되는 method는? => 이것은 4.0 이 double이므로 method C 가 더 한정적이므로 method C 가 호출된다.
  • sum(3,4.0) 을 호출했을때 수행되는 method는? => 이것은 당연히 type이 일치하는 method D.
  • sum(3,4) 를 호출했을때 수행되는 method는?? => 이런 코드가 소스내에 있으면 다음과 같은 compile 오류를 표출한다.

<PRE class=wiki> OverloadTest.java:48: reference to sum is ambiguous, both method sum(int,double) in OverloadTest and method sum(double,int) in OverloadTest match System.out.println("sum(3,4) = " + sum(3,4)); ^ 1 error</PRE>

method D와 method E가 애매하다는 compile 오류이다. 이것은 둘중 어느것이 더 한정적인지 찾을 수 없으므로 bytecode 를 생성 할 수 없다는 것이다.

"이렇듯 compiler에게 불필요한 오해(혼동)를 초래하는 overload는 사용하지 않는 것이 좋다. 개인적으로 overload를 가능한 사용하지 않으려 하고 필요하다면 인수의 개수가 다른 overload를 사용하는 편이다."

6.1.3 (참고) 또다른 혼동, overload한 method를 override 하면? #

overload란 compiler가 bytecode변환시 다른 이름을 붙이는 기능이라는 것을 위에서 설명했다. 따라서 super class에서 overload한 method를 상속하여 override하면 완전 별개의 method를 override한것처럼 JVM은 판단한다. 즉, overload와 override는 직교(전혀상관없는)하는 개념이다.

6.2 상속/override/은폐 에서의 복잡함 #

6.2.1 "Java class의 member 4 종류" #

  1. instance field
  2. instance method
  3. static field
  4. static method
여기서 상속을 하였을 경우 runtime시 객체의 형식에 따라 선택되는 것은? 2번 instance method 뿐이다. 즉, 동명의 member를 sub class에서 선언했을 때 instance method만 override 되고 나머지는 완전 별개의 member가 된다. 따라서 위의 1,3,4는 sub class에서 동일하게 선언했을 경우 별개의 것으로 인식되며 compile시에 무엇을 access 할지 결정된다.

즉, instance method는 override되지만 instance field/static field는 은폐된다. override는 실행시 객체의 형식에 따라 처리 할당되지만, 은폐의 경우는 compile시에 결정되고 만다.

6.2.2 "override시 method 이름에 대한 함정" #

과거에 코딩을 하던중 정말이지 어처구니 없는 경우를 당했다. override 하는 method이름을 잘못써서 황당한(?) 고생을 한적이 있다. super class의 writable()이라는 method를 writeable()이라고 override(?)하였는데 프로그램 수행 중에 writable()이 항상 false가 나오는 것이 아닌가? 그래서 소스를 추적추적 하다 몇시간을 허비했었던 기억이 있다.

java를 접한지 얼마되지 않았고 요즘같이 eclipse같은 에디터도 없이 메모장에서 코딩하던 시절이라 더욱 고생했던것 같다. 한참 후에야 우연히 스펠링이 잘못된걸 알고 얼마나 황당했던지... 지금 생각하면 이것도 좋은 추억이리라.

무조건 override 잘 되었을거라 생각 했던 나의 불찰도 있었지만 compile때나 runtime시 아무런 반응을 보이지 않던 Java도 원망스러웠다. 2003년도에 C#으로 프로젝트를 했는데 C#은 상속의 override에 대하여 "override void writalbe().."과 같이 정의시 override를 명시해야 된다는 것을 보고 상당히 마음에 들어 했던 기억이 있다. 가독성도 뛰어날 뿐더러 나의 몇시간동안의 헤메임도 없을 것이기 때문다. Java도 이렇게 확실한 명세였으면 정말 좋겠다.

6.2.3 "또다른 나의(?) 실수 - 말도 안되는 오타" #

위의 method이름을 잘못써서 고생하기 이전에 아주 비슷한 고생을 한적이 있다.

'난 정말 바보인가'라는 생각을 들게 했던 문제였다. 초보 시절에는 왜이리도 오타가 많이 나던지... 요즘은 대충 키보드 두드려도 오타가 잘 안나는데 그 시절에 오타 때문에 느린 CPU에서 컴파일을 몇번을 했는지...
기억을 되살리면 소스는 다음과 같다.

<PRE class=wiki> public class Member { private int memberNo; public int getMemberNo(){ return this.memberNo; } public void setMemberNo(int menberNo){ this.memberNo = memberNo; } ...... }</PRE>


위 소스의 Member에는 다른 여러가지 member field가 있는데 DB의 member table에 memberid 컬럼이 memberno로 변경되면서 Member class의 memberId를 memberNo로 변경하게 되었다. 위와 같이 수정하여 배포해놓고 테스트를 하는데 시스템이 완전히 뒤죽박죽으로 돌아버리는 것이 아닌가. 이 경우도 method 이름처럼 몇시간을 헤매었다.

이번에 argument의 오타로 인한 어처구니 없는 실수였다. setMemberNo(int menberNo)에서 문제가 발생되었던 것이다. 인수의 memberNo를 menberNo로 잘못친것이다. 그래서 memberNo에는 해당 member의 memberno가 아닌 0이 모두 들어갔어던 것이다. 시스템은 memberno를 기준으로 도는 부분이 너무나 많았기에 오류나는 부분도 많았으며 DB에서는 제대로 된 memberno을 읽어 왔으며, compile과 runtime시 아무런 반응도 없었기에, 초보자를 그렇게도 고생시켰나 보다.

이것도 member field면 무조건 this를 붙이도록 하던지 Java가 인수는 'm_'와 prefix를 붙이도록 Coding Style을 정의- SUN사이트의 Java Coding 규약에는 "Variable names should not start width underscore_ or dollar sign $ characters, even though both are allowed." 와 같이 명시되어 있다 - 했더라면 발생하지 않았을 문제이다.

또한 C언어나 C#에서 처럼 compile 경고레벨을 높여놓으면 "menberNo는 어디서도 사용하지 않습니다."와 같은 메세지를 보여 줬더라면 고생을 덜 하지 않았을까?

6.2.4 "static member를 instance를 경유하여 참조해서는 안 된다." #

예를 들어 ClassA 에 public static int AA 라는 static field가 있을 경우 ClassA.AA 로 접근해야 하는데, 다음과 같이 사용하는 실수를 범한다.(물론 오류는 없지만)

<PRE class=wiki> ClassA a = new ClassA(); int i = a.AA; //instance를 경유하여 접근 int j = ClassA.AA; //올바르게 접근</PRE>

그럼 왜 굳이 ClassA.AA와 같이 instance가 아닌 class이름을 붙여야 할까?

static member(static field/static method)는 compile시에 이미 어느것을 호출할 지 결정하기 때문에 위의 a.AA와 같은 것은 static이 아닌것 같은 오해와 혼란만 가져오기 때문이다. 심지어 개인적으로는 동일 class 내 - 위 소스에서 ClassA의 member method - 에서 ClassA.AA라고 사용하는 편이다.

이는 local variable과 혼동될 염려도 없을뿐더러 AA라는 변수가 static이라는 것도 확실히 알 수 있기 때문이다. 물론 private static 의 경우는 ClassA.BB 와 같이 하지 않고 BB 라고 해도 무방하겠지만 말이다.

6.2.5 "super keyword는 부모의 this" #

Java 개발자 대부분은 'super' 에 대하여 그렇게 민감하지 않을 것이다. 그거 super() 나 super.method1() 과 같이 사용되지 그 이상에 대해선 깊이 생각하지 않게 된다. super를 한마디로 정리하면 다음과 같다.

"super keyword는 instance method등에서 this를 사용할 수 있는 곳에서만 쓸 수 있다. this의 자리에 super라고 쓰면 현재 class의 member가 참조되는 대신 부모 class의 member가 참조되는 것이다."

6.3 상속에 관한 또 다른 문제 #


6.4 그밖의 함정 #

6.4.1 "생성자에 void 를 붙인다면?" #

생성자에 void를 붙인다면 그 class가 new 될때 그 생성자(?)가 실행될까?? 아래의 'Constuctor'라는 문자열은 출력될까?

<PRE class=wiki> public class ConstructorTest{ void ConstructorTest(){ System.out.println("Constuctor"); } ..... }</PRE>

출력되지 않는다. 물론 compile시 아무런 경고도 없었다. 즉, void가 붙은 ConstructorTest()는 생성자가 아니라 instance method일 뿐이었고 new시에는 default constructor가 실행 되었던 것이다.

6.4.2 "if / switch 의 함정" #

Java 개발자라면 대부분이 초보시절에 if 조건절에 '==' 대신 '='을 써본 기억이 있을것이다. 예를 들어 "if( isListenLecture == Student.STUDENT )" 를 "if( isListenLecture = Student.STUDENT )" 로 잘못 쓴 경우이다. 여기서 Student.STUDENT는 boolean type이다. 여기서 isListenLecture는 항상 Student.STUDENT 값을 갖게 되는 버그가 생긴다. 이는 compile시에 아무런 경고도 없다. 이렇게 한번 당하고 나면 앞으로는 '=='를 정확히 쓰게 되거나 아니면 다음과 같이 쓴다.

"if( isListenLecture )" 또는 "if( !isListenLecture )" 라고 말이다. 이것이 더욱 간결하고 의미도 분명해 지기 때문이다. 또한 다음 소스와 같은 오류도 범하는 경우가 있다. 이는 잘못된 indentation으로 빚어지는 초보의 함정이다.

이글을 읽는 분께 한가지 당부드리고 싶은것은 여기서 초보라고 다 그런건 아니라는 것이다.

<PRE class=wiki> .... if( a < 5 ) b = 3; c = 10; //이부분은 나중에 추가된 라인이다. if( isStudent ) if( isFemale ) sayHello("Hi~~"); else sayHello("Hello Professor~");</PRE>

위의 소스중 c = 10; 이 if( a < 5 )의 참일때 수행된다고 오해할 수도 있고, sayHello("Hello Professor~"); 부분이 if( isStudent )의 else 부분이라고 오해 할 수도 있다. 이것은 전적으로 indentation(들여쓰기)의 불찰로 개발자가 잘못 읽을 수 있는 부분이다. Java Coding Style에서는 if문 다음에 한줄의 코드가 있더라도 {} 를 사용하길 권고한다. 그러면 첫번째 if문과 같은 오류를 방지할 수 있고 두번째 if문에서도 보다 가독성이 생길 것이다.

이와 유사한 것으로 switch문의 case 절에서 break를 쓰지 않아 항상 동일하게 처리되는 버그도 경험해 보았을 것이다.

7 Java 기능 적용 몇가지 #

7.1 대규모 개발에서 interface 분리하기 #

7.1.1 "interface 분리의 필요성" #

Java와 같은 객체지향언어에서는 공개해야 할 method만을 public으로 하고, 공개할 필요가 없는 것은 private으로 하여 class의 상세한 내용을 은폐할 수 있게 되어 있다. 그런데 private 부분이 은폐되어 있는것 처럼 보이는가?

소스를 보면 훤히 들여다 보이는데?

대규모 개발은 하부 class부터 bottom-up으로 진행하는 것이 이상적인 형태일 것이다. 그런 형태로 개발하면 임의의 시점에서 테스트를 할 수도 있다. 그러나 현실적으로 단기간에 많은 수의 개발자가 붙어서 단시간에 개발을 진행하는 경우가 많다. 또한 서로 호응하는 관계에 있는 class들은 어느쪽이 하부인지 정의하기가 난감할때가 많다. 이런경우 우리는 흔히 package단위로 나누어 개발한다. 하지만 이럴경우 어느정도 코딩이 종료될때까지 테스트하기가 상당히 힘들어 진다. Java에서는 private member와 method 구현까지 하나의 파일에 코딩하는데 개발 중간에 공개하여 다른 개발자가 이용해야 하는 class를 배포할 수 없으므로 동시 개발이 까칠해 진다.

이 상황에서 다른 package(개발자)에 공개해야 하는 class 부분을 interface로 공개하면 많은 부분 유연하게 된다. 이 interface를 다른 개발자는 개발을 하고 테스트가 필요하다면 TestImpl class를 만들어 하면된다. RMI나 CORBA에서도 Stub은 이런식으로 IDL을 정의한다.

7.2 Java에서의 열거형 #

Java에서는 열거형-C의 구조체, 공용체-이 없다. 열거형이 왜 필요하냐고 반문하는 개발자도 있을 것이다.

하지만 열거형이 없어 곤란을 경험한 개발자도 꽤 있으리라 본다. 최근언어(특히 객체지향 언어) - Java, Eiffel, Oberon등 - 에는 열거형은 포함되어 있지 않다. C#에는 있긴 하지만.

이런 이유로 Java AWT의 Label class는 다음과 같이 구현되어 있다.(텍스트의 정렬값관련)

<PRE class=wiki> public static final int LEFT = 0; public static final int CENTER = 1; public static final int RIGHT = 2; ... label.setAlignment(Label.CENTER); ...</PRE>

하지만 위의 소스에는 문제가 있다. setAlignment() method의 인자가 int인 것이다. 만약 위에 정의한 0, 1, 2가 아닌 다른 int 값이 들어가도 compile/runtime시 알수가 없다. 그래서 주석을 달게 되는데, 주석이라 함은 정말이지 최후의 수단이라고 봐야 한다.

실제로 우리가 개발해 놓은 소스에도 이런부분이 있으리라 예상된다. 이 문제를 어떻게 하면 해결할 수 있을까?
Java에서 열거형을 한번 만들어 보자.

<PRE class=wiki> //LabelAlignment.java public class LabelAlignment { private LabelAlignment() {} //이는 생성자를 private으로 하여 다른데서는 만들지 못하도록 하기위함이다. public static final LabelAlignment LEFT = new LabelAlignment(): public static final LabelAlignment CENTER = new LabelAlignment(): public static final LabelAlignment RIGHT = new LabelAlignment(): } //변형된 Label.java 의 일부.. public synchronized void setAlignment(LabelAlignment alignment){ if( alignment == LabelAlignment.LEFT ){ ...//왼쪽으로 맞추기.. }else if( ... ... } } ...</PRE>

위에서 작성한 소스는 잘 작동한다. 서로 다른 3개의 instance이므로 reference가 달라 '==' 연산도 가능하고, 훌륭하다.

하지만 한가지 문제가 있다. LabelAlignment가 Serializable한 class에서 serialize되었다 deserialize 된다면?

LabelAlignment alignment 는 새로운 instance가 되고 serialize전의 reference와 다른 참조 위치를 갖게 되어 '==' 연산은 버그를 발생시킨다.
그럼 이것만 해결하면 되겠는데, 어떻게 refactoring하면 될 것인가? '==' 연산 대신 equals로 변형하면 되겠는데.

<PRE class=wiki> //LabelAlignment.java public class LabelAlignment { private int flag; private LabelAlignment(int flag){ this.flag = flag; } public static final LabelAlignment LEFT = new LabelAlignment(0): public static final LabelAlignment CENTER = new LabelAlignment(1): public static final LabelAlignment RIGHT = new LabelAlignment(2): public boolean equals(Object obj){ return ((LabelAlignment)obj).flag == this.flag; } } //변형된 Label.java 의 일부.. public synchronized void setAlignment(LabelAlignment alignment){ if( LabelAlignment.LEFT.equals(alignment) ){ ...//왼쪽으로 맞추기.. }else if( ... ... } } ...</PRE>

하하, Serialize까지 잘 작동한다. ^^;

여기서 Debug를 고려한다면 0, 1, 2 대신 문자열로 "LEFT", "CENTER", "RIGHT"로 한다면 더욱 명확하지 않을까?

(주의) 위에서처럼 LabelAlignment.LEFT 라고 쓰기 싫어서 상수 interface를 만들어 그걸 implements 하여 그냥 LEFT 라고 쓰는 것을 뿌듯해 하며 쓰는 개발자들이 있다. 물론 Swing의 소스들을 보다보면 SwingConstants라는 interface에 LEFT를 비롯하여 온갖 잡다한 상수를 집어넣어놓고 여기 저기서 implements해서 사용하고 있다. 이런 코딩 스타일은 '내 스타일이야~' 가 아니라 냄새나는 코드이다.

LEFT라는 것이 구현한 class에 이미 있을 수 있을 수 있을뿐아니라 구현한 모든 클래스에서 LEFT를 보유하여 SwingConstants.LEFT뿐 아니라 Impl.LEFT로도 사용되게 되어 온갖 혼란을 초래하게 된다. 입력량을 줄이기 위해 interface를 implements 해서는 안되지 않을까?

7.3 Debug write #

C에서는 다음과 같이 pre-process로 정의하면 DEBUG라는 식별자를 #define하지 않으면 컴파일후 해당 소스의 부분이 삭제된다.

<PRE class=wiki> #ifdef DEBUG fprintf(stderr, "error...%d\n", error); #endif /* DEBUG */</PRE>

그럼 Java에서는?

Java에서는 Pre-process가 없지만 다음과 같이 작성했을때 Debug.isDebug 가 final로 선언되어 있으면 compile후 아래 3줄 모두 삭제 된다.(단 Debug.isDebug 가 false 로 초기화 되었다면 제거된다.)

<PRE class=wiki> if( Debug.isDebug ){ System.out.println("error..." + error); }</PRE>

Java는 compile시 byte code 생성시 final은 정적으로 판단하여 미리 정의하기 때문에 위의 3줄은 삭제될 수 있다. if문과 함께 없어지게 되므로 처리 속도에 피해를 주지 않는다. 단, 주의해야 할 점은 Debug.isDebug 값이 변경되면 이 것을 사용하고 있는 측도 모두 함께 다시 compile해야 한다. bytecode를 다시 만들어야 하기 때문이다.

그런데, 이 소스를 Debug.write()와 같이 static 으로 하여 이 method내에서 판단하게 하면 편리할텐데. 그리고 class별로 ON/OFF 처리를 할 수 있으면 좋을텐데, 어찌 하면 가능할 것인가?

그럼 먼저 호출한 쪽의 class이름을 찾아보자. 접근은 Exception의 printStackTrace()로 부터 시작되었다. 하지만 이 소스에는 Exception 객체를 new한 시점에 결정되어 있다. 그래서 부모인 Throwable의 생성자를 확인해 보니 fillInStackTrace() 로 되어있는데 이 method는 native method였다.

API Reference를 보면 Thread class에서는 dumpStackTrace()라는 method가 있었다. 소스를 보니, 그것도 생성시점이었다. 아무래도 예외방면에서 찾는건 무리인듯 했다.

그래서 class의 호출계층을 나타내는 java.lang.SecurityManager의 getClassContext() method로 접근하였다. sample 소스는 다음과 같다.

<PRE class=wiki> // 1. GetCallerSecurityManager.java public final class GetCallerSecurityManager extends SecurityManager { public Class[] getStackTrace(){ return this.getClassContext(); } } // 2. GetCallerClass.java public final class GetCallerClass { private static GetCallerSecurityManager mgr; static{ mgr = new GetCallerSecurityManager(); System.setSecurityManager(mgr); } public static void writeCaller(String str){ Class[] stk = mgr.getStackTrace(); int size = stk.length; for(int i = 0; i < size; i++){ System.out.println("stk[" + i + "] = " + stk[i]); } String className = stk[2].getName(); System.out.println("className is " + className + " : " + str); } } // 3. GetCallerClassMain1 : 호출하는 클래스 예제 1 public class GetCallerClassMain1 { public static void main(String[] args){ GetCallerClass.writeCaller(", real is 1."); } } // 4. GetCallerClassMain1 : 호출하는 클래스 예제 2 public class GetCallerClassMain2 { public static void main(String[] args){ GetCallerClass.writeCaller(", real is 2."); } }</PRE>

위의 3번 주석과 4번 주석 부분을 수행하면 다음과 같은 결과가 나온다.

<PRE class=wiki> className is GetCallerClassMain1 : , real is 1. className is GetCallerClassMain2 : , real is 2.</PRE>

정확히 호출한 클래스를 표현하고 있다. 이것을 비교해서 클래스별 ON/OFF를 구현하면 된다.

8 Java 5.0 Tiger 에 대하여 #

Tiger에서는 새로운 개념의 적용이 많은 부분 시도 되었다. 이중 가장 기본이 되는 몇가지를 살펴보자.

8.1 Working with java.util.Arrays #

Tiger에서는 무엇보다도 Collection class들에 대해 많은 부분 정비하였다. 예를 들면 for/in 구문 지원과 Generic Type member와 Arrays Utility class 등이다. 그럼 Collection에 대한 static method들을 담고 있는 Arrays 에 대해 다음 example로 한눈에 살펴보자.

<PRE class=wiki>package com.jeid.tiger;import java.util.Arrays;import java.util.Comparator;import java.util.List;public class ArraysTester { private int[] arr; private String[] strs; public ArraysTester(int size) { arr = new int[size]; strs = new String[size]; for (int i = 0; i < size; i++) { if (i < 10) { arr[i] = 100 + i; } else if (i < 20) { arr[i] = 1000 - i; } else { arr[i] = i; } strs[i] = "str" + arr[i]; } } public int[] getArr() { return this.arr; } public String[] getStrs() { return this.strs; } public static void main(String[] args) { int size = 50; ArraysTester tester = new ArraysTester(size); int[] testerArr = tester.getArr(); int[] cloneArr = tester.getArr().clone(); String[] testerStrs = tester.getStrs(); String[] cloneStrs = tester.getStrs().clone(); // clone test if (Arrays.equals(cloneArr, testerArr)) { System.out.println("clonse int array is same."); } else { System.out.println("clonse int array is NOT same."); } if (Arrays.equals(cloneStrs, testerStrs)) { System.out.println("clonse String array is same."); } else { System.out.println("clonse String array is NOT same."); } // 2부터 10까지 값 셋팅 Arrays.fill(cloneArr, 2, 10, new Double(Math.PI).intValue()); testerArr[10] = 98; testerStrs[10] = "corea"; testerStrs[11] = null; List<String> listTest = Arrays.asList(testerStrs); System.out.println("listTest[10] = " + listTest.get(10)); System.out.println("------- unsorted arr -------"); System.out.println("Arrays.toString(int[]) = " + Arrays.toString(testerArr)); System.out.println("Arrays.toString(String[]) = " + Arrays.toString(testerStrs)); Arrays.sort(testerArr); // Arrays.sort(testerStrs); //NullPointerException in sort method..(null이 없더라도 길이에 대한 크기 체크는 못함) Arrays.sort(testerStrs, new Comparator<String>() { public int compare(String s1, String s2) { if (s1 == null && s2 == null) { return 0; } else if (s1 == null && s2 != null) { return -1; } else if (s1 != null && s2 == null) { return 1; } else if (s1.length() < s2.length()) { return -1; } else if (s1.length() > s2.length()) { return 1; } else if (s1.length() == s2.length()) { return 0; } else { return s1.compareTo(s2); } } }); System.out.println("------- sorted arr -------"); System.out.println("Arrays.toString(int[]) = " + Arrays.toString(testerArr)); System.out.println("Arrays.toString(String[]) = " + Arrays.toString(testerStrs)); System.out.println("------------------------------------------------"); String[][] mstrs1 = { { "A", "B" }, { "C", "D" } }; String[][] mstrs2 = { { "a", "b" }, { "c", "d" } }; String[][] mstrs3 = { { "A", "B" }, { "C", "D" } }; System.out.println("Arrays.deepToString(mstrs1) = " + Arrays.deepToString(mstrs1)); System.out.println("Arrays.deepToString(mstrs2) = " + Arrays.deepToString(mstrs2)); System.out.println("Arrays.deepToString(mstrs3) = " + Arrays.deepToString(mstrs3)); if( Arrays.deepEquals(mstrs1, mstrs2)) { System.out.println("mstrs1 is same the mstrs2."); }else { System.out.println("mstrs1 is NOT same the mstrs2."); } if( Arrays.deepEquals(mstrs1, mstrs3)) { System.out.println("mstrs1 is same the mstrs3."); }else { System.out.println("mstrs1 is NOT same the mstrs3."); } System.out.println("mstrs1's hashCode = " + Arrays.deepHashCode(mstrs1)); System.out.println("mstrs2's hashCode = " + Arrays.deepHashCode(mstrs2)); System.out.println("mstrs3's hashCode = " + Arrays.deepHashCode(mstrs3)); }}</PRE>

8.2 Using java.util.Queue interface #

Queue를 이용하여 First In First OutOrdering한 Queue를 구현 가능하다.

<PRE class=wiki>package com.jeid.tiger;import java.util.LinkedList;import java.util.PriorityQueue;import java.util.Queue;public class QueueTester { public static void main(String[] args) { System.out.println("---------- testFIFO ----------"); testFIFO(); System.out.println("---------- testOrdering ----------"); testOrdering(); } private static void testFIFO() { Queue<String> q = new LinkedList<String>(); q.add("First"); q.add("Second"); q.add("Third"); String str; while ((str = q.poll()) != null) { System.out.println(str); } } private static void testOrdering() { int size = 10; Queue<Integer> qi = new PriorityQueue<Integer>(size); Queue<String> qs = new PriorityQueue<String>(size); for (int i = 0; i < size; i++) { qi.offer(10 - i); qs.offer("str" + (10 - i)); } for (int i = 0; i < size; i++) { System.out.println("qi[" + i + "] = " + qi.poll() + ", qs[" + i + "] = " + qs.poll()); } }}</PRE>

8.3 java.lang.StringBuilder 사용하기 #

StringBuffer가 synchronize하지 않은 method들로 구성된 듯한 StringBuilder를 사용하므로 성능 향상을 도모할수 있다. 사용법은 StringBuffer와 동일하다.

<PRE class=wiki>package com.jeid.tiger;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class StringBuilderTester { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("str1"); list.add("str2"); list.add("str3"); String ret = appendItems(list); System.out.println("ret = " + ret); } private static String appendItems(List<String> list) { StringBuilder sb = new StringBuilder(); for (Iterator<String> iter = list.iterator(); iter.hasNext();) { sb.append(iter.next()).append(" "); } return sb.toString(); }}</PRE>

8.4 Using Type-Safe Lists #

Collection에 type을 명시하여 type-safe 하게 처리 가능. 아래에서 type을 명시하지 않을 경우 compile error가 남을 보여준다. tip으로 Number를 이용하여 byte, short, int, long, double, float 동시 사용하는 부분 참조.

<PRE class=wiki>package com.jeid.tiger;import java.util.Iterator;import java.util.LinkedList;import java.util.List;public class ListTester { public static void main(String[] args) { List<String> list = new LinkedList<String>(); list.add("str1"); list.add("str2"); list.add(new Integer(123)); // <-- String이 아니므로 compile error!! //Iterator에 String type을 명시하므로 정삭작동됨. for (Iterator<String> iter = list.iterator(); iter.hasNext();) { String str = iter.next(); System.out.println("srt = " + str); } //Iterator에 String type을 명시하지 않았으므로 아래 A 부분에서 compile 오류 발생!! for (Iterator iter = list.iterator(); iter.hasNext();) { String str = iter.next(); //A System.out.println("srt = " + str); } //byte, short, int, long, double, float 동시 사용 List<Number> lstNum = new LinkedList<Number>(); lstNum.add(1); lstNum.add(1.2); for (Iterator<Number> iter = lstNum.iterator(); iter.hasNext();) { Number num = iter.next(); System.out.println("num = " + num); } }}</PRE>

8.5 Writing Generic Types #

class 나 interface keyword에 type을 명시하여 동일 타입 명시 가능. 주의 할 점은 any type은 static 일 수 없다.(동적으로 type이 정해지므로)

<PRE class=wiki>class AnyTypeList<T> {//class AnyTypeList<T extends Number> { // <-- 이는 Number를 상속한 type은 허용하겠다는 의미. private List<T> list; //private static List<T> list; // <-- 이는 정적이므로 compile error 발생!!! public AnyTypeList(){ list = new LinkedList<T>(); } public boolean isEmpty(){ return list == null || list.size() == 0; } public void add(T t){ list.add(t); } public T grap(){ if (!isEmpty() ) { return list.get(0); } else { return null; } }}</PRE>

8.6 새로운 static final enum #

예제를 통해 알아보자.

<PRE class=wiki>package com.jeid.tiger;import com.jeid.BaseObject;import com.jeid.MyLevel;public class EnumTester extends BaseObject { private static long start = System.currentTimeMillis(); public static void main(String[] args) { try { test(); enum1(); } catch (Exception e) { e.printStackTrace(); } printEllapseTime(); } private static void test() throws Exception { byte[] b = new byte[0]; System.out.println(b.length); } private static void enum1() { //enum TestEnum { A, B }; //enum cannot be local!!! for(MyVO.TestEnum te: MyVO.TestEnum.values()){ System.out.println("Allow TestEnum value : " + te); } System.out.println("---------------------------------------"); MyVO vo = new MyVO(); vo.setName("enum1"); vo.setLevel(MyLevel.A); System.out.println(vo); System.out.println("isA = " + vo.isA() + ", isGradeA = " + vo.isLevelA()+ ", isValueOfA = " + vo.isValueOfA()); System.out.println("getLevelInKorean = " + vo.getLevelInKorean()); } private static void printEllapseTime() { System.out.println("==> ellapseTime is " + (System.currentTimeMillis() - start) + " ms."); }}package com.jeid.tiger;import com.jeid.BaseObject;import com.jeid.MyLevel;public class MyVO extends BaseObject { enum TestEnum { A, B }; // this is same public static final private int id; private String name; private MyLevel grade; // private List<T> list; public MyLevel getLevel() { return grade; } public void setLevel(MyLevel grade) { this.grade = grade; } public boolean isA() { return "A".equals(this.grade); } public boolean isValueOfA() { return MyLevel.valueOf("A").equals(grade); } public boolean isLevelA() { return MyLevel.A.equals(this.grade); } //A,B,C..대신 0,1,2... 도 동일함. public String getLevelInKorean() { switch(this.grade){ case A: return "수"; case B: return "우"; case C: return "미"; case D: return "양"; case E: return "가"; default: return "없음"; } } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}</PRE>

8.7 Using java.util.EnumMap #

java.util.Map과 동일하나 key가 enum type이어 한다. 예제로 살펴보자.

<PRE class=wiki>package com.jeid.tiger;import java.util.EnumMap;public class EnumMapTester { private enum MyEnum { A, B, C }; // this is same the static final.. public static void main(String[] args) { MyEnum[] enums = MyEnum.values(); System.out.println("MyEnum is " + enums[0] + ", " + enums[1] + ", " + enums[2]); EnumMap<MyEnum, String> em = new EnumMap<MyEnum, String>(MyEnum.class); em.put(MyEnum.A, "수"); em.put(MyEnum.B, "우"); em.put(MyEnum.C, "미"); em.put(MyEnum.B, "가"); //key 중복은 HashMap과 동일하게 overwrite임. for (MyEnum myEnum : MyEnum.values()) { System.out.println(myEnum + " => " + em.get(myEnum)); } }}</PRE>

8.8 Using java.util.EnumSet #

java.util.Set과 동일하나 value가 enum type이어 한다. 예제로 살펴보자.

<PRE class=wiki>package com.jeid.tiger;import java.util.EnumSet;public class EnumSetTester { private enum MyEnum { A, B, C, a, b, c }; // this is same the static final.. public static void main(String[] args) { MyEnum[] enums = MyEnum.values(); System.out.println("MyEnum is " + enums[0] + ", " + enums[1] + ", " + enums[2]); EnumSet<MyEnum> es1 = EnumSet.of(MyEnum.A, MyEnum.B, MyEnum.C); EnumSet<MyEnum> es2 = EnumSet.of(MyEnum.a, MyEnum.b, MyEnum.c); EnumSet<MyEnum> es3 = EnumSet.range(MyEnum.a, MyEnum.c); if (es2.equals(es3)) { System.out.println("e2 is same e3."); } for (MyEnum myEnum : MyEnum.values()) { System.out.println(myEnum + " contains => " + es1.contains(myEnum)); } }}</PRE>

8.9 Convert Primitives to Wrapper Types #

int, short, char, long, double등 primitive와 이들의 Object Wrapper 인 Integer, Shrt, Char등 간의 converting에 있어 자동으로 처리해주는 boxing과 unboxing이 지원 됨에 따라 type에 대한 유연한 처리가 가능해졌다. 예제로 살펴보자.

<PRE class=wiki>package com.jeid.tiger;public class AutoBoxingTester { public static void main(String[] args) { int i = 0; Integer ii = i; // boxing. JDK 1.4에서는 incompatible type error가 발생 했었으나 Tiger에서는 괜찮다. int j = ii; // unboxing for (ii = 0; ii < 5; ii++) { // Integer인데도 ++ 연산자 지원. } i = 129; ii = 129; if (ii == i) { System.out.println("i is same ii."); } // -128 ~ 127 사이의 수는 unboxing이 되어 == 연산이 허용되지만, // 그 범위 외의 경우 Integer로 boxing된 상태므로 equals를 이용해야함. // 이는 버그가 발생했을 경우 찾기 쉽지 않은 단점도 내포하고 있다.!! checkIntegerSame(127, 127); // same checkIntegerSame(128, 128); // Not same checkIntegerEquals(128, 128); // equals checkIntegerSame(-128, -128); // same checkIntegerSame(-129, -129); // Not same checkIntegerEquals(-129, -129); // equals System.out.println("--------------------------------------------"); Boolean arriving = false; Boolean late = true; String ret = arriving ? (late ? "도착했지만 늦었네요." : "제시간에 잘 도착했군요.") : (late ? "도착도 못하고 늦었군요." : "도착은 못했지만 늦진 않았군요."); System.out.println(ret); StringBuilder sb = new StringBuilder(); sb.append("appended String"); String str = "just String"; boolean mutable = true; CharSequence chSeq = mutable ? sb : str; System.out.println(chSeq); } private static void checkIntegerSame(Integer ii, Integer jj) { if (ii == jj) { System.out.println("ii = " + ii + ", jj = " + jj + " ==> jj is same ii."); } else { System.out.println("ii = " + ii + ", jj = " + jj + " ==> jj is NOT same ii!!"); } } private static void checkIntegerEquals(Integer ii, Integer jj) { if (ii.equals(jj)) { System.out.println("ii = " + ii + ", jj = " + jj + " ==> jj is equals ii."); } else { System.out.println("ii = " + ii + ", jj = " + jj + " ==> jj is NOT equals ii!!"); } }}</PRE>

8.10 Method Overload resolution in AutoBoxing #

int가 127을 초과할 경우 boxing이 이루어 질듯 하지만, method overload에 있어서는 boxing이 이루어 지지 않아 JDK1.4와 동일한 결과를 얻는다. 예제로 살펴보자.

<PRE class=wiki>package com.jeid.tiger;public class OverloadTester { public static void main(String[] args) { double d = 10; Integer ii = new Integer(10); doSomething(10); doSomething(1000); doSomething(ii); doSomething(d); } private static void doSomething(Integer ii) { System.out.println("This is doSomething(Integer)"); } private static void doSomething(double d) { System.out.println("This is doSomething(double)"); }}</PRE>

8.11 가변적인 argument 개수 ... #

인수가 가변적일 경우 인수의 개수가 없는것 부터 다수개까지 모두 지원. 예제로 살펴보자.

<PRE class=wiki>package com.jeid.tiger;public class VarArgsTester { public static void main(String[] args) { setNumbers(1, 2); setNumbers(1, 2, 3, 4); setNumbers(1); // setNumbers(); //해당 되는 method가 없어 compile error!! System.out.println("=============================================="); setNumbers2(1, 2, 3, 4); setNumbers2(1); setNumbers2(); } // this is same setNumbers(int first, int[] others) private static void setNumbers(int first, int... others) { System.out.println("-----------setNumbers()----------- : " + first); for (int i : others) { System.out.println("i = " + i); } } // this is same setNumbers(int[] others) private static void setNumbers2(int... others) { System.out.println("-----------setNumbers2()----------- : " + (others != null && others.length > 0 ? others[0] : "null")); for (int i : others) { System.out.println("i = " + i); } }}</PRE>

8.12 The Three Standard Annotation #

@Override - sign the override from superclass.

<PRE class=wiki> //정상적인 사용 @Override public int hashCode(){ return toString().hashCode(); } //스펠링이 틀려 compile error!! @Override public int hasCode(){ //misspelled => method does not override a method from its superclass error!! return toString().hashCode(); }</PRE>

@Deprecated deprecated 주석과 동일하나 부모의 method가 deprecated되면 자식의 method를 사용해도 deprecated로 나온다.

<PRE class=wiki>package com.jeid.tiger;public class AnnotationDeprecateTester { public static void main(String[] args){ DeprecatedClass dep = new DeprecatedTester(); dep.doSomething(10); //deprecated }}class DeprecatedClass { @Deprecated public void doSomething(int ii){ //deprecated System.out.println("This is DeprecatedClass's doSomething(int)"); } public void doSomethingElse(int ii){ System.out.println("This is DeprecatedClass's doSomethingElse(int)"); }}class DeprecatedTester extends DeprecatedClass { @Override public void doSomething(int ii){ System.out.println("This is DeprecatedTester's doSomething(int)"); }}</PRE>

@SuppressWarnings SuppressWarnings에 인자는 String[] type으로 여러개를 배열형태로 쓸수 있다.

<PRE class=wiki>package com.jeid.tiger;import java.util.ArrayList;import java.util.List;public class AnnotationSuppressWarningsTester { @SuppressWarnings({"unchecked", "fallthrough"} ) private static void test1(){ List list = new ArrayList(); list.add("aaaaaa"); } @SuppressWarnings("unchecked") private static void test2(){ List list = new ArrayList(); list.add("aaaaaa"); } //warning이 없는 소스. private static void test3(){ List<String> list = new ArrayList<String>(); list.add("aaaaaa"); }}</PRE>

8.13 Creating Custom Annotation Types #

나만의 annotation을 정의할 수 있는데 키워드는 @interface이 각 method정의가 member라고 보면 된다. 간단한 예를 보면 다음과 같다.

<PRE class=wiki>package com.jeid.tiger;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Documented@Target( { ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation { String columnName(); String methodName() default "";}//사용하는 쪽..public class AnnotationTester { @MyAnnotation(columnName = "test", methodName = "setTest") private String test; @MyAnnotation(columnName = "grpid") public String grpid; ....}//위의 test 멤버의 경우 다음과 같이 접근 가능하다.Field testField = cls.getDeclaredField("test");if (testField.isAnnotationPresent(MyAnnotation.class)) { Annotatioin anno = testField.getAnnotation(MyAnnotation.class); System.out.println(anno.columnName() + ", method = " + anno.methodName());}</PRE>

9 The for/in Statement #

9.1 for/in 의 자주 사용되는 형태 #

for/in은 무엇보다 다양한 유형의 예제를 보는것이 제일 빠를것이다. 형태별 사용 예제를 살펴보면 다음과 같다.

<PRE class=wiki>//1. 가장 단순한 형태인 배열(array)String[] strs = { "aaa", "bbb", "ccc" };for (String str : strs) { System.out.println(str);}//2. List by using IteratorList<Number> lstNum = new LinkedList<Number>();lstNum.add(1);lstNum.add(1.2);for (Iterator<Number> iter = lstNum.iterator(); iter.hasNext();) { Number num = iter.next(); System.out.println("num = " + num);}//3. List를 바로 사용List<String> lst = new LinkedList<String>();lst.add("aaaaa");lst.add("bbbbb");lst.add("ccccc");lst.add("ddddd");for (String str : lst) { System.out.println("str = " + str);}// 4. List of ListList[] lists = { lst, lst };for (List<String> l : lists) { for (String str : l) { System.out.println("str = " + str); }}</PRE>

10 Static Import #

10.1 static member/method import #

Tiger에서는 다른 클래스의 member와 method를 import 할수 있다. 단, static 일 경우만 가능하다.

<PRE class=wiki>//예를 들어 System.out.println() 이라는 것을 사용하기 위해서는 다음의 import 문이 필요하다.import java.lang.System; //물론 java.lang 이기에 import 문이 필요없지만 예를 들자면 그렇다는 것이다.&^^//허나, Tiger에서는 다음과 같이 사용할수 있다.import static java.lang.System.out;...out.println(...);// method를 import 한다면..import static java.lang.System.out.println;...println(...);</PRE>

11 References #


'JAVA' 카테고리의 다른 글

Virtual Word Wrap-자동줄바꿈  (0) 2009.11.24
Oracle JDBC 드라이브 연결  (0) 2009.11.24
clone()메소드  (0) 2009.11.24
equals() 메소드와 오버라이딩 instanceof 사용예제  (0) 2009.11.24
toString사용의 간단한 예제  (0) 2009.11.24
Posted by Tiwaz
JAVA2009. 11. 24. 22:22

clone()메소드 Java

2009/02/09 17:03

작성자: 베레(lsj403)

객체를 복제하는 기능의 메소드. 똑같은 값을 갖는 객체를 하나 더 만들수 있다.!!

사용을 위해서는 Cloneable 인터페이스를 구현하는 클래스만 가능!

*Clone 메소드는 객체 자신을 복제하는 메소드 이므로 리턴하는 값은 메소드를 호출하는 데 사용한 객체와 똑같은 타입의 객체! 리턴 타입이 Object 타입일 경우 리턴하는 객체를 그타입에 맞게 사용하기 위해 캐스트 연산이 필요함!*

 

사용예제.

 

class Name implements Cloneable {  //객체 복사를 위한 인터페이스 구현

     Name() {

     }

     public Object Clone() {  //Cloneable을 구현하기 위한 메소드

          try {

                return super.clone();

          }

          catch (CloneNotSupportedException e) {

                return null;

           }

     }

 

}//

Posted by Tiwaz
JAVA2009. 11. 24. 22:22
equals() 메소드와 오버라이딩 instanceof 사용예제 Java

2009/02/09 16:57

작성자: 베레(lsj403)

객체가 가지고 있는 값을 비교하는데 사용되는 메소드!!

사용법 : obj1.equals(obj2)

두개의 값을 서로 비교하여 true / false를 리턴 한다.

 

예를 들어 GregorianCalendar 의 equals()의 경우 두 객체의 필드 값을 비교

하지만 Object의 equals()의 경우 두 객체의 참조값을 비교한다.

 

Object클래스의 equals로 필드값을 참조 하기 위해서는 오버라이딩이 필요함!!

 

public boolean equals(Object obj) {  //비교하고자하는 객체를 파라미터 선언

     if(!obj instanceof Circle)) // 캐스팅 가능여부 확인.

          return false; // 불가능 하다면 false 반환

     Circle circle = (Circle) obj; //가능할경우 객체를 Circle로 형변환

     if(radius == circle.radius) //두값을 비교하여

          return true;  //같으면 true를 반환

     else

          return false; //틀리면 false 반환

}

 

A(Object) instanceof B(Class, Interface) 형태로 사용.

A위치(Object), B 위치(클래스 나 인터페이스).

 

instanceof 연산자는 A Object를 B 클래스나 인터페이스로

캐스팅이


'JAVA' 카테고리의 다른 글

JAVA - 개발자가 놓치기 쉬운 자바의 개념, 기본원리  (0) 2009.11.24
clone()메소드  (0) 2009.11.24
toString사용의 간단한 예제  (0) 2009.11.24
String->int, int->String  (0) 2009.11.24
클래스 인스턴스화  (0) 2009.11.24
Posted by Tiwaz
JAVA2009. 11. 24. 22:21

toString사용의 간단한 예제 Java

2009/02/09 15:56

작성자: 베레(lsj403)

Object클래스의 toString 메소드는 객체가 가지고 있는 값을 문자열로 만들어서 리턴하는 메소드.

원하는 형태로 출력을 하기 위해서는 toString을 오버라이딩 해줘야 한다.

 

-toString의 간단한 예제-

 

package chap11;

public class JinTest1 {
      JinTest1() {  //클래스의 기본 생성자
           init();  //메소드 호출
      }
      void init() { //메소드 정의
            Jins jin = new Jins("YiSangjin", 26);  //입력받는 데이터로 객체 생성
            String str = jin.toString(); //오버라이드된 toString  메소드 호출
            System.out.println(str); //출력
      }
 
      public static void main(String[] args) {
            new JinTest1();  //클래스 생성
      }//main
}//class

//----------------------

class Jins { //toString을 오버라이드 하는 클래스
      String name;  //문자열 이름 필드
      int age;  //인트형 나이 필드
      Jins(String name, int age) {  //필드 초기화
            this.name = name;
            this.age = age;
      }//생성자
      public String toString() { //toString을 오버라이드 하는 부분.
            String str = "name : "+name+"\n"+"age : "+age;
            return str; //정의된 형태를 반환
      }
}//class

 

Posted by Tiwaz
JAVA2009. 11. 24. 22:20

String->int, int->String 비공개 Java

2009/02/03 20:08

작성자: 베레(lsj403)

String은 int형으로, int형은 String으로

ParseExam.java

Java에서의 형 변환은 빈번히 일어나는 일이다.
그래서 형 변환에 관해서는 필수적으로 알아야 한다.
그 중 가장 많이 일어나는 String->int, int->String에 대해서 살펴보자.
String을 int형으로 바꾸기 위해서는 java.lang팩키지의 Integer클래스에서 parseInt(String s)메소드를 알아야 한다.
  • public static int parseInt(String s) throws NumberFormatException
    • 매개변수로 String클래스의 인스턴스 s를 받아서 숫자로 변환이 가능하면 int형의 값을 반환한다.
    • 만약에 숫자로 변환이 가능하지 않으면 NumberFormatException이라는 RuntimeException을 던지는 메소드이다.
두번째는 int형을 String으로 바꾸는 과정이다. 이것은 java.lang팩키지의 String클래스에서 valueOf(int i)메소드를 알아야한다.
  • public static String valueOf(int i)
    • 매개변수 int형의 변수 i를 받아서 String형의 인스턴스를 반환한다.
    • Integer.toString()에 의해서 반환되는 값과 동일하다.
위의 두 메소드만 알면 String과 int형 사이에서의 형 변환은 쉽게 할 수 있다.
public class ParseExam {
   
    public static void main(String[] args) {
       
        String numStr = "54";
       
        // String값을 int형의 값으로 바꾸는 방법
        int numInt = Integer.parseInt(numStr);
        System.out.println(numInt);
       
        // int형의 값을 String으로 바꾸는 방법
        String numStr2 = String.valueOf(numInt);
        System.out.println(numStr2);
    }

}
Posted by Tiwaz
JAVA2009. 11. 24. 22:19

클래스 인스턴스화 Java

2009/01/22 00:09

작성자: 베레(lsj403)

클래스 --(인스턴스화)--> 객체(인스턴스)

 

가장 간단한 예문.

class Tv { //Tv를 하나 만든다. 

//Tv의 특징을 만든다.
 String color; //색상은 뭘까?검정,은생? (색상필드)
 boolean power; //전원도 있어야함. (전원필드)
 int channel; //채널은 뭘 볼까? (채널필드)

//Tv의 주요 기능은뭘까? 

void power() { //Tv를 보기위한 메소드 선언, 전원 on/off
  power =! power;
 }
 void channelDown() { //다른 채널을 보기 위해서는? 채널을 낮춘다.
  --channel;
 }
 void channelUp() { //한쪽으로 채널바꾸긴 힘들다. 채널을 높인다.
  ++channel;
 }
}//

 

=============================================================

// Tv를 보자~!
class TvTest { //Tv를 볼수있는 클래스를 만든다.
 public static void main(String[] args) {
  Tv t; //Tv는 어디에 있을까~? Tv 인스턴스를 참조하기 위한 변수 t를 선언
  t = new Tv(); //Tv 클레스에서 참조 한다. Tv인스턴스를 생성한다.
  t.channel = 7; //Tv인스턴스의 멤버변수 channel의 값을 7로 한다.
  t.channelDown(); //Tv인스턴스의 메서드 channelDown()의 호출한다.
  System.out.println("현재 채널은 "+t.channel+" 입니다.");
  t.power();
 }//main
}//class
*/

Posted by Tiwaz
JAVA2009. 11. 24. 22:18
Calendar, GregorianCalendar, dateFormat, SimpleDateFormat 을 이용한 시간 출력 Java

2009/01/21 19:41

작성자: 베레(lsj403)

/*
//년 월일 출력 방법.
import java.util.GregorianCalendar; //하위 클레스
import java.util.Calendar; //상위 클레스

class CalendarExample1 {
 public static void main(String[] args) {
  GregorianCalendar calendar = new GregorianCalendar();
  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH); //0~11까지 나오므로 +1을 해야 1월에서 12 표현
  int date = calendar.get(Calendar.DATE);
  int amPm = calendar.get(Calendar.AM_PM); // 9 의 값을 가짐
  int hour = calendar.get(Calendar.HOUR);
  int min = calendar.get(Calendar.MINUTE);
  int sec = calendar.get(Calendar.SECOND);
  String sAmPm = amPm == Calendar.AM ? "오전" : "오후";
  System.out.printf("%d년 %d월 %d일 %s %d시 %d분 %d초",
    year, month, date, sAmPm, hour, min, sec);
  System.out.println();
 }//main
}//class
*/

/*
//타임존 설정 방법 /영국시로 설정
import java.util.*;

class CalendarExample1 {
 public static void main(String[] args) {
  TimeZone timeZone = TimeZone.getTimeZone("Europe/London"); //타임존클래스를 이용한객체 생성 및 런던의 타임존의 시간으로 설정
  
  calendar.setTimeZone(timeZone); //객체 참조 
  [static TimeZone / getTimeZone(String ID)   Gets the TimeZone for the given ID.]

  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH); //0~11까지 나오므로 +1을 해야 1월에서 12 표현
  int date = calendar.get(Calendar.DATE);
  int amPm = calendar.get(Calendar.AM_PM); // 9 의 값을 가짐
  int hour = calendar.get(Calendar.HOUR);
  int min = calendar.get(Calendar.MINUTE);
  int sec = calendar.get(Calendar.SECOND);
  String sAmPm = amPm == Calendar.AM ? "오전" : "오후";
  System.out.printf("%d년 %d월 %d일 %s %d시 %d분 %d초",
    year, month, date, sAmPm, hour, min, sec);
  System.out.println();
 }//main
}//class
*/
/*
//DateFormat 클래스와SimpleDateFormat 클래스는 우리가 원하는 형식으로 출력할때 사용
//게시판에서 많이 사용함.
import java.util.*;
import java.text.*; //SimpleDateFormat 클래스가 속하는 패키지

class DateFormatExample1 {
 public static void main(String[] args) {
  GregorianCalendar calendar = new GregorianCalendar();
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy년 MM월 dd일 aa hh시 mm분 ss초");
  String str = dateFormat.format(calendar.getTime()); //GregorianCalendar 객체를 Date객체로 만들어서 format 메소드에 넘겨 줍니다.
  System.out.println(str);
 }//main
}//class
*/

/*
import java.util.*;
import java.text.*;

class DateFormatExample1 {
 public static void main(String[] args) {
  GregorianCalendar calendar = new GregorianCalendar();
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy년 MM월 dd일 aa hh시 mm분 ss초");
  dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York")); //뉴욕에 해당하는 TimeZone객체를 가지고 SetTimeZone에 메소드를 호출
  String str = dateFormat.format(calendar.getTime()); //GregorianCalendar 객체를 Date객체로 만들어서 format 메소드에 넘겨 줍니다.
  System.out.println(str);
 }//main
}//class
*/


Posted by Tiwaz
JAVA2009. 11. 24. 22:17

String, StringBuilder, StringTokenizer 클래스의 메소드 사용 예제 Java

2009/01/21 19:02

작성자: 베레(lsj403)

/*
//String클래스 length, charAt 메소드 사용예제
class StringExample1 {
 public static void main(String[] args) {
  String str = "자바 커피";
  int len = str.length();  //length() [int / Returns the length of this string.]
  for(int cnt = 0; cnt<len; cnt++) {
   char ch = str.charAt(cnt); //charAt(int index) [char / Returns the char value at the specified index.]
   System.out.println(ch);
  }
 }
}//class 결과 : 자바 커피
*/


//동등 연산자를 이용한문자열 비교 프로그램
/* //리터럴을 이용한 객체 생성
class StringExample2 {
 public static void main(String[] args) {
  String str1 = "자바"; //리터럴 문자를 선언
  String str2 = "자바"; //리터럴 문자를 선언
  if(str1==str2) //리터럴로 선언된 문자열의 경우 S.S(문자열저장소)에서 객체 참조
   System.out.println("같음");
  else
   System.out.println("다름");
 }//main
}//class 결과 : 같음
*/
//생성자를 이용한 객체 생성
/*
class StringExample3 {
 public static void main(String[] args){
  String str1 = new String("자바"); //생성자로 문자를 선언
  String str2 = new String("자바"); //생성자로 문자를 선언
  if(str1 == str2)  //다른 생성자로서 다른 문자열에 생성  (객체 3개= 자바, str1, str2 객체가 생성
   System.out.println("같음");
  else
   System.out.println("다름");
 }//main
}//class 결과 : 다름
*/

/*
//str1.equals(str2)
//String의 equals 는 내용비교
//Object의 equals 는 True or False로 확인

class StringExample4 {
 public static void main(String[] args) {
  String str1 = "자바"; //리터럴로 자바 생성
  String str2 = "자바"; //리터럴로 자바 생성
  if(str1.equals(str2)) //equals 는 같은 문자열의 내용인지 내용 비교 후 True or false 반환
   System.out.println("같음");
  else
   System.out.println("다름");
 }//main;
}//class 결과 : 같음
*/

/*
//str1.equals(str2)
//String의 equals 는 내용비교
//Object의 equals 는 True or False로 확인
class StringExample5 {
 public static void main(String[] args) {
  String str1 = new String("자바"); // 문자열 객체 생성
  String str2 = new String("자바"); //문자열 객체 생성
  if(str1.equals(str2)) //equals 는 같은 문자열의 내용인지 내용 비교 후 True or false 반환
   System.out.println("같음");
  else
   System.out.println("다름");
 }//main
}//class 결과 : 같음
*/

/*
//String 클래스의 substring 메소드 사용
class StringExample6 {
 public static void main(String[] args) {
  String str = "뇌를 자극하는 자바";
  System.out.println(str.substring(3));   // [string / substring(int beginIndex)  Returns a new string that is a substring of this string.]
  System.out.println(str.substring(3,7)); // [string / substring(int beginIndex, int endIndex)  Returns a new string that is a substring of this string.]
 }//main  결과 : 자극하는 자바 (뇌를 )제외한 나머지를 반환
}//class  결과 : 자극하는      (뇌를 )시작부터 3번째까지, 시작부터 7번째까지 사이의 문자열을 반환
*/

/*
//string 문자열 조작 메소드
class StringExample2 {
 public static void main(String[] args) {
  String str1 = "     Let it be.     "; //공백을 포함한 문자 리터럴 선언
  String str2 = str1.trim();  // 공백 제거 메소드 [String / trim() Returns a copy of the string, with leading and trailing whitespace omitted.]

  System.out.println(str2); // Let it be. // str1 변수값의 참조값을 trim 후 출력
  System.out.println(str2.concat("Speaking words of wisdom."));  //Let it be Speaking words of wisdom.
  //문자열 접합 연산자 [String / concat(String str) Concatenates the specified string to the end of this string.]
  System.out.println(str2.toUpperCase()); //LET IT BE.
  //문자열을 대문자로 변환 [string / toUpperCase() Converts all of the characters in this String to upper case using the rules of the default locale.]
  System.out.println(str2.toLowerCase()); //let it be
  //문자열을 소문자로 변환 [string / toLowerCase() Converts all of the characters in this String to lower case using the rules of the default locale.]
  System.out.println(str2.replace('e','a')); //lat it ba.
  //문자열을 다른 문자열로 변환 [string / replace(char oldChar, char newChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.]
  System.out.println(str1); // Let it be.
  System.out.println(str2); //Let it be
 }//main
}//class
*/


//StringBuilder 클래스 생성자
//파라미터 없는 객체 생성시 +16인 버퍼 생성, 파라미터에 버퍼 값을 넣을수 있음.
/*
class StringBuilderExample1 {
 public static void main(String[] args) {
  StringBuilder sb = new StringBuilder("역사를 하노라고 맨땅을 파다가 ");
  System.out.println(sb);
  System.out.println(sb.append("커다란 고인돌을 끄집어 내어놓고 보니"));
  //문자열뒤에 문자열을 붙임 [stringbuilder / append(String str) Appends the specified string to this character sequence.]
  System.out.println(sb.insert(26, "하나"));
  //offset 위치에 문자열 삽입. [StringBuilder / insert(int offset, char c) Inserts the string representation of the char argument into this sequence.]
  System.out.println(sb.delete(21, 23));
  //시작점과 종점사이를 삭제 [StringBuilder / delete(int start, int end) Removes the characters in a substring of this sequence.]
  System.out.println(sb.deleteCharAt(9));
  //인덱스의 위치를 삭제 [StringBuilder  /  deleteCharAt(int index) Removes the char at the specified position in this sequence.]
 }
}//class
*/
/*
//capacity(); 버퍼크기를 리턴하는 메소드
class StringBuilderExample2 {
 public static void main(String[] args) {
  StringBuilder sb = new StringBuilder("푸른 산빛을");
        //sb.ensureCapcity(50); //미리 버퍼 공간을 할당.
  printStringBuilder(sb);
  sb.append("깨치고 적은 길을 걸어서 참어 떨치고 갔습니다.");
  printStringBuilder(sb);
  sb.insert(10, "단풍
  나무 숲을 향하여 난");
  printStringBuilder(sb);
 }//main
 static void printStringBuilder(StringBuilder sb) {
  String str = sb.toString(); //버퍼의 문자열을 string객체로 만들어서 리턴하는 메소드
  //[string / toString() Returns a string representing the data in this sequence.]
  int len = sb.length(); //문자열의 길이를 int로 반환메소드
  //[int / length() Returns the length (character count). ]
  int bufSize = sb.capacity(); //문자열의 크기를 반환하는 메소드
  //[int / capacity()  Returns the current capacity.]
  System.out.printf("%s(%d):: %d %n ", str, len, bufSize);
 }
  }//class
  */

//[void / ensureCapacity(int minimumCapacity) Ensures that the capacity is at least equal to the specified minimum.  ensureCapacity(100);
//버퍼의 크기를 미리 증가 시키는 메소드

/*
//trimToSize() //버퍼 크기를 문자열의 크기에 맞게 맞춤. 불필요한 버퍼는 제거
class StringBuilderExample4 {
 public static void main(String[] args) {
  StringBuilder sb = new StringBuilder(50);
  sb.append("자바");
  System.out.println(sb + ": " + sb.capacity());
  sb.trimToSize();
  // [void / trimToSize()   Attempts to reduce storage used for the character sequence. ]
    System.out.println(sb + ": " + sb.capacity());
 }//main
}//class
*/

/*
//StringTokenizer를 이용한문자열 분리
import java.util.StringTokenizer;
//StringTokenizer : 문자열 분리 클래스
class StringTokenizerExample1 {
 public static void main(String[] args) {
  StringTokenizer stok = new StringTokenizer("사과 복숭아 배");
  while(stok.hasMoreTokens()) {
   //토큰을 가질경우 true, 없을시 false [boolean / hasMoreTokens() Tests if there are more tokens available from this tokenizer's string.]
   String str = stok.nextToken();
   //다음 토큰이 있는지 찾아서 저장 [string / nextToken() Returns the next token from this string tokenizer.]
   System.out.println(str);
  }
 }//main
}//class
*/

/*
import java.util.StringTokenizer;
//StringTokenizer : 문자열 분리 클래스
class StringTokenizerExample1 {
 public static void main(String[] args) {
  StringTokenizer stok = new StringTokenizer("사과,복숭아,배", ","); //구분자 Delim
  while(stok.hasMoreTokens()) {
   //토큰을 가질경우 true, 없을시 false [boolean / hasMoreTokens() Tests if there are more tokens available from this tokenizer's string.]
   String str = stok.nextToken();
   //다음 토큰이 있는지 찾아서 저장 [string / nextToken() Returns the next token from this string tokenizer.]
   System.out.println(str);
  }
 }//main
}//class
*/

/*
import java.util.StringTokenizer;
//StringTokenizer : 문자열 분리 클래스
class StringTokenizerExample1 {
 public static void main(String[] args) {
  StringTokenizer stok = new StringTokenizer("사과=10|복숭아=20|배=30", "=|", true); //true 사용시 구분자까지 구획문자도 포함
  while(stok.hasMoreTokens()) {
   //토큰을 가질경우 true, 없을시 false [boolean / hasMoreTokens() Tests if there are more tokens available from this tokenizer's string.]
   String token = stok.nextToken();
   //다음 토큰이 있는지 찾아서 저장 [string / nextToken() Returns the next token from this string tokenizer.]
   if(token.equals("=")
    System.out.println("\t");
   else if(token.equlas("|"))
    System.out.println("\n");
   else
    System.out.println(token);
  }
 }//main
}//class
*/

Posted by Tiwaz
JAVA2009. 11. 21. 14:09
 
본문스크랩 Java Memory Model Java

2009/01/20 18:05

작성자: 베레(lsj403)

출처 Hurukku's the pleasure | 후루꾸
원문 http://blog.naver.com/inter999/140053719800

Java Memory Model

 

JVM(Java Virtual Machine)

Machine이라는 말이 들어가 있지만 실제로는 Software. Machine이라는 말이 들어간 이유는 JVM 실제적으로 CPU 같은 역할을 하기 때문이다.

Program CPU 위에서 돌아간다. C C++ 같은 프로그램 에서 Compile 코드(native code) CPU에서 바로 실행이 가능하다. 하지만 Java Compile 결과물 “.class” byte code이다. byte code 실행시키기 위한 가상적인 CPU  JVM(Java Virtual Machine) 이다. 이와 같이 JVM 이용하는 이유는 Program 이식성을 높이기 위함이다.

 

JVM Memory Model


1.
   
Method Area

-       JVM 모든 Thread들이 공유하는 데이터 영역

-       Class 정보, Method 정보, 멤버변수, static 변수 저장 영역

 

2.    Heap

-       프로그램 상에서 데이터를 저장하기 위해 동적(실행시간)으로 할당하여 사용하는 영역

-       “new”연산자로 생성된 객체와 배열을 저장

-       주로 실행시간에 생성되는 객체를 저장

-       GC(Garbage Collection)으로 관리 되는 영역

 

3.    Java Stack

-       Method 호출될 때마다 스택 프레임이 생성. 이것이 쌓여 스택을 구성.

-       수행되는 Method 정보, 로컬변수, 매개변수, 연산중 발생하는 임시데이터 저장.

-       JVM 스택영역을 실행중인 Thread 따라 각각 구성

 

4.    Native Method Stacks

-       Native 메소드를 호출할 native Method 매개변수, 지역변수 등을 저장

 

Heap Memory 구조

1.    New Generation

Young Generation이라고도 하며 새로 생성된 객체들을 위해 사용됨.

Eden Space

-        새로(new) 생성된 모든 객체들이 위치한다.

Survivor Space 1

-        Minor GC 의해서 Eden, Survivor 2영역의 객체 활성화된 객체만 이동하여 위치

-        Old Space 이동되기 전의 객체만 위치

Survivor Space 2

-        Minor GC 의해서 Eden, Survivor 1영역의 객체 활성화된 객체만 이동하여 위치

-        Old Space 이동되기 전의 객체만 위치

 

2.    Old Generation

-        Survivor 1, Survivor 2 영역에서 이동해온 객체만 위치

-        Full GC 대상이 되는 객체가 위치

 

3.    Permanent Generation

-        JVM 클래스와 Method 객체를 위한 영역

1.    Minor GC : New(young)영역의 GC방법

Young 영역은 Eden 개의 Survivor 영역으로 구성된다. Minor GC 순서는 다음과 같다.

 

A.     Minor GC 발생하면 Eden Survivor 1영역에 있는 Alive 개체를 Survivor 2영역으로 이동하고 Eden Survivor 1영역을 Clear 한다.

B.     다음 Minor GC 발생하면 Eden Survivor 2영역에 있는 Alive 개체를 Survivor 1영역으로 이동하고 Eden Suvivir 2영역을 Clear 한다.

C.     A,B 반복하다가 Survivor영역에 오래된 개체는 Old영역으로 이동시킨다.

 

Minor GC 같은 방식을 Copy & Scavenge라고 한다. 방법은 매우 속도가 빠르면 작은 크기의 메모리를 Collecting하는데 매우 효과적이다. Minor GC 자주 일어나기 때문에 알고리즘이 적합하다.

 

2.    Full GC : Old 영역의 Garbage Collection 말한다.

Full GC 사용되는 알고리즘은 Mark & Compact라는 알고리즘을 이용한다. 알고리즘은 전체 객체들의 reference 따라가다가 reference 연결되지 않는 객체를 Mark한다. 작업이 끝나면 사용되지 않는 객체를 모두 Mark 되고, Mark 객체를 삭제한다. Full GC 매우 속도가 느리며, Full GC 일어나는 도중에는 순간적으로 자바어플리케이션이 멈춰 버리기 때문에, Full GC 일어나는 정도와 Full GC 소요되는 시간은 어플리케이션의 성능과 안정성에 영향을 준다.

 

 

Java Stack Memory 변화

Test Code

package com.naver.blog.inter999.jvm;

 

public class JavaStackModel {

 

           private static int x=0;

          

           public static void main(String[] args) {

                     // Step 1

                     int a=1;

                     int b=2;

                     int c;

                     // Step 2

                     c = sum(a,b);

                     // Step 4

                     x = c;

                     // Step 5

                     System.out.println(x);

           }

          

           public static int sum(int a, int b) {

                     // Step 3

                     return (a+b);

           }

}

 

Step 1

-        Method Area 영역에JavaStackModel.class” Method, static 변수 저장

-        Java Stack main Method 매개변수 args 생성

 

Step 2

-        main Method 지역변수인 a, b, c 생성

 

Step 3

-        sum Method 호출에 사용된 매개 변수 a, b Java Stack 영역에 생성

 

Step 4

-        sum Method 실행 완료 sum Method 할당된 a, b 변수 제거

-        sum Method return 값을 변수 c 할당.

 

Step 5

-        변수 c 값을 static 변수 x 할당

 

Heap Memory 변화

Test Code

package com.naver.blog.inter999.jvm;

 

public class HeapModel {

           public static String userGroup;

 

           public static void main(String[] args) {

                     //Step 1

                     String name1 = "홍길동";

                     String name2 = "일지매";

                     //Step 2

                     User u1 = new User(name1);

                     User u2 = new User(name2);

                     //Step 3

                     userGroup = u1.getName()+","+u2.getName();

                     //Step 4

           }

}

 

class User {

           private String name;

          

           public User(String name) {

                     this.name = name;

           }

          

           public String getName() {

                     return name;

           }

}

 

 

Step 1

-        Method Area HeapModel, User Class, Static 변수 userGroup 저장된다.

-        Java Stack HeapModel.main Method 매개 변수 args 저장된다.

 

Step 2

-        HeapModel.main 로컬변수 name1, name2 생성 된다.

 

Step 3

-        Heap User Class Instance u1, u2 생성된다.

-        Java Stack Instance u1, u2 변수가 생성된다.

 

Step 4

-        u1, u2 getName Method return 값이 Static 변수 userGroup 할당 된다.

 


Posted by Tiwaz
JAVA2009. 11. 21. 14:02

 

//CastChild.java

class CastChild extends CastParent {
 String name = "CastChild";
 int age = 20;

 CastChild(){
  System.out.println("CastChild타입의 객체 생성됨!");
 }
 CastChild(String name, int age) {
  System.out.println("CastChild타입 객체 생성됨!");
  this.name = name;
  this.age = age;
 }

 void method1(){
  System.out.println("CastChild의 method1()호출됨");
 }
 void method2(){
  System.out.println("CastChild의 method2()호출됨");
 }
 void method3(){
  System.out.println("CastChild의 method3()호출됨");
 }
}

----------------------------------------------------

//CastParent.java
class CastParent {
 String name = "CastParent";
 int age = 50;

 //생성자
 CastParent(){
  System.out.println("CastParent타입의 객체 생성됨!");
 }
 CastParent(String name, int age) {
  System.out.println("CastParent타입의 객체 생성됨!");
  this.name = name;
  this.age = age;
 }

 //메소드 오버라이드
 void method1() {
  System.out.println("CastParent의 method1() 호출됨");
 }

 void method2() {
  System.out.println("CastParent의 method2() 호출됨");
 }
}//class

----------------------------------------------------

//CastMain.java

class CastMain {
 public static void main(String[] args){
  CastParent cp1 = new CastParent(); //부모는 부모 자신을
  System.out.println(cp1.name+":"+cp1.age+"\n");

  CastChild cc1 = new CastChild(); //자식은 자식 자식을
  System.out.println(cc1.name+":"+cc1.age+"\n");


  CastParent cp2 = new CastChild(); //부모(상)는 자식(하)을 묵시적 변환
  System.out.println(cp2.name+":"+cp2.age+"\n");
  
  CastChild cc2 =(CastChild)cp2; //자식타입 cc2에 부모cp를 자식타입으로 변환 후
  System.out.println(cc2.name+":"+cc2.age+"\n");
  
  cp2.method1();
  cp2.method2();
//  cp2.method3();  //CastParent에는 Method3()없음
  
  System.out.println();
  cc2.method1();
  cc2.method2();
  cc2.method3();


  //부모의 method1()과 부모의 method2()호출
  System.out.println();
  CastParent cp3 = new CastParent();
  System.out.println(cp3.name+":"+cp3.age);

  cp3.method1();
  cp3.method2();

  // instanceof
  CastParent cp4 = new CastParent();
  CastChild cc4 = new CastChild();
  CastParent cp5 = cc4;
  if(cp4 instanceof CastParent) {
   System.out.println("41 cp4 is instance of CastParent");
  }
  if(cp4 instanceof CastChild) { //안찍힘.
   System.out.println("44 cp4 is instance of CastChild");
  }
  if(cc4 instanceof CastChild) {
   System.out.println("47 cc4 is instance of CastChild");
  }
  if(cc4 instanceof CastParent) {
   System.out.println("50 cc4 is instance of CastParent");
  }
  if(cp5 instanceof CastParent) {
   System.out.println("53 cp5 is instance of CastParent");
  }
  if(cp5 instanceof CastChild) {
   System.out.println("56 cp5 is instance of CastParent");
  }
  if(cc4 instanceof Object) {
   System.out.println("59 cc4 is instance of Object");
  }
 }
}//class

'JAVA' 카테고리의 다른 글

String, StringBuilder, StringTokenizer 클래스의 메소드 사용 예제  (0) 2009.11.24
Java Memory Model  (0) 2009.11.21
enum 열거 상수  (0) 2009.11.21
실습5 패키지 컴파일 cmd창  (0) 2009.11.21
성적표 출력 실습  (0) 2009.11.21
Posted by Tiwaz
JAVA2009. 11. 21. 14:00
enum 열거 상수 Java

2009/01/20 15:37

작성자: 베레(lsj403)

---------------------------

//Day.java

enum Day {
 MONDAY("월"), TUESDAY("화"), WEDNES("수"), THURSDAY("목"), FRIDAY("금"), SATDAY("토"), SUNDAY("일") ;
 final private String name;
 
 Day(String name){
  this.name = name;
 }
 
 String value() {
  return name;
 }
}

------------------------------------------------------------

//DayExample1.java

class DayExample1 {
 public static void main(String[] args){
  Day[] weeks = Day.values();
  int len = weeks.length;
  for(int i=0;i<len;i++){
   System.out.println(weeks[i].value());
  }
 }//main
}//class


'JAVA' 카테고리의 다른 글

Java Memory Model  (0) 2009.11.21
클레스간 상속 (객체 생성 타입 케스팅 예제)  (0) 2009.11.21
실습5 패키지 컴파일 cmd창  (0) 2009.11.21
성적표 출력 실습  (0) 2009.11.21
점과 점 사이의 거리 출력 실습  (0) 2009.11.21
Posted by Tiwaz
JAVA2009. 11. 21. 13:59
 

//A.java
package a;
public class A{
 public A(){
  System.out.println("A const");
 }
}

----------------------

package a.b;

import a.A;

public class B extends A {
 public B() {
  System.out.println("B constr");
 }
}
-----------------------------

 

package a.b.main;

import a.b.B;

class PackageMain {
 public static void main(String[] args){
  B b=new B();
 }
}

 

--------------------------------

 

##실습 5 - 패키지 컴파일
-패키지와 클래스 생성
/src/a/A
/src/a/b/B
/src/a/b/main/PackageMain.java

-클레스 파일구조

PackageMain
p_test/classes/a/A
p_test/classes/a/b/B
p_test/classes/a/b/main/PackageMain.java

디렉토리 : 실습5/src/a
위치 : src
javac -d ../classes a/A.java
javac -d ../classes a/b/B.java
javac -d ../classes a/b/main/PackageMain.java

java -classpath ../classes a.b.main.PackMain

디렉토리 : 실습5/classes
javac -d ./classes src/a/A.java src/a/b/B.java src/a/b/main/PackageMain.java
java -d ./classes a.b.main.PackageMain


'JAVA' 카테고리의 다른 글

클레스간 상속 (객체 생성 타입 케스팅 예제)  (0) 2009.11.21
enum 열거 상수  (0) 2009.11.21
성적표 출력 실습  (0) 2009.11.21
점과 점 사이의 거리 출력 실습  (0) 2009.11.21
간단한 성적 출력  (0) 2009.11.21
Posted by Tiwaz
JAVA2009. 11. 21. 13:58

성적표 출력 실습 Java

2009/01/18 15:35

작성자: 베레(lsj403)

// 성적을 나타내는 성적클래스  Score.java
class Score {

     int kor; // 국어
     int eng; // 영어
     int math;  // 수학

     Score(int kor, int eng, int math) {
          this.kor = kor;
          this.eng = eng;
          this.math = math;
     }

      // 세 과목의 합계를 연산해서 반환
      int getTotal() {
           return kor+eng+math;
      }

}//class

-----------------------------------------------------------------------------

// 학생을 나타내는 학생클래스  Student.java
class Student {

     String name; // 이름
     Score score; // 성적

     Student() {
     }

     Student(String name, Score score) {
          this.name = name;
          this.score = score;
     }

}//class

------------------------------------------------------------------------------

// 메인클래스 StudentMain.java

import java.util.ArrayList;

 

class StudentMain {

        public static void main(String[] args) {

               // 학생 객체 생성

               /* 방법1

               Student student1

                       = new Student("홍길동", new Score(100,90,80));

               Student student2

                       = new Student("강감찬", new Score(90,80,70));

               Student student3

                       = new Student("이순신", new Score(80,90,80));

               printResult(student1);

               printResult(student2);

               printResult(student3);

               */

 

               /* 방법2

               Student[] sArray = new Student[3];

               for (int i=0; i

                       sArray[i] = new Student();

               }

               sArray[0].name = "홍길동";

               sArray[0].score = new Score(100,90,80);

               sArray[1].name = "강감찬";

               sArray[1].score = new Score(90,80,70);

               sArray[2].name = "이순신";

               sArray[2].score = new Score(80,90,80);

 

               for (int i=0; i

                       printResult(sArray[i]);

               }

               */

 

               /* 방법3 */

               ArrayList list = new ArrayList();

               list.add(new Student("홍길동", new Score(100,90,80)));

               list.add(new Student("강감찬", new Score(90,80,70)));

               list.add(new Student("이순신", new Score(80,90,80)));

 

               for (int i=0; i

                       printResult((Student)list.get(i));

               }

 

        } // main

 

        static void printResult(Student s) {

               //홍길동 학생은 국어 100, 영어 90, 수학 80, 270

               System.out.println(s.name+" 학생은 국어 "

                       +s.score.kor+", 영어 "+s.score.eng+", 수학 "

                       +s.score.math+", "+s.score.getTotal()+"");

        }

} // class

 

'JAVA' 카테고리의 다른 글

enum 열거 상수  (0) 2009.11.21
실습5 패키지 컴파일 cmd창  (0) 2009.11.21
점과 점 사이의 거리 출력 실습  (0) 2009.11.21
간단한 성적 출력  (0) 2009.11.21
Random을 이용한 난수 발생  (0) 2009.11.21
Posted by Tiwaz
JAVA2009. 11. 21. 13:57
점과 점 사이의 거리 출력 실습 Java

2009/01/18 15:30

작성자: 베레(lsj403)

// 점을 나타내는 Point클래스  Point.java

 class Point {

      int x; // 점의 x좌표
      int y; // 점의 y좌표

     

      // 점을 생성하는 생성자
      Point(int x, int y) {
           this.x = x;
           this.y = y;
       }

 

       // 두 점의 거리를 연산하여 반환하는 메소드
       static double distance(Point p1, Point p2) {
            double dist;
            dist = Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)
                  +(p1.y-p2.y)*(p1.y-p2.y));
            return dist;

          /*sqrt(double a)   //루트연산 
          Returns the correctly rounded positive square root of a double value.*/
       }

}//class

 

-----------------------------------------------------------------------------------

// 두 점을 생성해서 거리를 출력하는 메인클래스 PointMain.java
class PointMain {

     public static void main(String[] args) {

    

          // 두 점 생성
          Point p1 = new Point(10, 10);
          Point p2 = new Point(5, 5);

    

          // 두 점의 거리 연산
          double result = Point.distance(p1, p2);

    

          // 출력
          System.out.println("두 점 사이의 거리는 "  + result + "입니다!");

     } // main

} // class


'JAVA' 카테고리의 다른 글

실습5 패키지 컴파일 cmd창  (0) 2009.11.21
성적표 출력 실습  (0) 2009.11.21
간단한 성적 출력  (0) 2009.11.21
Random을 이용한 난수 발생  (0) 2009.11.21
입력을 받아 구구단 출력  (0) 2009.11.21
Posted by Tiwaz
JAVA2009. 11. 21. 13:56
간단한 성적 출력 Java

2009/01/18 15:23

작성자: 베레(lsj403)

class Score {

     public static void main(String[] args){

         int cnt = args.length; 
         int total = 0;
         float avg = 0;

         String[] subject = new String[cnt/2];
         int[] score = new int[cnt/2];

         for (int i=0; i              if (i%2==0) { 
                  subject[i/2] = args[i];
              } else {
                score[i/2] = Integer.parseInt(args[i]);
                total += score[i/2];
              }
         }

         avg = total / (float)cnt * 2;

         System.out.println("================");
         System.out.println("과목\t점수");
         System.out.println("================");
         for (int i=0; i              System.out.println(subject[i]+"\t"+score[i]);
         }
         System.out.println("================");
         System.out.println("총점\t"+total);
         System.out.println("평균\t"+avg);
         System.out.println("================");

     } // main

} // class


'JAVA' 카테고리의 다른 글

성적표 출력 실습  (0) 2009.11.21
점과 점 사이의 거리 출력 실습  (0) 2009.11.21
Random을 이용한 난수 발생  (0) 2009.11.21
입력을 받아 구구단 출력  (0) 2009.11.21
구구단 출력 실습  (0) 2009.11.21
Posted by Tiwaz
JAVA2009. 11. 21. 13:54
Random을 이용한 난수 발생 Java

2009/01/18 15:20

작성자: 베레(lsj403)

import java.util.Random;

 

class RandomTest {

     public static void main(String[] args) {

          Random rd = new Random();

          while (true) {
                //int ran = (int)(Math.random()*45) + 1;
               int ran = rd.nextInt(45)+1;
               System.out.println("=====> "+ran);
               try {
                    Thread.sleep(1000);
               } catch (InterruptedException ie) {
                     ie.printStackTrace();
               }
          }

    }//main

}//class


'JAVA' 카테고리의 다른 글

점과 점 사이의 거리 출력 실습  (0) 2009.11.21
간단한 성적 출력  (0) 2009.11.21
입력을 받아 구구단 출력  (0) 2009.11.21
구구단 출력 실습  (0) 2009.11.21
5가지 별찍기 실습  (0) 2009.11.21
Posted by Tiwaz