If you put multiple types in a single source file, only one can be public, and it must have the same name as the source file.
You can include non-public types in the same file as a public type, but only the public type will be accessible from outside of the package. All the top-level, non-public types will be package private.
To include non-public types in the same file as a public type is strongly discouraged, unless the non-public types are small and closely related to the public type.
Only one type in one source file!
기록 보관소 JAVA
JAVA language tutorial.
2013년 8월 12일 월요일
Restrictions on Generics
Java generics has following restrictions:
Cannot Instantiate Generic Types with Primitive Types
Cannot Create Instances of Type Parameters
Cannot Declare Static Fields Whose Types are Type Parameters
Cannot Use Casts or instanceof With Parameterized Types
Cannot Create Arrays of Parameterized Types
Cannot Create, Catch, or Throw Objects of Parameterized Types
Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
Wildcards
Upper Bounded Wildcards
public static void process( List<? extends Foo> list ) { /* ... */ }
Unbounded Wildcards
public static void printList(List<Object> list) {
for (Object elem : list)
System.out.println(elem + " ");
System.out.println();
}
public static void printList( List<?> list ) {
for (Object elem: list)
System.out.print(elem + " ");
System.out.println();
}
The goal of printList is to print a list of any type, but it fails to achieve that goal —
it prints only a list of Object instances;
it cannot print List<Integer>, List<String>, List<Double>, and so on,
because they are not subtypes of List<Object>.
To write a generic printList method, use List<?>
List<?>. This is called a list of unknown type.
Lower Bounded Wildcards
public static void addNumbers( List<? super Integer> list) {
for (int i = 1; i <= 10; i++) {
list.add(i);
}
}
2013년 8월 10일 토요일
Interface
Generally speaking, interfaces are contracts.
// 어떤 약속.
// 어떤 계약.
public interface Pair<K, V> {
public K getKey();
public V getValue();
public void setKey( K key);
public void setValue( V value);
}
interface 는 어떤 기능을 가지고 있는가를 서술하는 용도로 사용할 수 있다.
interface 에 Generic 을 더하면, Type 을 지정하는 효과를 낼수 있다.
기능 존재여부 검사는 순수 interface 로 표현하기로 한다.
기능존재여부와 Type 검사기능을 분리하면, 더 생산적인 결과를 얻을 수 있다고 본다.
이상세계가 아닌 현실세계에서 과연 분리하는것이 가능할까.
쉽지 않을것이다.
operator overloading
Java doesn't offer operator overloading!
So, We can't write code like this;
Complex a, b, c;
a = b + c;
Instead, We must write code like this;
Complex a, b, c;
a = b.add(c);
Surprise!
피드 구독하기:
글 (Atom)