Lesson 2. Java Basics

이번장에서 배우게 될 내용은:

  • 학생수를 세기 위한 int 형 변수 사용하기
  • 여러 학생을 저장하기 위한 ArrayList 사용하기
  • 기본 생성자 이해하기
  • ArrayList를 어떻게 사용하는지 이해하기 위해 J2SE API를 어떻게 사용하는지 배우기
  • Student 객체만을 담기 위해 ArrayList를 제약하기
  • 여러 class를 test하기 위한 TestSuite 만들기
  • 패키지를 이해하고 import문을 배우기
  • 상수를 정의하는 방법과 사용
  • 시스템 라이브러리의 date와 calandar 클래스 사용하기
  • Java가 허용하는 다양한 주석문 배우기
  • javadoc을 사용하여 API 문서 생성하기

int

The int type allows variables to be created that store integer values from -2,147,483,648 to 2,147,483,647.

Numbers in Java are not objects like String literals are. You cannot send messages to numbers, although numbers can be passed as parameters along with messages just like Strings can. Basic arithmetic support in Java is provided syntactically; for many other operations, support is provided by system libraries. You will learn about similar non-object types later in Agile Java. As a whole, these non-object types are known as primitive types.

Initialization

you have two ways to initialize fields: You can initialize at the field level or you can initialize in a constructor.

There is no hard-and-fast rule about where to initialize. I prefer initializing at the field level when possiblehaving the initialization and declaration in one place makes it easier to follow the code. Also, as you will learn later in this lesson, you can have more than one constructor; initializing at the field level saves you from duplicate initialization code in each constructor.

Default Constructor

If you do not define any constructors in a class, Java provides a default, no-argument constructor.

The use of default constructors also implies that Java views constructors as essential elements to a class. A constructor is required in order for Java to initialize a class, even if the constructor contains no additional initialization code. If you don't supply a constructor, the Java compiler puts it there for you.

Suites

여러 클래스들을 한꺼번에 테스트하고 싶을 때 다음과 같이 suite을 생성하여 추가합니다.


위 그림은 JUnit 3.8 대의 사용 법.
아래 그림은 JUnit 4.1에서 annotation을 사용 하는 방법.

The SDK and java.util.ArrayList

java.util.ArrayList<Student> allStudents = session.getAllStudents();

A type appearing in this forma class name followed by a parameter type within angle brackets (< and >) is known as a parameterized type. In the example, the parameter Student of java.util.ArrayList indicates that the java.util.ArrayList is bound such that it can only contain Student objects.

The class java.util.ArrayList is one of thousands available as part of the Java SDK class library.

왼쪽 상당은 패키지, 왼쪽 하단은 해당 패키지에 속한 클래스들 오른쪽은 하나의 클래스에 대한 설명을 보여주는 3단 구조로 되어있다.

Objects In Memory


사실 "Cain DiVoe" 같은 문자열도 메모리 어딘가를 참조하는 변수 일 텐데 그것은 그림에 빠져있다.

Packages and the import Statement

Packages provide a way for developers to group related classes. They can serve several needs: First, grouping classes into packages can make it considerably easier on developers, saving them from having to navigate dozens, hundreds, or even thousands of classes at a time. Second, classes can be grouped into packages for distribution purposes, perhaps to allow easier reuse of modules or subsystems of code.

Third, packagesprovide namespaces in Java. 서로 다른 패키지 안에 있다면 클래스의 이름이 같아도 상관없다.

Typing java.util.ArrayList throughout code can begin to get tedious, and it clutters the code as well. Java provides a keyword import that allows identification of fully qualified class names and/or packages at the source file level. Use of import statements allows you to specify simple class names throughout the remainder of the source file.

The java.lang Package

The Java library contains classes so fundamental to Java programming that they are needed in many, if not all, classes. The classes String and Object are two such classes. The ubiquitous nature of these classes is such that the designers of Java wanted to save you from the nuisance of having to specify an import statement for them everywhere.