[Data Structures & Algorithms in Java 6th] Ch.1 - Java Prmier

2021. 4. 29. 18:59개발공부/자료구조 알고리즘

자료구조, 알고리즘 교재를 공부하며 정리한 내용입니다. 원서 특성 상 원문을 최대한 살렸습니다.

교재 정보는 글 제목에 있는 책 제목을 구글에 검색하시면 바로 나옵니다. 자세한 정보는 글 맨 아래 [출처]를 확인하시면 됩니다.

챕터 1은 전체를 다루지 못했고 바로 [챕터 8 Tree]로 넘어가 자료구조 공부를 선행하기로 순서를 변경했습니다.


1.2 Classes and Objects

- Identifier is the name of class, method or variables in Java.

 

- Method is a block of codes that can be called to perform.

  . accessor method : returns information to caller without changing any fields.

  . update method : returns information to caller changing one or more fields.

 

- Classes are known as reference type and variable type is reference variable.

  . reference variable can store location of object from declared class.

 

- Abstract methods included more than one class should declared as abstract.

  . permissive to have non abstract method.

 

- Final modifier is typically declared as static as well. It is wasteful for every instance to store identical value then that value can be shared by the entire class.

 

 - Return type of Java must be single. To return multiple types, we have to use compound object. Whose instance variables include all values we want to return.

 

- Parameters in Java is passed by value, any time we pass parameter to a method it creates a copy of parameter. We can use copy of parameter value in method body.

  . demonstration

1
2
3
4
5
6
7
public static void badReset(Counter c) {
    c = new Counter();
}
 
public static void goodReset(Counter c) {
    c.reset();
}
cs

 . badReset does not resets reference instance of parameter but since goodReset parameter has same reference instance with variable c , it resets parameter.

 

- this refers as a reference to the instance upon the method was invoked.

  . thing.foo(a, b, c) this will be the instance thing

  . this can allow one constructor body to invoke another constructor body.

  . demonstration

1
2
3
public Counter() {
 this.(0); //invoke one-parameter constructor with value zero 
}
cs

 

- The main method is an execution method(self-standing class)in Java. It has parameter String[] args, we can optionally add arguments in command line. This is an example of execute Aquarium class in command line with optional 45 argument, which refers to number of fish. java Aquarium 45


1.3 Strings, Wrappers, Arrays, and Enum Types

- String is a class that its instances are immutable, when concatenating the operation creates new string instance. For long string this can be very time consuming.

 

- StringBuilder class is effectively a mutable version of a string.

 

- Wrapper class is for Java libraries that only works with object types. Integer

  . Automatic boxing and unboxing is done with base types (int) and wrapper types (Integer)

 

- Enum types are only allowed to take on values that come from a specified set of names.

  . modifier enum name {valueName0, valueName1, ...}; valueName should be capitalized. 

1
2
3
4
5
6
public enum Day {MON, TUE, WED, THU, FRI, SAT, SUN};
//type definition for days of the week
 
Day today; //Day becomes an official type and we may declare variables or parameters with type Day.
 
today = Day.TUE; //assignment of a value
cs

 

💡why use enum in Java

  . Increase compile-check time and avoid error from passing invalid constants.

  . It is type-safe. You can not assign any value other than specified in Enum Constants.


1.4 Expressions

- Increment and Decrement Operator

  . int i = 8; int j = i++;  -> j becomes 8 then i becomes 9

 

- Compound Assignment Operators

  . a[5] = 10 j = 5 a[j++] += 2 -> a[5] is 12 and j becomes 6

  . Since the expression a[j++]is evaluated only once.


출처 : Data Structures and Algorithms in Java, 6th Edition 6th Edition (Michael T. Goodrich, Wiley)