1. 构造器
2. 方法重载
5:f1(int)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
char:f1(char)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
byte:f1(byte)f2(byte)f3(short)f4(int)f5(long)f6(float)f7(double)
short:f1(short)f2(short)f3(short)f4(int)f5(long)f6(float)f7(double)
int:f1(int)f2(int)f3(int)f4(int)f5(long)f6(float)f7(double)
long:f1(long)f2(long)f3(long)f4(long)f5(long)f6(float)f7(double)
double:f1(double)f2(double)f3(double)f4(double)f5(double)f6(double)f7(double)
package net.mindview.initialization;
import static net.mindview.util.Print.*;
public class PrimitiveOverloading {
void f1(char x) {print("f1(char)");}
void f1(byte x) {print("f1(byte)");}
void f1(short x) {print("f1(short)");}
void f1(int x) {print("f1(int)");}
void f1(long x) {print("f1(long)");}
void f1(float x) {print("f1(float)");}
void f1(double x) {print("f1(double)");}
void f2(byte x) {print("f2(byte)");}
void f2(short x) {print("f2(short)");}
void f2(int x) {print("f2(int)");}
void f2(long x) {print("f2(long)");}
void f2(float x) {print("f2(float)");}
void f2(double x) {print("f2(double)");}
void f3(short x) {print("f3(short)");}
void f3(int x) {print("f3(int)");}
void f3(long x) {print("f3(long)");}
void f3(float x) {print("f3(float)");}
void f3(double x) {print("f3(double)");}
void f4(int x) {print("f4(int)");}
void f4(long x) {print("f4(long)");}
void f4(float x) {print("f4(float)");}
void f4(double x) {print("f4(double)");}
void f5(long x) {print("f5(long)");}
void f5(float x) {print("f5(float)");}
void f5(double x) {print("f5(double)");}
void f6(float x) {print("f6(float)");}
void f6(double x) {print("f6(double)");}
void f7(double x) {print("f7(double)");}
/**测试常量*/
void testConstVal(){
print("5:");
f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);println("");
}
/**测试char*/
void testChar(){
char x = 'x';
print("char:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);println("");
}
/**测试byte*/
void testByte(){
byte x = 0;
print("byte:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);println("");
}
/**测试Short*/
void testShort(){
short x = 0;
print("short:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);println("");
}
/**测试Int*/
void testInt(){
int x = 0;
print("int:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);println("");
}
/**测试long*/
void testLong(){
long x = 0;
print("long:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);println("");
}
/**测试double*/
void testDouble(){
double x = 0;
print("double:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);println("");
}
public static void main(String[] args) {
PrimitiveOverloading p = new PrimitiveOverloading();
p.testConstVal();
p.testChar();
p.testByte();
p.testShort();
p.testInt();
p.testLong();
p.testDouble();
}
}
f();
void f(){};
int f(){return 1};
4. this关键字
public class Leaf {
int i = 0;
Leaf increment() {
i++;
return this;
}
}
5. 垃圾回收器
package net.mindview.initialization;
class Book{
boolean checkedOut = false;
Book(boolean checkedOut){
this.checkedOut = checkedOut;
}
void checkIn(){
checkedOut = false;
}
protected void finzlized() throws Throwable {
if(checkedOut){
System.out.println("有书被借走但未归还 ");
//基类版本的finalize()也要做一些重要的事, 应使用super来调用.
super.finalize();
}
}
}
public class TerminationCondition {
public static void main(String[] args) {
Book novel = new Book(true);
//合理的清理
novel.checkIn();
//书被借出后, 没有归还
new Book(true);
//强制垃圾回收并且执行finalized方法
System.gc();
}
}
6. 成员初始化
7.构造器初始化
public class Test14 {
public static String str1 = "abc";
public static String str2;
public String str3;
static {
str2 = "def";
//str3 = "aaa"; //报错
}
}
package net.mindview.initialization;
public class Spoon {
static int i;
static {
i = 47;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
package net.mindview.initialization;
public class Test14 {
public static String str1 = "abc";
public static String str2;
public String str3;
//静态成员初始化
static {
str2 = "def";
//str3 = "aaa"; //报错
}
//非静态成员初始化
{
str3 = "aaa";
str2 = "mmm";//也可以给静态成员初始化
}
public static void print(){
System.out.println("str1 :"+ str1);
System.out.println("str2 :"+ str2);
}
public static void main(String[] args) {
Test14 t = new Test14();
print();
System.out.println("str3 :"+ t.str3);
}
}
8. 可变参数列表
package net.mindview.initialization;
public class NewVarArgs {
static void printArray(Object ... args){
for(Object arg: args) {
System.out.print(arg + "");
}
System.out.println("");
}
public static void main(String[] args) {
printArray(new Integer(47), new Float(3.14), new Double(2.333));
printArray((Object[])new Integer[]{1,2,3,4});
printArray();
}
}
9. 枚举类型
package net.mindview.initialization;
enum Spiciness {
NOT,MILD,MEDIUM,HOT,FALMING
}
public class SimpleEnumUse {
public static void main(String[] args) {
Spiciness falming = Spiciness.FALMING;
System.out.println(falming);
}
}
package net.mindview.initialization;
enum Spiciness {
NOT,MILD,MEDIUM,HOT,FALMING
}
public class SimpleEnumUse {
public static void main(String[] args) {
Burrito plain = new Burrito(Spiciness.HOT);
Burrito greenChile = new Burrito(Spiciness.MEDIUM);
Burrito jalapeno = new Burrito(Spiciness.NOT);
plain.describe();
greenChile.describe();
jalapeno.describe();
}
}
class Burrito {
Spiciness degree;
public Burrito(Spiciness degree){
this.degree = degree;
}
public void describe(){
switch(degree){
case NOT:
System.out.println("not");
break;
case MILD:
case MEDIUM:
System.out.println("medium");
break;
case HOT:
System.out.println("hot");
break;
case FALMING:
default:
System.out.println("falming");
break;
}
}
}
package net.mindview.initialization;
enum Spiciness {
NOT,MILD,MEDIUM,HOT,FALMING
}
public class SimpleEnumUse {
public static void main(String[] args) {
for(Spiciness s: Spiciness.values()){
System.out.println(s + ", " + s.ordinal());
}
}
}