在线工具
java https://c.runoob.com/compile/10
在线编写运行 Java 8 https://www.tutorialspoint.co...
SQL 在线运行 https://www.bejson.com/runcod...
Java牛客题库 https://www.nowcoder.com/inte...
JDK, JRE, JVM
- JDK : Java Development Kit
- JRE : Java Runtime Enviroment
- JVM : Java Virtual Machine
输入Hello Wolrd
vim Hello.java
public class Hello{ public static void main(String[] args){ System.out.print("Hello World!"); }}
编译
javac Hello.java
执行
java Hello
输入
Hello World!
Java程序运行机制
- 编译型:
- 解释型:
程序运行机制:
源文件(*.java) => Java编译器 => 字节码(.class)
=> JVM ( 类装载器 =>字节码校验器 => 解释器 )
=> 操作系统平台
IDEA
新建工程
File => New => Project… => Empty Project
File => Project Structure => Project => Project SDK (1.8.0) => Project language level (8) => OK
根底语法
1.正文,标识符,关键字
正文
//单行正文/* 多行正文 *//** * 文档正文 * @Author itxiaoma */
标识符
类名,变量名,办法名都称为标识符
- 标识符以字母,美元($),下划线(_)开始
- 首字母后能够是字母,美元($),下划线(_),数字的组合
- 不能应用关键字作为变量名,办法名
- 大小写敏感
关键字(50+)
2.数据类型
强类型语言:变量应用必须严格符合规定,所有变量都必须先定义后能力应用
Java数据类型
根本类型:(8大根本类型)
数值
整数
- byte:1个字节(-128~127)
- short:2个字节(-32768~32767)
- int:4个字节(-2147483648~2147483647)21亿+
- long:8个字节(例:long num=40L;)
浮点
- float:4个字节(例:float num=40F;)
- double:8个字节
- 字符串char:2个字节
- boolean:1位,true/false
- 援用类型:类,接口,数组
注1:String不是数据类型,是类
注2:1bit示意1位;(bit是计算机外部数据存储的最小单位)
1Byte示意1个字节,1B=8b;
1KB = 1024B;
1MB = 1024KB;
1GB = 1024MB;
1TB = 1024GB;
Java数据类型的常见问题
整数扩大
int a = 10;int b = 010;//八进制int c = 0x10;//十六进制
浮点型扩大
float f = 0.1f;double d = 1.0/10;System.out.println(f==d);//falsefloat d1 = 123123123123f;float d2 = d1 + 1;System.out.println(d1==d2);//true//float 无限 离散 舍入误差 靠近但不等于//不要用浮点数进行比拟//数学工具类BigDecimal
字符类扩大
char a = 'a';System.out.println((int)a);//97char a1 = '\u0061';System.out.println(a1);//a//所有的字符实质还是数字转义字符:\t 制表符\n 换行...
3.类型转换
运算中,不同类型的数据会先转换位同一类型,再进行计算
优先级:低 ==> 高
byte,short,char => int => long => float => double
主动转化:低 => 高
强制转换: 高 => 低 (类型)变量名
注1:
- 不能对布尔值进行转换
- 不能把对象类型转为不相干的类型
- 高容量转低容量,会有内存溢出或精度问题
注2:JDK7的新个性,数字间能够用下划线宰割(1000 = 1_000)
4.变量、常量、作用域
变量
int a = 1;
常量 - final
常量:初始化后不能扭转,个别为大写
static final double PI = 3.14;
作用域
- 类变量
- 实例变量
- 局部变量
public class Hello { //类变量:static static int a = 1; //实例变量:属于对象,不须要初始化(会主动初始化为类型默认值) //布尔值默认false,除根本类型以外其余变量都是null String b; public static void main(String[] args) { System.out.println(a); int c = 3;//局部变量:必须申明和初始化 System.out.println(c); Hello obj = new Hello(); System.out.println(obj.b);//null }}
命名规定:
- 类变量,局部变量,办法名:驼峰(首字母小写)
- 常量:大写 + 下划线
- 类型:驼峰(首字母大写)
5.根本运算符
- 算术运算符:+, -, *, /, %, ++, --
- 赋值运算符:=
- 关系运算符:>, <, >=, <=, ==, !=, instanceof
- 逻辑运算符:&&,||,!
- 位运算符:&, |, ^, ~, >>, << , >>>
- 条件运算符:? :
- 扩大赋值运算符:+=, -=, *= ,/=
例:
long a = 123L;int b = 123;short c = 10;byte d = 8;System.out.println(a+d);//longSystem.out.println(b+c);//intSystem.out.println(c+d);//int//论断:有long类型时转long,否则转int
幂运算:2^3
double pow = Math.pow(2, 3);System.out.println(pow);//8.0
位运算
<< 乘2 >> 除2
6.包机制
包的实质就是文件夹
- 包机制是为了更好地组织类,用于区别类名的命名空间
- 个别用公司名倒置作为报名 pakage com.baidu.www;
- 应用import导入包(import 包名.* 示意导入包下所有类)
7.JavaDoc生成文档
正文参数
/** * @author 作者 * @version 版本 * @since 须要最早应用的jdk版本 * @param 参数 * @return 返回值 * @throws 异样抛出 */
中文编码
javadoc -encoding UTF-8 -charset UTF-8 Demo.java
流程管制
1.用户交互Scanner (Java5新个性)
根底应用
import java.util.Scanner;public class Demo { public static void main(String[] args) { //创立扫描器对象,用于接管键盘数据 Scanner s = new Scanner(System.in); //判断用户输出 if(s.hasNextLine()){ String str = s.nextLine(); System.out.println(str); } //IO流的类会始终占用内存,用完须要敞开 s.close(); }}//next()以空格为结束符,nextLine()以回车为结束符
应用进阶
计算多个数字的总和与平均数,非数字完结
Scanner s = new Scanner(System.in);double sum = 0;int num = 0;while(s.hasNextDouble()){ double x = s.nextDouble(); sum += x; num++;}System.out.println("和是:" + sum);System.out.println("平均值是" + (sum/num));s.close();
2.九九乘法表
/*1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 */for (int m = 1; m <= 9; m++) { for (int n = 1; n <= m; n++) { System.out.print(m + "*" + n + "=" + (m * n) + "\t"); } System.out.println();}//System.out.print 不换行输入
3.加强for循环(JDK1.5新个性)
int[] numbers = {1, 2, 3, 4, 5};for (int x : numbers) { System.out.println(x);}
4.label
打印101 - 150间的所有质数
outer:for (int i = 101; i <= 150; i++) { for (int j = 2; j < i / 2; j++) { if (i % j == 0) { continue outer; } } System.out.println(i);}
Java办法
办法是用来实现特定性能的代码片段,由办法名和办法体组成
办法名:修饰符 返回值类型 办法名 (参数类型 参数名){}
办法体:return 返回值;
参数类型:实参,形参
注:java都是值传递,没有援用传递
1.办法的重载
重载就是在一个类中,有雷同的函数名称,但形参不同的函数
办法重载规定:
- 办法名称必须雷同
- 参数列表必须不同(个数,类型,参数程序不同)
- 返回类型可雷同可不同
2.命令行传参
程序运行时传递参数,能够通过main办法传递
public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]); }}
3.可变参数(JDK1.5新个性)
public static void main(String[] args) { test(1,2,3);}public static void test(int... i){ System.out.println(i[0]); System.out.println(i[1]); System.out.println(i[2]);}
4.递归
递归头:什么时候不调用本身办法。如果没有头,将陷入死循环。
递归体:什么时候须要调用本身办法。
Java数组
数组是雷同类型数据的有序汇合
dataType[] arrayRefVar = new dataType[size];int nums[] = new int[10];//二维数组int[][] n = new int[2][2];int[][] n = {{1,2},{3,4}}
根本特点
- 长度是确定的,一旦创立,长度不可变
- 元素类型必须雷同
- 数组对象自身在堆中(数组是援用类型,可看做对象,数组元素相当于对象的成员变量,Java的对象在堆中)
内存剖析
graph LRA(java内存)A --> B(堆) B -->B1[寄存new的对象和数组] B -->B2[能够被所有线程共享,不会寄存别的对象援用] A --> C(栈) C -->C1[寄存根本变量类型,蕴含具体数值] C -->C2[援用对象的变量,寄存援用在堆中的地址] A --> D(办法区) D -->D1[能够被所有线程共享] D -->D2[蕴含所有calss和static变量]
//1.申明数组int[] nums;//在栈中//2.创立数组nums = new int[10];//在堆中
三种初始化状态
//动态初始化int[] a = {1, 2, 3};Man[] mans = {new Man(1),new Man(2)}//动静初始化(蕴含默认初始化)int[] b = new int[10];//默认调配10个0b[0] = 10;
注:数组是援用类型,数组元素相当于类的实例变量,数组创立时调配空间,数组元素也会按默认值初始化。
数组工具类java.util.Arrays
打印数组元素 | Arrays.toString(a); | |
---|---|---|
数组填充 | Arrays.fill(a, 0 , 1, 0); | 数组下标0-1的元素用0填充 |
数组排序 | Arrays.sort(a); | |
比拟数组 | equals | |
查找数组元素 | binarySearch | 对排序好的数组进行二分法查找 |
冒泡排序
- 共有八大排序
- 工夫复杂度为O(n2)
稠密数组
当一个数组中大部分元素为0或值雷同时,能够应用稠密数组保留
解决形式:
1.记录数组行列数,不同值数
2.把不同值的元素行列信息,元素值记录在小规模数组中
/*稠密数组1 2 12 3 2还原数组0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 */public class Demo { public static void main(String[] args) { int[][] array1 = new int[11][11]; array1[1][2] = 1; array1[2][3] = 2; int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array1[i][j] != 0) { sum++; } } } int[][] array2 = new int[sum + 1][3]; array2[0][0] = 11; array2[0][1] = 11; array2[0][2] = sum; int count = 0; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1[i].length; j++) { if (array1[i][j] != 0) { count++; array2[count][0] = i; array2[count][1] = j; array2[count][2] = array1[i][j]; } } } System.out.println("稠密数组"); for (int j = 1; j < array2.length; j++) { System.out.println(array2[j][0] + "\t" + array2[j][1] + "\t" + array2[j][2]); } System.out.println("还原数组"); int[][] array3 = new int[array2[0][0]][array2[0][1]]; for (int j = 1; j < array2.length; j++) { array3[array2[j][0]][array2[j][1]] = array2[j][2]; } for (int[] ints : array3) { for (int num : ints) { System.out.print(num + "\t"); } System.out.println(); } }}
面向对象
以类的形式组织代码。以对象组织(封装)数据
三大个性:封装,继承,多态
1.创立类与对象
一个我的项目应该只存在一个main办法
创建对象
应用new创建对象时,除了分配内存空间,还会给创立好的对象进行默认初始化,及调用类中的结构器。
com/oop/demo/Application.java
package com.oop.demo;public class Application { public static void main(String[] args) { //类:形象的,实例化后会返回一个本人的对象 Student xiaoming = new Student(); Student xiaohong = new Student(); xiaoming.name = "小明"; xiaoming.age = 20; xiaohong.name = "小红"; xiaohong.age = 21; System.out.println("姓名:" + xiaoming.name + "\t年龄:" + xiaoming.age); xiaoming.study(); System.out.println("姓名:" + xiaohong.name + "\t年龄:" + xiaohong.age); xiaohong.study(); }}
com/oop/demo/Student.java
package com.oop.demo;public class Student { //属性 String name; int age; //办法 public void study() { System.out.println(this.name + "在学习"); }}
结构器(构造方法)
- 必须和类名雷同
- 必须没有返回类型(不能写void)
- 一旦定义了有参数的结构器,new对象时想不传参数,就必须写一个无参数结构器
public class Student { //属性 String name; int age; //无参(默认)结构器 public Student(){ } //有参 public Student(String name){ this.name = name; } public Student(String name,int age){ this.name = name; this.age = age; } }
创建对象内存剖析
public class Application { public static void main(String[] args) { //类:形象的,实例化后会返回一个本人的对象 Student xiaoming = new Student(); Student xiaohong = new Student(); xiaoming.name = "小明"; xiaoming.age = 20; xiaohong.name = "小红"; xiaohong.age = 21; System.out.println("姓名:" + xiaoming.name + "\t年龄:" + xiaoming.age); xiaoming.study(); System.out.println("姓名:" + xiaohong.name + "\t年龄:" + xiaohong.age); xiaohong.study(); }}
办法区也在堆中;
静态方法区和类一起加载,所以静态方法能够间接调用,不须要实例化
- 加载Application和Student类信息到办法区
- 加载main()办法到栈中
- 加载xiaoming和xiaohong援用变量名到栈中
- 加载Student xiaoming = new Student();和Student xiaohong = new Student();实例到堆中
- xiaoming和xiaohong调用办法区的study办法
注:根本类型外的变量都是援用类型,是通过援用来操作的;援用名在栈中,指向对象实体在堆中的地址。
封装
禁止间接拜访一个对象中数据的理论示意,应通过接口操作来拜访。
属性公有(private),get/set
public class Student { private String name; private int age; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; }}
继承extends
继承的实质是对某一批类的形象,从而实现更好的建模;
继承是类之间的关系,子类继承(extends)父类的全副public办法;
公有的属性和办法无奈被继承
注:
- Java中只有单继承,没有多继承 (一个子类只能有一个父类)
- Java中所有类,都默认间接或间接继承Object类
Super
留神点
- super调用父类的构造方法,必须在构造方法的第一行
- super只能呈现在子类的办法或构造方法中
- super和this不能同时在构造方法中调用(都须要在第一行)
super和this的区别
this | super | |
---|---|---|
代表对象不同 | 调用者自身的对象 | 父类对象的援用 |
应用前提不同 | 没有继承也能应用 | 只能在有继承时应用 |
调用构造方法不同 | 调用本类的构造方法 | 调用父类的构造方法 |
com/oop/demo/Application.java
package com.oop.demo;public class Application { public static void main(String[] args) { Student xiaoming = new Student(); xiaoming.test1(); //Person无参结构 //Student无参结构 //Student //Student //Person }}
com/oop/demo/Person.java
package com.oop.demo;public class Person { public Person() { System.out.println("Person无参结构"); } public void print(){ System.out.println("Person"); }}
com/oop/demo/Student.java
package com.oop.demo;public class Student extends Person{ public Student() { //暗藏代码:调用了父类的无参结构super(); //super(); System.out.println("Student无参结构"); } public void print(){ System.out.println("Student"); } public void test1(){ print(); this.print(); super.print(); }}
办法重写@Override
子类的办法和父类必须要统一,办法体不同
重写都是办法的重写,和属性无关
静态方法不能被重写 !
留神点
- 办法名,参数列表必须雷同
- 修饰符范畴能够扩充不能放大
- 抛出异样范畴能够放大不能扩充
代码示例
静态方法
public class B { public static void test(){ System.out.println("B=>test()"); }}public class A extends B{ public static void test(){ System.out.println("A=>test()"); }}public class Application { public static void main(String[] args) { //静态方法:办法调用只和右边定义的数据类型无关 A a = new A(); a.test();//A=>test() //父类的援用指向了子类 B b = new A(); b.test();//B=>test() }}
办法重写
public class B { public void test(){ System.out.println("B=>test()"); }}public class A extends B{ @Override public void test() { System.out.println("A=>test()"); }}public class Application { public static void main(String[] args) { A a = new A(); a.test();//A=>test() B b = new A(); b.test();//A=>test() }}
多态
同一办法依据发送对象不同,采纳多种不同的行为形式;
一个对象的类型是确定的,但能够指向对象的援用类型有很多(能够是子类,父类,Object)
多态存在的条件:
- 有继承关系
- 子类重写父类办法
- 父类援用指向子类对象
注:多态是办法的多态,属性没有多态。
无奈重写的办法
- static办法,属于类,不属于实例
- final 常量 (final润饰的类不能被继承)
- private办法
//子类能调用的办法都是本人的或继承父类的A a = new A();B b = new B();//对象能执行哪些办法看右边的援用类型//父类援用能够指向子类,但不能调用子类独有的办法B c = new A();a.test();//A=>test()b.test();//B=>test()//子类重写了父类办法,会执行子类的办法c.test();//A=>test()
instanceof和类型转化
- 父类援用能够指向子类的对象,反过来不行
- 子类转换为父类称为向上转型,可能失落本人原本的一些办法
- 父类转换为子类称为向下转型,须要强制转换
public class Application { public static void main(String[] args) { //子类特有办法go Student s = new Student(); s.go(); //向上转型,转型后无奈再调用go办法 Person p = s; //向下转型 ((Student) p).go(); }}
static关键字
动态变量
private static int age;
静态方法
public class Application { private static void test(){ System.out.println("Static Function"); } public static void main(String[] args) { test(); }}
动态代码块
public class Person { { System.out.println("匿名代码块"); } static { System.out.println("动态代码块");//只会在类加载时执行一次 } public Person() { System.out.println("构造方法"); } public static void main(String[] args) { Person p = new Person(); //动态代码块 //匿名代码块 //构造方法 }}
动态导入包
import static java.lang.Math.random;public class Person { public static void main(String[] args) { System.out.println(random()); }}
抽象类 abstract
//抽象类 (接口能够多继承)abstract class Person { public abstract void doSomething();//形象办法:只有办法名,没有办法体}
注:
- 抽象类不能new,只能靠子类实现
- 形象办法必须在抽象类中,但抽象类能够蕴含一般办法
接口 interface
对对象的形象,束缚和实现拆散:面向接口编程
注:
- 接口不能被实例化,接口中没有构造方法
- 接口中所有办法都是形象的(public abstract)
- 接口中只有常量(public static final)
- 类通过implements实现接口,一个类能够实现多个接口,实现多继承
- 实现接口的类,必须重写接口中所有办法
public interface UserService { void userTest1(); void userTest2();}public interface TimeService { void time1(); void time2();}public class UserTime implements UserService,TimeService{ @Override public void userTest1() { } @Override public void userTest2() { } @Override public void time1() { } @Override public void time2() { }}
外部类
匿名对象:不将实例保留到变量(栈)中,只在堆内存中应用对象
public class Application { public static void main(String[] args) { new Apple().eat(); }}class Apple{ public void eat(){ System.out.println("吃苹果"); }}
匿名外部类
public interface UserService { void userTest1(); void userTest2();}public class Application { UserService u = new UserService() { @Override public void userTest1() { } @Override public void userTest2() { } };}
异样Error Exception
Error
Error类对象由JVM生成并抛出,大多数谬误与操作无关,产生谬误时JVM个别会终止线程;
如内存溢出(OutOfMemeryError),栈溢出,类定义谬误(NoClassDefFoundError),链接谬误(LinkageError)
Exception
异样通常由逻辑谬误引起,应尽量避免
- 非运行时异样(IO异样...)
- 运行时异样(RuntimeException)
注:Error通常是致命性谬误,程序无奈解决;Exception通常能够且应该尽可能的解决。
异样解决机制
- try 监控区域 (必要)
- catch 捕捉异样 (必要)
- finally 最终解决 (开释占用资源)
- throw 抛出异样(个别在办法中应用 )
- throws
注:捕捉多个异样须要从小到大XXXException < XXXException < Throwable
public class Application { public static void main(String[] args) { int a = 1; int b = 0; try{ if(b == 0){ throw new ArithmeticException(); } System.out.println(a/b); }catch (ArithmeticException e){ e.printStackTrace();//打印谬误的栈信息 }catch (Exception e){ }catch (Throwable e){ }finally { } }}
自定义异样
用户自定义异样类,只须要继承Exception类即可
public class MyException extends Exception { String msg; public MyException(String msg) { this.msg = msg; } @Override public String toString() { return "MyException{" + msg + "}"; }}public class Application { static void test(String msg) throws MyException{ throw new MyException(msg); } public static void main(String[] args) { try{ test("test"); }catch (MyException e){ System.out.println(e);//MyException{test} } }}