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!
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!
2013년 8월 8일 목요일
Operator
Operators | Precedence |
---|---|
postfix | expr++ expr-- |
unary | ++expr --expr +expr -expr ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
Simple Assignment Operator
= Simple assignment operator
Arithmetic Operators
+ Additive operator (also used
for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Unary Operators
+ Unary plus operator; indicates
positive value (numbers are
positive without this, however)
- Unary minus operator; negates
an expression
++ Increment operator; increments
a value by 1
-- Decrement operator; decrements
a value by 1
! Logical complement operator;
inverts the value of a boolean
Equality and Relational Operators
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Conditional Operators
&& Conditional-AND
|| Conditional-OR
?: Ternary (shorthand for
if-then-else statement)
Type Comparison Operator
instanceof Compares an object to
a specified type
Bitwise and Bit Shift Operators
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
Array
int[] anArray;
anArray = new int[10];
anArray[0] = 100;
anArray[1] = 200;
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
int[] anArray = {
100, 200, 300,
400, 500, 600,
700, 800, 900, 1000
};
the rows are allowed to vary in length
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};
length property is built-in.
System.out.println(anArray.length);
System.arraycopy()
public static void arraycopy(Object src, int srcPos,
Object dest, int destPos, int length)
class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
array can be converted to String easily.
new String(copyTo)
java.util.Arrays.copyOfRange
class ArrayCopyOfDemo {
public static void main(String[] args) {
char[] copyFrom = {'d', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd'};
char[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9);
System.out.println(new String(copyTo));
}
}
java.util.Arrays
binarySearch()
equals()
fill()
sort()
parallelSort()
2013년 8월 7일 수요일
Primitive Data Types
( http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html )
byte
short
int
long
float
double
boolean
char
uninitialized field will be set to a default value by the compiler.
but, uninitialized local variable will not be set to a default value by the compiler.
accessing an uninitialized local variable will result in a compile-time error!
// The number 26, in decimal
int decVal = 26;
// The number 26, in hexadecimal
int hexVal = 0x1a;
// The number 26, in binary
int binVal = 0b11010;
null can be used as a value for any reference type.
class literal refers to the object that represents the type itself.
ex) String.class
byte
short
int
long
float
double
boolean
char
uninitialized field will be set to a default value by the compiler.
but, uninitialized local variable will not be set to a default value by the compiler.
accessing an uninitialized local variable will result in a compile-time error!
// The number 26, in decimal
int decVal = 26;
// The number 26, in hexadecimal
int hexVal = 0x1a;
// The number 26, in binary
int binVal = 0b11010;
null can be used as a value for any reference type.
class literal refers to the object that represents the type itself.
ex) String.class
Package
package is a namespace that organizes a set of related classes and interfaces.
conceptually I can think of packages as being similar to different folders.
Java Platform, Standard Edition 7 API Specification
http://docs.oracle.com/javase/7/docs/api/index.html
Interface
interface
interface is a group of related methods with empty bodies.
implements keyword
public keyword to the implemented interface methods.
inheritance
inheritance
superclass
subclass
one direct superclass
unlimited number of subclass
extends keyword
2013년 8월 6일 화요일
Lesson: A Closer Look at the "Hello World!" Application
( http://docs.oracle.com/javase/tutorial/getStarted/application/index.html )
public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
The Java language support three kinds of commnets:
/* text */
/** documentation */
// text
Every application begins with a class definition.
Every application must contain a main method whose signature is:
public static void main(String[] args)
Lesson: The "Hello World!" Application
( http://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html )
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
D:\Java\Work\pp003>java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
D:\Java\Work\pp003>dir
2013-08-06 오전 11:04 <DIR> .
2013-08-06 오전 11:04 <DIR> ..
2013-08-06 오전 11:04 116 HelloWorldApp.java
D:\Java\Work\pp003>javac HelloWorldApp.java
D:\Java\Work\pp003>dir
2013-08-06 오전 11:04 <DIR> .
2013-08-06 오전 11:04 <DIR> ..
2013-08-06 오전 11:04 432 HelloWorldApp.class
2013-08-06 오전 11:04 116 HelloWorldApp.java
D:\Java\Work\pp003>java HelloWorldApp
Hello World!
How will Java technology change my life?
( http://docs.oracle.com/javase/tutorial/getStarted/intro/changemylife.html )
Java can't promise me fame, fortune, or even a job.
Still, it is likely to make my programs better and requires less effort than other languages.
They believe that Java technology will help me do the following:
Get started quickly: because it's easy to learn, especially for programmers already familiar with C or C++, just like me.
Write less code: a program written in Java programming language can be four times smaller than the same program written in C++.
( the life of C++ programmer is four times harder than the life of Java programmer. That's why my life was so hard. )
Write better code: because java language encourages good coding practices.
And because its object orientation and component architecture ( JavaBeans ) and easily extendible API let me reuse existing, tested code.
Develop programs more quickly: because java language is simpler than C++, and as such, my development time could be up to twice as fast.
Avoid platform dependencies:
Write once, run anywhere: ( Really? )
Distribute software more easily: With Java Web Start software. It does automatic version check at startup.
What can Java Technology do?
( http://docs.oracle.com/javase/tutorial/getStarted/intro/cando.html )
Java platform gives me the following feature:
Development Tools:
the javac compiler
the java launcher
the javadoc documentation tools
Application Programming Interface (API)
Deployment Technologies:
Java Web Start software
Java Plug-in software
User Interface Toolkits:
Swing
Java 2D toolkits
Integration Libraries:
Java IDL API
JDBC API
JNDI API
Java RMI
Java RMI-IIOP
About the Java Technology
( http://docs.oracle.com/javase/tutorial/getStarted/intro/definition.html )
.java : plain text files
.class : bytecodes ( the machine language of the Java Virtual Machine )
.class files are capable of running on many different operating systems.
The Java platform has two components.
One is the Java Virtual Machine and the other is Java Application Programming Interface (API)
The Java platform can be a bit slower than native code.
But, Java has great portability.
JVM means a Virtual Machine for the Java platform.
2013년 6월 15일 토요일
피드 구독하기:
글 (Atom)