1.涉及到基本类型的重载
public class PrimitiveOverloading {
void f1(char x) { printnb("f1(char) "); }
void f1(byte x) { printnb("f1(byte) "); }
void f1(short x) { printnb("f1(short) "); }
void f1(int x) { printnb("f1(int) "); }
void f1(long x) { printnb("f1(long) "); }
void f1(float x) { printnb("f1(float) "); }
void f1(double x) { printnb("f1(double) "); }
void f2(byte x) { printnb("f2(byte) "); }
void f2(short x) { printnb("f2(short) "); }
void f2(int x) { printnb("f2(int) "); }
void f2(long x) { printnb("f2(long) "); }
void f2(float x) { printnb("f2(float) "); }
void f2(double x) { printnb("f2(double) "); }
void f3(short x) { printnb("f3(short) "); }
void f3(int x) { printnb("f3(int) "); }
void f3(long x) { printnb("f3(long) "); }
void f3(float x) { printnb("f3(float) "); }
void f3(double x) { printnb("f3(double) "); }
void f4(int x) { printnb("f4(int) "); }
void f4(long x) { printnb("f4(long) "); }
void f4(float x) { printnb("f4(float) "); }
void f4(double x) { printnb("f4(double) "); }
void f5(long x) { printnb("f5(long) "); }
void f5(float x) { printnb("f5(float) "); }
void f5(double x) { printnb("f5(double) "); }
void f6(float x) { printnb("f6(float) "); }
void f6(double x) { printnb("f6(double) "); }
void f7(double x) { printnb("f7(double) "); }
void testConstVal() {
printnb("5: ");
f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5); print();
}
void testChar() {
char x = 'x';
printnb("char: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
}
void testByte() {
byte x = 0;
printnb("byte: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
}
void testShort() {
short x = 0;
printnb("short: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
}
void testInt() {
int x = 0;
printnb("int: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
}
void testLong() {
long x = 0;
printnb("long: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
}
void testFloat() {
float x = 0;
printnb("float: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
}
void testDouble() {
double x = 0;
printnb("double: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); print();
}
public static void main(String[] args) {
PrimitiveOverloading p =
new PrimitiveOverloading();
p.testConstVal();
p.testChar();
p.testByte();
p.testShort();
p.testInt();
p.testLong();
p.testFloat();
p.testDouble();
}
} /* Output:
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)
float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7(double)
double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double)
*///:~
public class Demotion {
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(char x) { print("f2(char)"); }
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 f3(char x) { print("f3(char)"); }
void f3(byte x) { print("f3(byte)"); }
void f3(short x) { print("f3(short)"); }
void f3(int x) { print("f3(int)"); }
void f3(long x) { print("f3(long)"); }
void f4(char x) { print("f4(char)"); }
void f4(byte x) { print("f4(byte)"); }
void f4(short x) { print("f4(short)"); }
void f4(int x) { print("f4(int)"); }
void f5(char x) { print("f5(char)"); }
void f5(byte x) { print("f5(byte)"); }
void f5(short x) { print("f5(short)"); }
void f6(char x) { print("f6(char)"); }
void f6(byte x) { print("f6(byte)"); }
void f7(char x) { print("f7(char)"); }
void testDouble() {
double x = 0;
print("double argument:");
f1(x);f2((float)x);f3((long)x);f4((int)x);
f5((short)x);f6((byte)x);f7((char)x);
}
public static void main(String[] args) {
Demotion p = new Demotion();
p.testDouble();
}
} /* Output:
double argument:
f1(double)
f2(float)
f3(long)
f4(int)
f5(short)
f6(byte)
f7(char)
*///:~
2.初始化顺序
</pre><pre name="code" class="java">package test;
class Window {
Window(int marker) { System.out.println("Window(" + marker + ")"); }
}
class House {
Window w1 = new Window(1); // Before constructor
House() {
// Show that we're in the constructor:
System.out.println("House()");
w3 = new Window(33); // Reinitialize w3
}
Window w2 = new Window(2); // After constructor
void f() { System.out.println("f()"); }
Window w3 = new Window(3); // At end
}
public class Test {
public static void main(String[] args) {
System.out.println("conhhhh");
House h = new House();
h.f(); // Shows that construction is done
}
}
输出结果:
conhhhh
Window(1)
Window(2)
Window(3)
House()
Window(33)
f()
Window(2)
Window(3)
House()
Window(33)
f()
区别:
package test;
class Window {
Window(int marker) { System.out.println("Window(" + marker + ")"); }
}
class House {
//Window w1 = new Window(1); // Before constructor
House() {
// Show that we're in the constructor:
System.out.println("House()");
Window w3 = new Window(33); // Reinitialize w3
}
//Window w2 = new Window(2); // After constructor
void f() { System.out.println("f()"); }
//Window w3 = new Window(3); // At end
}
public class Test {
public static void main(String[] args) {
System.out.println("conhhhh");
House h = new House();
h.f(); // Shows that construction is done
}
}
conhhhh
House()
Window(33)
f()
House()
Window(33)
f()
3.静态数据的初始化
package test;
class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f1(int marker) {
System.out.println("f1(" + marker + ")");
}
}
class Table {
static Bowl bowl1 = new Bowl(1);
Table() {
System.out.println("Table()");
bowl2.f1(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard {
// 非静态后续还有一次初始化,静态对象只初始化一次
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
bowl4.f1(2);
}
static Bowl bowl6 = new Bowl(6);
void f3(int marker) {
System.out.println("f3(" + marker + ")");
}
static Bowl bowl5 = new Bowl(5);
}
public class Test {
public static void main(String[] args) {
/* 没有static之后,其与System.out.println是有先后的顺序
*
* Table table = new Table();
Cupboard cupboard = new Cupboard();*/
System.out.println("Creating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
/*如果在类中加入static类初始化,那么main()函数里面的内容加载就要在其后面;否则,先
加载main()里面的内容
* */
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
输出:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(6)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(6)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
4.显示的静态初始化
java允许将许多个静态初始化动作组织层一个特殊的“静态子句”(“静态块”)
package test;
class Cup {
Cup(int marker) {
System.out.println("Cup(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Cups {
// 静态块(或称静态句子)
static Cup cup1;
static Cup cup2;
static {
cup1 = new Cup(1);
cup2 = new Cup(2);
}
Cups() {
System.out.println("Cups()");
}
}
public class Test {
public static void main(String[] args) {
System.out.println("Inside main()");
Cups.cup1.f(99); // (1)
}
/*static Cups cups1 = new Cups(); // (2)
static Cups cups2 = new Cups(); // (2)
*/ /* output:
* Cup(1)
Cup(2)
Cups()
Cups()
Inside main()*/
}
5.非静态实例初始化
package test;
class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
public class Test {
Mug mug1;
Mug mug2;
{
mug1 = new Mug(1);
mug2 = new Mug(2);
System.out.println("mug1 & mug2 initialized");
}
Test() {
System.out.println("Test()");
}
Test(int i) {
System.out.println("Test(int)");
}
public static void main(String[] args) {
System.out.println("Inside main()");
new Test();
System.out.println("new Test() completed");
new Test(1);
System.out.println("new Test(1) completed");
}
}
输出:
Inside main()
Mug(1)
Mug(2)
mug1 & mug2 initialized
Test()
new Test() completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Test(int)
new Test(1) completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Test()
new Test() completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Test(int)
new Test(1) completed
与静态初始化例子,相差一个static区别。这就导致其非静态成员变量每次都需要进行初始化(与匿名内部类相关)
数组初始化
package test;
import java.util.*;
public class Test {
public static void main(String[] args) {
Random rand = new Random(44);
Integer[] a = new Integer[rand.nextInt(10)];
System.out.println("length of a = " + a.length);
for(int i = 0; i < a.length; i++)
a[i] = rand.nextInt(500); // Autoboxing
System.out.println(Arrays.toString(a));
}
}
6.可变参数列表
在javaSE5之前,可变参数列表程序(Varargs)如下:
<pre name="code" class="java">package test;
class A {}
public class Test {
static void printArray(Object[] args) {
for(Object obj : args)
System.out.print(obj + " ");
System.out.println();
}
public static void main(String[] args) {
// 最后有没有逗号都是可选,这是维护长列表变得容易的一种特性
printArray(new Object[]{new Integer(47), new Float(3.14), new Double(11.11),});
// 第二种初始化对象数组的方式:
Object [] b={new Integer(47), new Float(3.14), new Double(11.11),};
printArray(b);
printArray(new Object[]{"one", "two", "three" });
printArray(new Object[]{new A(), new A(), new A()});
// test.A@6dc57a92
// 没有重新父类toString()方法,因此默认输出包.类名@十六进制地址
}
}
在新版的javaSE5之后的代码编写:
package test;
class A{}
public class Test {
static void printArray(Object... args) {
for(Object obj : args)
System.out.print(obj + " ");
System.out.println();
}
public static void main(String[] args) {
// Can take individual elements:没有必要在显示编写数组语法,编译自动完成数组填充
printArray(new Integer(47), new Float(3.14),new Double(11.11));
printArray(47, 3.14F, 11.11);
printArray("one", "two", "three");
printArray(new A(), new A(), new A());
// Or an array:其本身就已经是一个Integer数组了,只不过经过转型变为Object类
printArray((Object[])new Integer[]{ 1, 2, 3, 4 });
printArray(); // Empty list is OK
}
}
package test;
public class Test {
static void f(int required, String... trailing) {
System.out.print("required: " + required + " ");
for(String s : trailing)
System.out.print(s + " ");
System.out.println();
}
public static void main(String[] args) {
f(1, "one");
f(2, "two", "three");
f(0);
}
}
package test;
public class Test {
static void f(Character... args) {
/* getClass:This method returns the object of type Class
* that represents the runtime class of the object.
* for example:
* Integer i = new Integer(5);
* System.out.println("" + i.getClass());
* output : class java.lang.Integer
* */
System.out.print(args.getClass());
System.out.println(" length " + args.length);
}
static void g(int... args) {
System.out.print(args.getClass());
System.out.println(" length " + args.length);
}
public static void main(String[] args) {
f('a');
f();
g(1);
g();
System.out.println("int[]: " + new int[0].getClass());
}
}
可变参数与自动包装机制混合:
package test;
public class Test {
public static void f(Integer... args) {
for(Integer i : args)
System.out.print(i + " ");
System.out.println();
}
public static void main(String[] args) {
f(new Integer(1), new Integer(2));
f(4, 5, 6, 7, 8, 9);
f(10, new Integer(11), 12);
}
}
当可变参数列表运用在重载过程中时,与上述也是一样的,只不过一定要函数调用注意不要出现歧义(Ambiguous),否则编译无法完成
枚举类型
package test;
//枚举类型定义
enum Spiciness {
NOT, MILD, MEDIUM, HOT, FLAMING
}
public class Test {
Spiciness degree;
public Test(Spiciness degree) { this.degree = degree;}
public void describe() {
System.out.print("This Test is ");
switch(degree) {
case NOT: System.out.println("not spicy at all.");
break;
case MILD:
case MEDIUM: System.out.println("a little hot.");
break;
case HOT:
case FLAMING:
default: System.out.println("maybe too hot.");
}
}
public static void main(String[] args) {
Test
plain = new Test(Spiciness.NOT),
greenChile = new Test(Spiciness.MEDIUM),
jalapeno = new Test(Spiciness.HOT);
plain.describe();
greenChile.describe();
jalapeno.describe();
}
}