영어로 읽는 코딩

[자바] 코어 자바내 일부 문장들(자바)

p.42

 

 

 

public class FirstSample

{

     public static void main(String[] args)

     {

              System.out.println("We will not use 'Hello, World');

     }

}

 

It is worth spending all the time you need to become comfortable with the framework of this sample; the pieces will recur in all applications. First and foremost, Java is case sensitive. If made any mistakes in capitalization (such as typing Main instead of main), the program will not run.

Now let’s look at this source code line by line. The keyword public is called an access modifier; these modifiers control the level of access other parts of a program have to this code. We have more to say about access modifiers in Chapter 5. The keyword class reminds you that everything in a Java program lives inside a class. Although we will spend a lot more time on classes in the next chapter, for now think of a class as a container for the program logic that defines the behavior of an application. As mentioned in Chapter 1, classes are the building blocks with which all Java applications and applets are built. Everything in a Java program must be inside a class.

 

p. 79

Now you can use the various methods of the Scanner class to read input. For example, the nextline method reads a line of input.

System.out.print("What is your name? ")

String name = in.nextline();

 

Here, we use the nextline method because the input might contain spaces. To read a single word (delimited by whitespace), call

String firstName = in.next();

To read an integer, use the nextInt method.

System.out.print("How old are you? ");

int age = in.nextInt();

 

 

 

p.87 파일 탐색

To read from a file, construct a scanner object like this:

Scanner in = new Scanner(Paths.get("myfile.txt"), "UTF-8");

If the file name contains backslashes, remember to escape each of them with an additional backslash: “C:\\mydirectory\\myfile.txt”.

 

(p. 88 (Note) 위와 연결됨: 파일 경로 찾기)

When you specify a relative file name, such as “myfile.txt”, “mydirectory/myfile.txt” or “../myfile.txt”, the file is located relative to the directory in which the Java virtual machine was started. If you launched your program from a command shell, by executing

java MyProg

then the starting directory is the current directory of the command shell. However, if you use an integrated development environment, it controls the starting directory. You can find the directory location with this call:


String dir = System.getProperty(“user.dir”);
 

If you run into grief with locating files, consider using absolute path names such as

“c:\\mydirectory\\myfile.txt” or “/home/me/mydirectory/myfile.txt”.

 

p.112 배열 초기화

When you create an array of numbers, all elements are initialized with zero. Arrays of Boolean are initialized with false. Arrays of objects are initialized with the special value null, which indicates that they do not (yet) hold any objects. This can be surprising for beginners. For example,

String[] name = new String[10];

creates an array of ten strings, all of which are null. If you want the array to hold empty strings, you must supply them:


for (int i = 0; i<10 ; i++) names[i] = “ “;

 

To find the number of elements of an array, use array.length. For example:

for (int I = 0; I < a.length; i++)
System.our.println(a[i]);
 

Once you create an array, you cannot change its size (although you can, of course, change an individual array element). If you frequently need to expand the size of an array while your program is running, you should use a different data structure called an array list.

 

 

p.113 배열의 출력과 향상된 for

Java has a powerful looping construct that allows you to loop through each element in an array (or any other collection of elements) without having to fuss with index values.

The enhanced for loop

for(variable : collection) statement

sets the given variable to each element of the collection and then executes the statement (which, of course, may be a block). The collection expression must be an array or an object of a class that implements the Iterable interface, such as ArrayList.

For example,

for(int element : a)
System.out.println(element);
 

prints each element of the array a on a separate line.

 

(p.114)

There is an even easier way to print all values of an array, using the toString method of the Arrays class. The call Arrays.toString(a) returns a string containing the array elements, enclosed in brackets and separated by commans, such as “[2, 3, 5, 7, 11, 13]”. To print the array, simply call


System.out.println(Arrays.toString(a));

 

(p.122)

To print out a quick-and-dirty list of the elements of a two-dimensional array, call

System.out.println(Arrays.deeptoString(a));

The output is formatted like this;

[[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 12], [4, 15, 14, 1]]

 [Core Java]

댓글

댓글 본문
작성자
비밀번호
버전 관리
Yoo Moon Il
현재 버전
선택 버전
graphittie 자세히 보기