知识总结接:【Java入门】Java快速入门—java基础代码知识汇总(上)
不废话了,直接看代码:
public final class Program {
static final String STATIC_CONSTANTS = "STATIC_CONSTANTS";
final String INSTANCE_CONSTANTS = "INSTANCE_CONSTANTS";
public static void main(String[] args) {
final String LOCAL_CONSTANTS = "LOCAL_CONSTANTS";
System.out.println(STATIC_CONSTANTS);
System.out.println(new Program().INSTANCE_CONSTANTS);
System.out.println(LOCAL_CONSTANTS);
new Program().test("PARAMETER_CONSTANTS");
}
public final void test(final String msg) {
System.out.println(msg);
}
}
有一点需要注意的是:只有一种情况Java的常量是编译时常量(编译器会帮你替换),其它情况都是运行时常量,这种情况是:静态类型常量且常量的值可以编译时确定。
Java的接口可以包含方法签名、常量和嵌套类,见下例:
public final class Program {
public static void main(String[] args) {
Playable.EMPTY.play();
new Dog().play();
}
}
interface Playable {
Playable EMPTY = new EmptyPlayable();
void play();
class EmptyPlayable implements Playable {
@Override
public void play() {
System.out.println("无所事事");
}
}
}
class Dog implements Playable {
@Override
public void play() {
System.out.println("啃骨头");
}
}
Java枚举是class,继承自java.lang.Enum,枚举中可以定义任何类型可以定义的内容,构造方法只能是private或package private,枚举成员会被编译器动态翻译为枚举实例常量,见下例:
public final class Program {
public static void main(String[] args) {
System.out.println(State.ON);
System.out.println(State.OFF);
for (State item : State.values()) {
System.out.println(item);
System.out.println(State.valueOf(item.name()));
}
}
}
enum State {
ON(1), OFF(0);
int value = 1;
State(int value) {
this.value = value;
}
}
调用枚举的构造方法格式是:常量名字(xxx, xxx),如果构造方法没有参数只需要:常量名子,如:
1 enum State {
2 ON, OFF
3 }
Java中的异常分为checked和unchecked,checked异常必须声明在方法中或被捕获,这点我觉得比较好,必定:异常也是API的一部分,见下例:
public final class Program {
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void test() throws Exception {
throw new Exception("I am wrong!");
}
}
所有继承Exception的异常(除了RuntimeException和它的后代之外)都是checked异常。
Java提供了原始类型对应的引用类型,在1.5之后的版本还提供了自动装箱和自动拆箱,结合最新版本的泛型,几乎可以忽略这块。
import java.util.*;
public final class Program {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(1);
int item1 = (Integer) list.get(0);
System.out.println(item1);
}
}
注意:自动装箱和自动拆箱是Java提供的语法糖。
Java的泛型是编译器提供的语法糖,官方称之为:类型参数搽除,先看一下语法,然后总结一点规律:
测试代码
static void puts(T msg) {
println(msg);
}
static void println(Object msg) {
System.out.println("Object:" + msg);
}
static void println(String msg) {
System.out.println("String:" + msg);
}
调用泛型方法
System.out.println("generic method test");
puts("hello");
Program. puts("hello");
输出的结果是
generic method test
Object:hello
Object:hello
测试代码
class TestGenericClass {
T value;
void setValue(T value) {
this.value = value;
}
}
调用代码
1 System.out.println("generic class test"); 2 System.out.println(t.value);
输出结果
1 generic class test 2 1
测试代码
interface TestInterface {
void test(T item);
}
class TestInterfaceImp1 implements TestInterface {
@Override
public void test(String item) {
System.out.println(item);
}
}
class TestInterfaceImp2 implements TestInterface {
@Override
public void test(T item) {
System.out.println(item);
}
}
调用代码
System.out.println("generic interface test");
TestInterface testInterface1 = new TestInterfaceImp1();
testInterface1.test("hi");
for (Method item : testInterface1.getClass().getMethods()) {
if (item.getName() == "test") {
System.out.println(item.getParameterTypes()[0].getName());
}
}
TestInterface testInterface2 = new TestInterfaceImp2<>();
testInterface2.test("hi");
for (Method item : testInterface2.getClass().getMethods()) {
if (item.getName() == "test") {
System.out.println(item.getParameterTypes()[0].getName());
}
}
输出结果
generic interface test
hi
java.lang.String
java.lang.Object
hi
java.lang.Object
测试代码
class Animal {
}
class Dog extends Animal {
}
class Base {
public void test(T item) {
System.out.println("Base:" + item);
}
}
class Child extends Base {
@Override
public void test(Dog item) {
System.out.println("Child:" + item);
}
}
调用代码
System.out.println("bounded type parameters test");
Base base = new Child();
base.test(new Dog());
for (Method item : base.getClass().getMethods()) {
if (item.getName() == "test") {
System.out.println(item.getParameterTypes()[0].getName());
}
}
输出结果
1 bounded type parameters test 2 Child:Dog@533c2ac3 3 Dog 4 Animal
class Base {
public void test(T item) {
System.out.println("Base:" + item);
}
}
1 class Base {
2 public void test(Animal item) {
3 System.out.println("Base:" + item);
4 }
5 }
class Child extends Base {
@Override
public void test(Animal item) {
this.test((Dog)item);
}
public void test(Dog item) {
System.out.println("Child:" + item);
}
}
System.out.println("bounded type parameters test");
Base base = new Child();
base.test(new Dog());
for (Method item : base.getClass().getMethods()) {
if (item.getName() == "test") {
System.out.println(item.getParameterTypes()[0].getName());
}
}