狂神说java
0x00 java基础
- jdk 开发环境
- jre 运行环境
- jvm java虚拟机
0x00 java程序运行机制
0x00 hello world
public class Hello{
public static void main(String[] args){
System.out.print("Hello world!");
}
}
cmd下
javac hello.java
编译生成hello.class文件 字节码文件
运行: java hello #若存在包,最要退回去包之外,例如 java com.test.method.hello
0x01 java基础语法
0x00 基本数据类型
public class Demo1 {
public static void main(String[] args) {
// 八大基本数据类型
// 整数
byte num1 = 12;
int num2 = 12;
short num3 = 122;
long num4 = 122L; // long在后面加个 L
// 小数:浮点数
float num5 = 30.1F; // float在后面加个 F
double num6 = 3.1415;
// 字符
char num = 'A';
// 布尔值
boolean flag = true;
String str = "string";
System.out.println(str);
}
}
0x01 类型转换
精度从低到高
byte, short, char -> int -> long -> float -> double
数据从低变高
自动转换
数据从高变低
需要强制转换
存在溢出的可能
注意 两个数相加,结果类型
有 long 为 long
有 double 为 double
啥都没有就是 int
0x02 变量作用域
public class Demo02 {
// 类变量 static
static double salary = 2500;
// 实例变量,可以不赋值
// 默认是 0 0.0 False null
String str;
int age;
Boolean flag;
// 常量 final 修饰符不在意位置
static final double PI = 3.1415;
// main方法
public static void main(String[] args) {
Demo02 demo02 = new Demo02();
System.out.println(salary); //可直接用
System.out.println(demo02.age); //可直接用
System.out.println(PI); //可直接用
}
}
0x03 命名规范
- 类名: HelloWorld()
- 常量: MAX_VALUE
- 其他: getName()
0x04 运算符
算数运算符
+ - * / % ++ --
赋值运算符
= += -= *= /=
关系运算符
> < >= <= == != instanceof
逻辑运算符
&& || !
位运算符
& | ^ ~ >> << >>>
条件运算符
? :
Math类
2^3 = 8
Math.pow(2,3) = 8
0x05 JavaDoc
/**
* @author Martin
* @version 1.0
* @since 1.8 #指明最早需要的jdk版本
*
*/
public class Doc {
String name;
/**
* @param name
* @return
* @throws Exception
*/
public String getName(String name) throws Exception{
return name;
}
}
在cmd命令下 javadoc -encoding UTF-8 -charset UTF-8 Doc.java
生成javaDoc文档,也可用IDEA生成
0x02 java流程控制
0x00 用户交互Scanner
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
// 两个方法
// next() hasNext() 空格为结束符
// nextLine() hasNextLine() 回车为结束符
// 或者 nextDouble() 等等
// 创建一个扫描器对象,用于接收键盘输入
Scanner scanner = new Scanner(System.in);
System.out.println("等待接收:");
// 判断用户有没有输入
if (scanner.hasNext()){
// 使用next接收
String str = scanner.next();
System.out.println("输出的内容为:"+ str);
}
// 关闭IO流
scanner.close();
}
}
0x01 Switch
public class Demo02 {
public static void main(String[] args) {
char c = 'A';
// jdk 7以后,表达式可以是 字符串
switch (c){
case 'A':
System.out.println("优秀");
break; //不加的话,就不会退出,出现case穿透,导致下面的全部输出
case 'B':
System.out.println("及格");
break;
default:
System.out.println("未知");
}
}
}
0x02 For
/*
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
*/
public class Demo03For {
public static void main(String[] args) {
// 九九乘法表
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(i +"*"+ j +"="+ i*j + "\t");
}
System.out.println("");
}
// 增强FOR
int[] numbers = {1,2,3,4,5};
for(int x:numbers){
System.out.print(x+"\t"); // 1 2 3 4 5
}
}
}
0x03 Break 和 Continue
0x03 java方法
0x00 方法重载
- 方法名称相同
- 参数列表不同
public class Demo01 {
public static void main(String[] args) {
int num1 = test(10);
double num2 = test(10);
}
public static int test(int num){
return num;
}
public static double test(double num){
return num;
}
}
0x01 可变参数
- JDK1.5开始
- 指定参数后加一个省略号
- 一个方法最多一个可变参数,且是最后一个
public class Demo02 {
public static void main(String[] args) {
Demo02 demo02 = new Demo02();
demo02.test(1,2,3,4,5);
}
// 本质是数组
public void test(int... i){
System.out.println(i[0]);
System.out.println(i[1]);
System.out.println(i[2]);
System.out.println(i[3]);
System.out.println(i[4]);
}
}
0x02 递归
// 阶乘
public class Demo03 {
public static void main(String[] args) {
System.out.println(f(5)); //5! = 120
}
public static int f(int num){
if (num==1){
return 1;
}else {
return num*f(num-1);
}
}
}
0x04 数组
0x00 数组基础
- 第一步:栈中声明
- 第二步:堆中找一个内存空间
- 第三步:赋值
public class Demo01 {
public static void main(String[] args) {
// 1. 声明一个数组
int[] num1;
int num2[];
// 2. 创建一个数组
num1 = new int[10];
// 3. 赋值
for (int i = 0; i < num1.length; i++) {
num1[i] = i;
System.out.println(num1[i]);
}
// 三种初始化方式
// 静态初始化: 创建+赋值
int[] a = {1,2,3};
// 动态初始化:创建不赋值,默认值
int[] b = new int[10];
}
}
0x01 二维数组
package array;
public class Demo02 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
// 快捷键 arrays.for
for (int array : arrays) {
System.out.println(array);
}
int[][] newArrays = {{1,2},{2,3},{3,4},{4,5}};
// [4][2]
/*
1,2 array[0]
2,3 array[1]
3,4 array[2]
4,5 array[3]
*/
}
}
0x02 Arrays类
import java.util.Arrays;
public class Demo03 {
public static void main(String[] args) {
int[] a = {1,6,4,6,7,82,9};
Arrays.sort(a);
System.out.println(Arrays.toString(a)); //[1, 4, 6, 6, 7, 9, 82]
Arrays.fill(a,2,4,0);
System.out.println(Arrays.toString(a)); //[1, 4, 0, 0, 7, 9, 82]
int[] b = {1,6,4,6,7,82,9};
// 冒泡排序
for (int i = 0; i < b.length-1; i++) {
boolean flag = false;
for (int j = 0; j < b.length-i-1 ; j++) {
if (b[j+1] < b[j]) {
int tmp = b[j];
b[j] = b[j+1];
b[j+1] = tmp;
flag = true;
}
}
if(flag == true){
break;
}
}
System.out.println(Arrays.toString(b));
}
}
0x03 稀疏矩阵
0x05 面向对象
0x00 基础
- 三大特性
- 封装
- 继承
- 多态
public class Test {
public static void main(String[] args) {
Student student = new Student();
System.out.println(Student.getStaticName()); // static
System.out.println(student.getName()); // null
}
}
class Student {
// 直接用类名调用
public static String getStaticName(){
return "static";
}
// 必须实例化才能调用
public String getName(){
return "null";
}
}
0x01 构造函数
public class Test {
public static void main(String[] args) {
Student student = new Student("小马");
student.getName(); // 小马
}
}
class Student{
String name;
// 构造函数 快捷键 Alt + Insert
public Student(String name){
this.name = name;
}
public void getName(){
System.out.println(this.name);
}
}
0x02 封装
public class Test {
public static void main(String[] args) {
Student student = new Student();
student.setName("setName");
System.out.println(student.getName());
}
}
class Student{
// 私有属性
private String name;
private int id;
// 需要提供一些方法来操作私有属性
// Alt + Insert Getter and Setter
// 一般会在里面做一些安全性的判定
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
0x03 继承
//继承
public class Test {
public static void main(String[] args) {
// 子类对象
Student student = new Student();
// 继承父类的public等非私有变量
System.out.println(student.id); //123
// 继承父类的所有非私有方法
System.out.println(student.sayId()); //123
// 测试super()
student.testSuper();
}
}
class Person{
public String name = "Person";
public int id = 123;
// 一个方法
public int sayId(){
return this.id;
}
}
// 只能单继承
class Student extends Person{
public String name = "Student";
public void testSuper(){
System.out.println(this.name); //Student
System.out.println(super.name); //Person
}
}
- 子类的构造函数中默认有
super()
代表先父类构造,然后子类构造。且必须在第一行
0x04 重写
重写,子类重写父类的方法!
- 方法名相同
- 参数列表相同
- 修饰符修饰符:范围可以扩大但不能缩小:public>Protected>Default>private
- 抛出的异常:范围,可以被缩小,但不能扩大; classNotFoundException -->Exception(大)
public class Test {
public static void main(String[] args) {
// 左边 new 右边()
A a = new A();
B b = new A(); // 父类的引用指向了子类
// 静态方法 与左边有关
A.testStatic(); // A->testStatic()
B.testStatic(); // B->testStatic()
// 非静态方法 与右边有关
a.test(); // A->test()
b.test(); // A->test() 子类重写了父类的方法,所以父类调用子类
}
}
class A extends B{
// Override 重写,有特定功能的注解 Alt + Insert Override
@Override
public void test() {
System.out.println("A->test()");
}
public static void testStatic(){
System.out.println("A->testStatic()");
}
}
class B{
public void test(){
System.out.println("B->test()");
}
public static void testStatic(){
System.out.println("B->testStatic()");
}
}
0x05 多态
多态的前提条件:
- 1 要有继承关系(实现关系)
- 2 要有方法的重写
- 3 要有父类的引用指向子类对象。
多态指的是同一个方法调用,由于对象不同可能会有不同的行为。
用该父类引用调用子类重写的方法,此时多态就出现了。
package oop.Demo06;
// 多态
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
Person s2 = new Student(); // 通过父类 new一个子类
s1.run(); //Student run
s2.run(); //Student run
s1.say(); //Student say
// 高变低 强制转换
((Student)s2).say(); //Student say
}
}
class Person{
public void run(){
System.out.println("Person run");
}
}
class Student extends Person{
@Override
public void run() {
System.out.println("Student run");
}
public void say(){
System.out.println("Student say");
}
}
0x06 instanceof
X instanceof Y
X和Y是不是有父子类的关系
0x07 抽象类和接口
- 普通类: 只有具体实现
- 抽象类: 可有规范也可有具体实现
- 接口: 只有规范
public class TestAbstract {
public static void main(String[] args) {
}
}
// 抽象类 abstract
abstract class Action{
// 约束 有人帮我们实现
// abstract 抽象方法,只有名字没有实现
public abstract void doSomething();
public void run(){
}
// 1.抽象类不能被 new 出来,只能子类实现
// 2.可以有普通的非抽象方法
}
// 子类必须实现 抽象类的所有方法,除非子类也是抽象类
class A extends Action{
@Override
public void doSomething() {
}
}
接口不能被实例化
// 约束和实现分离
public class TestInterface {
public static void main(String[] args) {
}
}
// interface 接口
// 接口都需要有实现类
interface UserService{
// 接口中所有的定义都默认是抽象的,默认有 public abstract
void run();
}
interface TimeService{
void timer();
}
// 实现类, 可以间接实现伪多继承
class UserServiceImpl implements UserService, TimeService{
@Override
public void timer() {
}
@Override
public void run() {
}
}
0x08 内部类
package oop.Demo08;
public class Test {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.in(); //Inner
inner.getId(); //10
}
}
class Outer{
private int id=10;
public void out(){
System.out.println("Outer");
}
public class Inner{
public void in(){
System.out.println("Inner");
}
// 获得外部类的私有属性
public void getId(){
System.out.println(id);
}
}
}
0x06 异常
0x00 try-catch
package exception;
public class Demo01 {
public static void main(String[] args) {
int a = 1;
int b = 0;
// 假设捕获多个异常,要从小到大
// 快捷键 Ctrl + Alt + T
try {
if (b==0){
// 主动抛出异常,一般在方法中用
throw new ArithmeticException();
}
System.out.println(a/b);
}catch (ArithmeticException e){
System.out.println("b不能为0");
}catch (Error e){
System.out.println("error");
}finally { // 一般处理善后的工作,例如关闭IO
System.out.println("finally");
}
// b不能为0
// finally
try {
test(a,b);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
public static void test(int a,int b) throws ArithmeticException{
if (b==0){
throw new ArithmeticException();
}
}
}
0x01 自定义异常
package exception;
// 自定义异常
public class Demo02 {
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
System.out.println("MyException->" + e); // MyException->MyException{detail=11}
}
}
public static void test(int a) throws MyException {
if (a>10){
throw new MyException(a);
}
}
}
class MyException extends Exception{
private int detail;
public MyException(int detail) {
this.detail = detail;
}
@Override
public String toString() {
return "MyException{" + "detail=" + detail + '}';
}
}