Java要点和《疯狂Java讲义》例程整理(一)

2014-11-24 03:26:37 · 作者: · 浏览: 0

基础知识

imoprt

The import keyword is to bring in an entire library or a member of that one import java.util.*; import java.util.Vector; import static java.lang.Math.*;

compilation unit

Each compilation unit must have a name ending in java, and inside the compilation unit there can be a public class that must have the same name as teh file. There can be only one public class in each compilation unit.

classpath

Place all the .class files for a particular package into a single directory CLASSPATH contains one or more directories that are used as roots for a search for .class files.

流程控制

public class Test{
	
	public static void main(String[] args)
	{
		int n = 0, m, j, i;
		for(i=3; i<=100; i+=2)
		{
			if(i>=20)
			{
				i=100;
				continue;
			}
			m = (int)Math.sqrt((double)i);
			for(j=2; j<=m; j++)
			{
				if((i%j) == 0)
					break;
			}
			if(j > m)
			{
				System.out.print(i+"\t");
				n++;
				if(n%5 == 0)
					System.out.println();
			}
		}
	}
}

3	5	7	11	13	
17	19

带标签的break

P83
public class Test {
	public static void main(String[] args){
		outer:
		for(int i=0; i<5; ++i){
			for(int j=0; j<3; ++j){
				System.out.println("i : "+i+", j : "+j);
				if( j == 1 ){
					break outer;
				}
			}
		}
	}
}

i : 0, j : 0
i : 0, j : 1

带标签的continue

P85
public class Test {
	public static void main(String[] args){
		outer:
		for(int i=0; i<5; ++i){
			for(int j=0; j<3; ++j){
				System.out.println("i : "+i+", j : "+j);
				if( j == 1 ){
					continue outer;
				}
			}
		}
	}
}

i : 0, j : 0
i : 0, j : 1
i : 1, j : 0
i : 1, j : 1
i : 2, j : 0
i : 2, j : 1
i : 3, j : 0
i : 3, j : 1
i : 4, j : 0
i : 4, j : 1

类绑定

public class Test {
	public static void main(String[] args){
		A a = new B();
		a.f(a);
	}
}

class A{
	public void f(A x){
		System.out.println("A.f(A)");
	}
	public void f(B x){
		System.out.println("A.f(B)");
	}
}

class B extends A{
	public void f(A x){
		System.out.println("B.f(A)");
	}
	public void f(B x){
		System.out.println("B.f(B)");
	}
}

B.f(A)

类的初始化

P159
public class Test {
	public static void main(String[] args){
		new Leaf();
		new Leaf();
	}
}

class Root
{
	static{
		System.out.println("Root's static init block");
	}
	{
		System.out.println("Root's normal init block");
	}
	public Root()
	{
		System.out.println("Root's Non-arg Ctor");
	}
}

class Mid extends Root
{
	static{
		System.out.println("Mid's static init block");
	}
	{
		System.out.println("Mid's normal init block");
	}
	public Mid()
	{
		System.out.println("Mid's Non-arg Ctor");
	}	
}

class Leaf extends Mid
{
	static{
		System.out.println("Leaf's static init block");
	}
	{
		System.out.println("Leaf's normal init block");
	}
	public Leaf()
	{
		System.out.println("Leaf's Non-arg Ctor");
	}	
}

Root's static init block
Mid's static init block
Leaf's static init block
Root's normal init block
Root's Non-arg Ctor
Mid's normal init block
Mid's Non-arg Ctor
Leaf's normal init block
Leaf's Non-arg Ctor
Root's normal init block
Root's Non-arg Ctor
Mid's normal init block
Mid's Non-arg Ctor
Leaf's normal init block
Leaf's Non-arg Ctor

基本数据类型的包装类

P166
public class Test {
	public static void main(String[] args){
		Integer ina = 2;
		Integer inb = 2;
		System.out.println(ina == inb);
		Integer biga = 128;
		Integer bigb = 128;
		System.out.println(biga == bigb);
	}
}

true
false

== 和 equals方法

当使用== 来判断两个变量是否相等时,如果都是基本变量类型,且都是数值类型,则只要值相等,就返回true;对于两个引用类型变量,必须指向同一个对象,==判断才为true;对于基本数据的包装类,值相等则判断为true equals是Object类提供的实例方法,可以重写equals方法实现对象的自定义比较。
P169
public class Test {
	public static void main(String[] args){
		int it = 65;
		float fl = 65.0f;
		System.out.println(it == fl);
		char ch = 'A';
		System.out.println(it == ch);
	}
}

true
true

P169
public class Test {
	public stati