你好,欢迎打开Java世界!
知识总结接:【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精品资料,Java 实战项目,内含多线程,结构,算法,BATJ面试题等大牛累积经验~~~
需要更多资料,可以加下我们Java交流群学习基地,免费获取 +君羊:925050116!!!
接口
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 <T> 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.<String> puts("hello");
输出的结果是
generic method test
Object:hello
Object:hello
泛型类
测试代码
class TestGenericClass<T> {
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<T> {
void test(T item);
}
class TestInterfaceImp1 implements TestInterface<String> {
@Override
public void test(String item) {
System.out.println(item);
}
}
class TestInterfaceImp2<T> implements TestInterface<T> {
@Override
public void test(T item) {
System.out.println(item);
}
}
调用代码
System.out.println("generic interface test");
TestInterface<String> testInterface1 = new TestInterfaceImp1();
testInterface1.test("hi");
for (Method item : testInterface1.getClass().getMethods()) {
if (item.getName() == "test") {
System.out.println(item.getParameterTypes()[0].getName());
}
}
TestInterface<String> 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<T extends Animal> {
public void test(T item) {
System.out.println("Base:" + item);
}
}
class Child extends Base<Dog> {
@Override
public void test(Dog item) {
System.out.println("Child:" + item);
}
}
调用代码
System.out.println("bounded type parameters test");
Base<Dog> 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); } }
- 将T换成extends指定的约束类型,默认是Object。
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()); } }
到此Java快速入门基本代码语法知识的总结完了,关于一些高级特性在后面再持续更新总结,如:运行时进程模型、类型加载机制、反射、注解、动态代理等。还请大家点赞,收藏,关注,同时小伙伴们也可以加入这个学习基地,前来交流学习,一个人闷头自学,永远赶不上一群志同道合的小伙伴讨论学习效率来的快。加入吧(Java:925050116)!