Lesson 3. Strings and Packages

이번 장에서 배울 내용:

  • String 클래스에 대해 좀더 알아보고
  • Java에서 문자들을 어떻게 다루는지
  • 플랫폼 독립적인 코드를 확인하기 위해 시스템 속성 사용하기
  • 문자열을 동적으로 만들기 위해 StringBuffer 객체 사용하기
  • collection에 있는 각각의 객체들 순회하기
  • System.out 사용해서 출력하기
  • class들을 패키지로 조직화 하기
  • public과 private 접근 지시자 이해하기

Characters and Strings

While Strings are objects, they are made up of sequences of individual characters. Java represents a character as a primitive type known as char. Since a char value is of a primitive type (like an int value), remember that you cannot send messages to it.

Java includes a char type that represents letters, digits, punctuation marks, diacriticals, and other special characters.

Java uses two bytes to store each character.

Special Characters

'\r' '\n' '\t' '\f' '\b' '\'' '\\' '\"'

Strings

A String object represents a sequence of char values of fixed length.

You can construct Strings in a number of ways. Any time you create a new String literal, the Java VM constructs a String object behind the scenes. Here are two ways to construct a String object and assign it to a reference variable:

[CODE]

String a = "abc";

String b = new String("abc"); // DON'T DO THIS

[/CODE]

Since strings are sequences of characters, they can embed special characters.

String z = "\t\n";

you will notice that there are no methods that change a string. You cannot alter the length of a string, nor can you alter any of the characters contained within a string. String objects are thus immutable. If you want to perform any manipulations on a string, you must create a new string.

System Properties

The System class contains a method named getProperty that takes a system property key (a String) as a parameter and returns the system property value associated with the key. The Java VM sets several system properties upon startup. Many of these properties return information about the VM and execution environment. The API documentation method detail for getProperties shows the list of available properties.

getProperties

public static Properties getProperties()
Determines the current system properties.

First, if there is a security manager, its checkPropertiesAccess method is called with no arguments. This may result in a security exception.

The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties always includes values for the following keys:

Key Description of Associated Value
java.version Java Runtime Environment version
java.vendor Java Runtime Environment vendor
java.vendor.url Java vendor URL
java.home Java installation directory
java.vm.specification.version Java Virtual Machine specification version
java.vm.specification.vendor Java Virtual Machine specification vendor
java.vm.specification.name Java Virtual Machine specification name
java.vm.version Java Virtual Machine implementation version
java.vm.vendor Java Virtual Machine implementation vendor
java.vm.name Java Virtual Machine implementation name
java.specification.version Java Runtime Environment specification version
java.specification.vendor Java Runtime Environment specification vendor
java.specification.name Java Runtime Environment specification name
java.class.version Java class format version number
java.class.path Java class path
java.library.path List of paths to search when loading libraries
java.io.tmpdir Default temp file path
java.compiler Name of JIT compiler to use
java.ext.dirs Path of extension directory or directories
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
user.dir User's current working directory

Multiple paths in a system property value are separated by the path separator character of the platform.

Note that even if the security manager does not permit the getProperties operation, it may choose to permit the getProperty(String) operation.

Returns:
the system properties

Throws:
SecurityException - if a security manager exists and its checkPropertiesAccess method doesn't allow access to the system properties.

See Also:
setProperties(java.util.Properties), SecurityException, SecurityManager.checkPropertiesAccess(), Properties

Looping through Collections

What you would like to be able to do is to execute the same three lines of code for each of the students in the ArrayList, regardless of how many students the ArrayList contains. There are several ways of doing this in Java. The most straightforward means in J2SE 5.0 is to use a for-each loop.

[CODE]

for (Student student: students) {

   // ... statements here ...

}

[/CODE]

Single-Responsibility Principle
One of the most basic design principles in object-oriented programming is that a class should do one thing and do it well. By virtue of doing this one thing, the class should have only one reason to change. This is known as the Single-Responsibility Principle.

System.out

Java provides output facilities to allow you to redirect information to the console, to files, and to other destinations. You will learn about these output facilities in depth in Lesson 11.

You will see that out is a static variable, of the type PrintStream, that represents the standard output stream, also known as stdout or simply "the console." You can directly access this console object using the following static variable reference: System.out

Access Modifier

The public and private keywords are known as access modifiers. You use access modifiers to control access to Java elements, including fields, methods, and classes. The access modifiers that are appropriate for a class are different than those that are appropriate for methods and fields.

By declaring a class as public, you allow classes in other packages to be able to import and refer directly to the class.

Using Ant

Ant is a platform-independent tool that allows you to create specifications of how your project should be built and deployed.

Other alternatives are to build a shell script or batch file as I demonstrated in Lesson 1. You can also use one of a number of available make tools. A make tool is a build tool very similar to Ant, but most make tools are very tightly bound to a specific operating system. Few make tools provide the ease of building Java applications that Ant does. Ant is the most effective way of doing builds in Java.