什么是JAVA
历史
Java源于上个世纪90年代詹姆斯·高斯林 (高司令,Java之父)开发的Oak语言,最早的目标是针对小型家电设备的嵌入式应用。后来开发Oak的SUN公司被Oracle收购,Oak也改名为Java
跨平台特性
Java介于编译型语言和解释型语言之间。
----编译型语言如C、C++,代码是直接编译成机器码执行,需要为每一种平台的编译对应的机器码。
----解释型语言如Python、Ruby,由解释器直接加载源码然后运行,代价是运行效率太低。
----Java是将代码编译成一种“字节码”,类似于抽象的CPU指令。这种“字节码"需要开发者针对不同平台分别编写虚拟机来加载字节码并执行。
源代码文件对应后缀.java
字节码文件对应后缀.class
$ javac对应编译命令----.java -> .class
$ java对应运行命令----run .class
JVM、JRE与JDK
JDK >> JRE >> JVM
----JVM(Java Virtual Machine)即JAVA虚拟机
----JRE(Java Runtime
Environment)不仅包括了JVM,还提供了编译器、调试器等开发工具,是将Java源码编译为Java字节码的运行环境
----JDK(Java Development
Kit)包括了JVM和JRE,还提供了许多Java工具和基础类库
三个版本
java具有三个版本
----JAVA SE(标准版):语言本身、核心开发技术、Java标准库
----JAVA EE(企业版):Spring框架、数据库开发、分布式架构
----JAVA ME(嵌入式):针对嵌入式设备
从包含内容上看,EE >> SE >> ME
Java基础
1)基本类型
1 2 3 4 5 6 7 - 整数类型:byte ,short ,int ,long - 浮点数类型:float ,double - 字符类型:char - 布尔类型:boolean ---------------------------------- - 字符串类型:String - 自动判断:var
2)命名规范
1 2 3 4 5 6 7 8 - Hello - NoteBook - VRPlayer - main - goodMorning - playVR
3)输入输出
--输入
1 2 3 4 5 6 7 8 9 10 11 12 import java.util.Scanner;public class Main { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.print("Input your name: " ); String name = scanner.nextLine(); System.out.print("Input your age: " ); int age = scanner.nextInt(); System.out.printf("Hi, %s, you are %d\n" , name, age); } }
--输出
1 2 3 4 5 6 7 8 9 public class Main { public static void main (String[] args) { System.out.print("A:" ); double d = 3.1415926 ; System.out.printf("%.2f\n" , d); System.out.println(); System.out.println("END" ); } }
%d ---- 整数
%f ---- 浮点数
%x ---- 十六进制浮点
%e ---- 格式化输出
%s ---- 字符串
4)数组与排序
1 2 3 4 5 6 7 8 9 import java.util.Arrays;public class Main { public static void main (String[] args) { int []a = {7 ,5 ,3 ,1 ,6 ,7 }; System.out.println(Arrays.toString(a)); Arrays.sort(a); System.out.println(Arrays.toString(a)); } }
5)命令行参数
1 2 3 4 5 6 7 8 9 10 11 12 import java.util.Arrays;public class Main { public static void main (String[] args) { for (String it : args){ System.out.println(it); } } }
Java面向对象特性
1)继承(extends)
在Java中使用extends实现继承。
每个类都有个父类,默认父类为Object。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Person { private String name; private int age; public Person (String name, int age) { this .name = name; this .age = age; } public String getName () {...} public void setName (String name) {...} public int getAge () {...} public void setAge (int age) {...} }class Student extends Person { private int score; public int getScore () { … } public void setScore (int score) { … } }
在java中使用super来访问父类的函数
在构造函数中必须先调用super()来实例化父类field
1 2 3 4 5 6 7 class Student extends Person { protected int score; public Student (String name, int age, int score) { super (name, age); this .score = score; } }
因为所有的class
最终都继承自Object
,而Object
定义了几个重要的方法:
toString()
:把instance输出为String
;
equals()
:判断两个instance是否逻辑相等;
hashCode()
:计算一个instance的哈希值。
2)重写(override)
例如,在Person
类中,我们定义了run()
方法:
1 2 3 4 5 class Person { public void run () { System.out.println("Person.run" ); } }
在子类Student
中,覆写这个run()
方法:
1 2 3 4 5 6 class Student extends Person { @Override public void run () { System.out.println("Student.run" ); } }
一个很经典的报税例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 public class Main { public static void main (String[] args) { Income[] incomes = new Income [] { new Income (3000 ), new Salary (7500 ), new StateCouncilSpecialAllowance (15000 ) }; System.out.println(totalTax(incomes)); } public static double totalTax (Income... incomes) { double total = 0 ; for (Income income: incomes) { total = total + income.getTax(); } return total; } }class Income { protected double income; public Income (double income) { this .income = income; } public double getTax () { return income * 0.1 ; } }class Salary extends Income { public Salary (double income) { super (income); } @Override public double getTax () { if (income <= 5000 ) { return 0 ; } return (income - 5000 ) * 0.2 ; } }class StateCouncilSpecialAllowance extends Income { public StateCouncilSpecialAllowance (double income) { super (income); } @Override public double getTax () { return 0 ; } }
此外,Java中也允许重写Object类的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Person { @Override public String toString () { return "Person:name=" + name; } @Override public boolean equals (Object o) { if (o instanceof Person) { Person p = (Person) o; return this .name.equals(p.name); } return false ; } @Override public int hashCode () { return this .name.hashCode(); } }
3)常量(final)
在Java中可以使用final来修饰类/方法/字段,以禁止继承/重写/修改
例如:
1.禁止重写
1 2 3 4 5 6 7 8 9 10 11 12 13 class Person { protected String name; public final String hello () { return "Hello, " + name; } }class Student extends Person { @Override public String hello () { } }
2.禁止继承
1 2 3 4 5 6 7 final class Person { protected String name; }class Student extends Person { }
3.禁止修改
1 2 3 4 5 6 class Person { public final String name = "Unamed" ; } Person p = new Person (); p.name = "New Name" ;
4)抽象(abstract)
如果父类的方法本身不需要实现任何功能,仅仅是为了定义方法签名,目的是让子类去覆写它,那么,可以把父类的方法声明为抽象方法:
1 2 3 4 class Person { private int name; public abstract void run () ; }
把一个方法声明为abstract
,表示它是一个抽象方法,本身没有实现任何方法语句。因为这个抽象方法本身是无法执行的,所以,Person
类也无法被实例化。编译器会告诉我们,无法编译Person
类,因为它包含抽象方法。必须把Person
类本身也声明为abstract
,才能正确编译它:
1 2 3 4 abstract class Person { private int name; public abstract void run () ; }
5)接口(interface)
1.基本定义
接口是没有字段的抽象类,如果一个抽象类没有字段,所有方法全部都是抽象方法,如下:
1 2 3 4 5 abstract class Person { public abstract void run () ; public abstract String getName () ; }class Student extends Person {...}
那么,就可以把该抽象类改写为接口:interface
,如下:
1 2 3 4 5 interface Person { void run () ; String getName () ; }class Student implements Person {...}
interface
不能定义实例字段。但是,interface
是可以有static
final字段的,例如:
1 2 3 4 5 6 public interface Person { public static final int MALE = 1 ; public static final int FEMALE = 2 ; }
2.实现多个接口
在Java中,一个类不能继承多个类,但是却可以实现多个接口,如下:
1 class Student implements Person , Hello {...}
3.接口继承
此外,一个interface
也可以继承自另一个interface
。interface
继承自interface
使用extends
,它相当于扩展了接口的方法。例如:
1 2 3 4 5 6 7 8 interface Hello { void hello () ; }interface Person extends Hello { void run () ; String getName () ; }
Person
接口现在实际上有3个抽象方法签名,其中一个来自继承的Hello
接口。
----接口的default方法
在接口中,可以定义default
方法,提供该方法未被实现类实现时的定义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 interface Person { String getName () ; default void run () { System.out.println(getName() + " run" ); } }class Student implements Person { private String name; public Student (String name) { this .name = name; } public String getName () { return this .name; } }
6)包(package)
Java定义了一种名字空间,称之为包:package
。一个类总是属于某个包,类名(比如Person
)只是一个简写,真正的完整类名是包名.类名
。
例如:
小明的Person
类存放在包ming
下面,因此,完整类名是ming.Person
;
小红的Person
类存放在包hong
下面,因此,完整类名是hong.Person
;
小军的Arrays
类存放在包mr.jun
下面,因此,完整类名是mr.jun.Arrays
;
JDK的Arrays
类存放在包java.util
下面,因此,完整类名是java.util.Arrays
。
例如:
1 2 3 4 5 6 7 8 9 10 11 package ming;import mr.jun.*;public class Person { public void run () { Arrays arrays = new Arrays (); } }
7)作用域(public)
public、private、protected是Java中声明作用域的修饰符
----default:类/字段/方法可以在同一包内被访问
----public:类/字段/方法在任意位置被访问
----private:类不可在其他地方被访问,字段/方法只能在类内部访问
----protected:字段/方法只能在类及其子类内部访问