本文为学习了b站java狂神说的笔记,个人认为,狂神讲解的通俗易懂,很适合大众学习Java,我在这里上传了我学习基础时,跟随课程做的笔记,希望能为您带来知识的进一步理解。(可配合视频进行学习https://www.bilibili.com/video/BV12J41137hu);
java 学习知识点总结
markdown学习
一级标题#+“ ”
二级##
粗体**
斜体*
删除线~
引用>
分割线
三个_或***
图片[名]()
ming
超链接
列表
有序列表1.+空格
无序列表- +空格
表格
1|2|3|
–|--|–|
1|1|1|删除空格
代码```
JAVA基础
Dos命令
1.查看目录文件所有文件dir
2.切换目录 cd /d f:mulu
cd …返回上一级
cd 目录
3.清理屏幕 cls
4.退出 exit
5.查看电脑IP: ipconfig
6.打开应用 calc 计算器
mspaint
hotepad 记事本
ping: ping:www.baidu.com
md test 目录名
cd>a.txt创建txt
del a.txt删除txt
rd 移除 test 目录名
快捷方式
Alt+F4 关闭
ctrl +x 剪切
ctrl+D 删除
windows+r +cmd
windows+e 文件
explorer 桌面
ctrl +s+esc 任务管理器
ctrl+w 关闭当前页面
ctrl+shift+t 恢复
windows+d显示桌面
windows+e打开文件管理器
java基础知识
long与float
long num=1L;
float num1=0.1F;
boolean flag=true 或false;
位 bit
字节byte B 8位 1B=8b
psvm ->public static void main(String[] args){}
sout ->System out println()
文档注释 /**
*@Author 作者
*/
int i=10;
int i2=010;//八进制
int i3=0x10;//十六进制
System out println(i);//10
System out println(i2);//8
System out println(i3);//16
float 有限 离散 舍入误差 大约 接近但不等于
double
float f=0.1f;
double d=1.0/10;
System out println(f==d);//false
float f1=23.2121212121f;
float f2=f1+1;
System out println(f1=f2);//true
最好完全避免使用浮点数进行比较
Unicode表
char c1='a';
char c2='中';
System out println((int)c1);//输出97
/* Unicode表(97=a 65=A)U0000-UFFFF 0-65535 2字节
Excel 2^16=65536
增添Unicode码内容:它前128个字符就是ASCII码,之后是扩展码。*/
char c3='\u0097' //a
类型转换
// byte,short,char->int->long->float->double
//强类型转换 高到低
int i=128;
byte b=(byte) i;//1.内存溢出 -128 取得补码
//自动转换 低到高
double b=i; //128.0
//2.精度问题
System out println((int)23.7); //23
System out println((int)-42.98f); //-42
//3.操作数比较大时,注意溢出
public class o {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
int money=10_0000_0000;
int years=20;
int total=money*years;//-1474836480,计算时溢出了
long total2=money*years;//-1474836480,默认是int,转换之前就已经存在问题
long total3=money*((long)years);//200_0000_0000先把一个数转换为了long型
}
}
变量作用域
public class Variable{
static int allClics=0;//类变量
String str="hello world";//实例变量
public void method(){
int i=0;//局部变量
}
}
————————————————————————————————————————————————————
public class Demo08{
//类变量 static
static double salary=2500;
//常量
//修饰符,不存在先后顺序
final static double PI=3.14;
//属性:变量
//实例变量:从属对象;如果不自行初始化,这个类型的默认值 0 0.0
//布尔值:默认是false
//除了基本类型,其余的默认值都是null;
String name;
int age;
//main方法
public static void main(String[] args) {
//
int i=10;
System.out.println(i);
//变量类型 变量名= new Demo08();
Demo08 demo08=new Demo08();
System.out.println(demo08.age);
System.out.println(demo08.name);
//类变量 static
System.out.println(salary);
System.out.println(PI);
}
}
变量的命名规范
1.所有变量,方法,类命:见名知意
2.类成员变量:首字母小写和驼峰原则:monthSalary 除了第一个单词外,后面的单词首字母大写
3.局部变量:首字母小写和驼峰原则
4.常量:大写字母和下划线:MAX_VALUE
5.类命:首字母大写和驼峰原则:GoodMan
6.方法名:首字母小写和驼峰原则:runRun()
运算符
//优先级:() > 正负号+- >自增自减非 > */% > +- > 位运算 >大小关系 > == != > 按位与& > 按位或| > 逻辑与& > 逻辑或| > 条件运算?: > 赋值运算=、+=、-=、*=、/=、%= > 位赋值运算&=、|=、<<=、>>=、>>>=
package demo;
public class Demo01 {
public static void main(String[] args) {
//数字类型规则
long a=121212121212L;
int b=123;
short c=10;
byte d=2;
System.out.println(a+b+c+d);//double
System.out.println(b+c+d);//int
System.out.println(c+d);//int
//++规则
int a1=3;
int a2=a1++;//a1=4
int a3=++a1;
System.out.println(a2);//3
System.out.println(a3);//5
//幂运算等运算用Math.
double e=Math.pow(2,3);//幂运算
System.out.println(e);//8.0
//逻辑运算
/*
(a&&b) a与 b 两个都为真,结果为true
(a||b) a或 b 两个变量有一个为真,结果为真
!(a&&b) 去结果为反
*/
//位运算
/*
A=0011 1100
B=0000 1101
-----------------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001 异或
~B = 1111 0010 非
2*8=16如何计算最快?2*2*2*2
效率极高!!!
<<左移 *2
>>右移 /2
*/
//扩展运算符
/*
int a=10;
int b=20;
a+=b; //a=a+b
a-=b; //a=a-b
*/
//字符串连接符
/*
//+两侧只要出现String类型都转换成String类型进行连接
System.out.println(""+a+b); //1020
System.out.println(a+b+""); //30
*/
//三元运算符
/*
//x?y:z 如果 x==true,则结果为y,否则结果为z
int score=80;
String type = score<60 ? "不及格":"及格";
System.out.println(type);
*/
}
}
包
1.一般公司域名倒置作为包名 com.ning.www
2.引用其它包 import package.*
JavaDoc
javadoc命令是用来生成自己API文档的
/**
*@author 作者名
*@version 版本号
*@since 知名需要最早使用的jdk版本
*@param 参数名
*@return 返回值情况
*@throws 异常抛出情况
*/
cmd中生成javadoc: javadoc -encoding UTF-8 -charset UTF-8 Doc.java
encoding -> 编码
JAVA流程控制
Scanner
Scanner s=new Scanner(System.in);
String s= scanner.nextline();
scanner.close();
next 和 nextLine 的区别:
next 遇空格结束 ;nextLine遇回车结束
//next方法
package com.ning.demo01;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方法接收:");
//判断用户有没有输入字符串
if (scanner.hasNext()) {
//使用next方式接收
String str = scanner.next();
System.out.println("输出的内容为:" + str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成良好的习惯用完就关掉
//用完关闭
scanner.close();//关闭
}
}
//next 以空白为结束符, nextLine 以回车为结束符
//nextLine方法
package com.ning.demo01;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方法接收:");
//判断是否还有输入
if (scanner.hasNext()) {
String str1 = scanner.nextLine();
System.out.println("输出的内容为:" + str1);
}
scanner.close();
}
}
输入数字类型不同的方法:
package com.ning.demo01;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
int i=0;
float f=0.0f;
if(scanner.hasNextInt()){
i=scanner.nextInt();
System.out.println("输入的整数是:"+i);
}else{
System.out.println("输入的不是整数:");
}
if(scanner.hasNextFloat()){
f=scanner.nextFloat();
System.out.println("输入的小数是:"+f);
}else{
System.out.println("输入的不是小数:");
}
}
}
列题:
//输入多个数字,并求其综合和平均数,每输入一个字用回车确认,通过输入非数字来结束输入并输出执行结果
package com.ning.demo01;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int m=0;//用来记录输入数字的个数
double sum=0;//用来记录输入数字的和
while(scanner.hasNextDouble()){
double x= scanner.nextDouble();
m++;
sum=sum+x;
System.out.println("输入了第"+m+"个数字。"+"数字的和为"+sum);
}
System.out.println(m+"个数字的平均值为:"+(sum/m));
scanner.close();
}
}
结构
顺序结构
选择结构
if - if-else -else
//equals:判断字符串是否相等
package com.ning.demo01;
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String s=scanner.nextLine();
//equals:判断字符串是否相等
if(s.equals("hello")) {
System.out.println("hi");
}else if(s.equals("lxx")){
System.out.println("zjn");
}else{
System.out.println("hh");
}
scanner.close();
}
}
switch
switch(expression){
case value :
//语句
break;//可选
case value :
//语句
break;//可选
//你可以有任意数量的case语句
defult ://可选
//语句
}
jdk7之后可以比较字符串了
package com.ning.demo01;
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
int x=scanner.nextInt();
switch (x){
case 1:
System.out.println("2");
break;
case 2:
System.out.println(3);
case 3:
System.out.println(3);
case 4:
System.out.println(3);
default:
System.out.println(3);
}
scanner.close();
}
}
//case 穿透 case2后没有break 输入2 输出3333
反编译 通过class 导入去看源代码
循环结构
while循环 ,do-while循环 ,for循环
while 与 do-while 区别
1.while先判断后执行,do-while先执行后判断
2.do-while保证循环体会被至少执行一次
for
for (int i = 0; i <= 100; i++) //初始化//条件判断//迭代
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减
100.for -> for (int i = 0; i <= 100; i++)
oddSum 奇数和 evenSum 偶数和
//练习用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个数
package com.ning.demo01;
public class Demo08 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){ //换行
//System.out.println();
System.out.print("\n");
}
}
}
}
print与println的区别
println 输出完换行
print 输出完不换行
增强for循环
package com.ning.demo01;
public class Demo10 {
public static void main(String[] args) {
int[] numbers={1,2,3,4,5};
for (int i = 0; i < 5; i++) {
System.out.print(numbers[i]);
}
System.out.println();
System.out.println("-----------");
//遍历数组元素
for(int x:numbers){
System.out.print(x);
}
}
}
break & continue
break,用于强行退出循环,不执行循环中剩余的语句。
continue,用于中止某次循环过程,及跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判断。
package com.ning.demo01;
public class Demo11 {
public static void main(String[] args) {
int i=0;
while(i<10){
System.out.println(i);
i++;
if(i==3){
break;
}
}
System.out.println("----------");
while(i<100){
i++;
if(i%10==0){
System.out.println();
continue;
}
System.out.print(i+"\t");
}
}
}
goto : 相当于使用标签 //不建议使用
//打印101-150之间所有的质数
package com.ning.demo01;
public class Demo12 {
public static void main(String[] args) {
int count = 0;
outer : for (int i = 101; i < 150; i++) {
for(int j =2;j<i/2;j++){
if(i % j ==0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}
流程控制练习
打印三角形
//打印三角形
package com.ning.Test;
public class TestDemo01 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >=i; j--) {
System.out.print(" ");
}
for(int j = 1;j <= i;j++){
System.out.print("*");
}
for(int j = 1;j < i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
输出:
*
***
*****
*******
*********
java方法详解
方法的定义
JAVA方法是语句的集合,他们在一起执行一个功能。
1.方法是解决一类问题的步骤的有序组合。
2.方法包含于类或对象中
3.方法在程序中被创建,在其他地方被引用
修饰符 返回值类型 方法名(参数类型 参数名){
...
方法体
...
return 返回值;
}
package com.ning.demo02;
public class Demo01 {
//main 方法
public static void main(String[] args) {
//实际参数:实际调用传递给他的参数
int sum=add(1,2);
System.out.println(sum);
}
//加法
//形式参数,用来定义作用的
public static int add(int a, int b) {
return a + b;
}
}
方法的重载
重载就是在一个类中,有相同的函数名称,但形参不同的函数。
方法重载的规则:
1.方法名称必须相同
2.参数列表必须不同(个数不同,或类型不同,参数排列顺序不同等)
3.方法的返回类型可以相同也可以不同
4.仅仅返回类型不同不足以成为方法的重载
实践理论:
方法名称相同时,编译器会根据调用方法的参数个数,参数类型等去逐个匹配,以选择对应的方法,如果匹配失败,则编译器报错。
命令行传参
可变参数
1.在方法声明中,在指定参数类型后加一个省略号(…)。
2.一个方法中只能指定一个可变参数,它必须是方法的最后一个参数。任何普通的参数必须在它之前声明。
package com.ning.demo02;
public class Demo02 {
public static void main(String[] args) {
//调用可变参数的方法
printMax(5,3,6,9,7);
}
public static void printMax(double...numbers){
if(numbers.length==0){
System.out.println("No argument passed");
return;
}
double result = numbers[0];
//排序
for (int i = 1; i < numbers.length; i++) {
if(numbers[i] > result){
result = numbers[i];
}
}
System.out.println("The max value is " + result);
}
}
递归
递归结构包括两个部分:
1.递归头: 什么时候不调用自身的方法,如果没有头,将陷入死循环。
2.递归体: 什么时候需要调用自身方法。
//计算阶层 4!=4*3*2*1
package com.ning.demo02;
public class Demo03 {
public static void main(String[] args) {
System.out.println(f(4));
}
public static int f(int n){
if(n==1){
return 1;
}else{
return n*f(n-1);
}
}
}
练习:计算器
//计算器
package com.ning.demo02;
import com.sun.source.tree.CaseTree;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
String s = scanner.next();
int j = scanner.nextInt();
switch (s){
case("+"):
System.out.println(i+j);
break;
case("-"):
System.out.println(i-j);
break;
case("*"):
System.out.println(i*j);
break;
case("/"):
System.out.println(i/j);
break;
}
scanner.close();
}
}
数组
1.数组是相同类型数据的有序集合
2.数组描述的是相同类型的若干个数据,按照一定的先后顺序排列组合而成
3.其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问他们
int[] nums;//习惯用
int nums[];
获取数组长度: arrays.length
package com.ning.demo02;
public class Demo05 {
public static void main(String[] args) {
int[] nums;//1.定义一个数组,常用
//int nums[];
nums = new int[10];//2.创建一个数组
nums[0]=0;//3.给数组元素中赋值
}
}
数组下标越界:java.lang.ArrayIndexOutOfBoundsException
java内存:
1.堆:
存放new的对象和数组;
可以被所有的线程共享,不会存放别的对象引用
2.栈:
存放基本变量类型(会包含这个基本类型的具体数值);
引用对象的变量(会存放这个引用在堆里面的具体地址)
3.方法区:
可以被所有的线程共享;
包含了所有的class和static变量
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rtF3WssT-1616834535068)(C:\Users\朱吉宁\AppData\Roaming\Typora\typora-user-images\image-20210112204349549.png)]
package com.ning.demo02;
public class Demo06 {
public static void main(String[] args) {
//静态初始化: 创建 + 赋值
int[] a={1,2,3,4,5};
//引用类型
//int mans = {new Man(),new Men()};
//动态初始化 : 包含默认初始化
int[] b = new int[10];
b[0] = 0;
}
}
数组的使用
1.普通的For循环
2.For-Each循环
3.数组做方法入参
4.数组做返回值
package com.ning.demo02;
public class Demo07 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
//jdk1.5 没有下标
for(int array : arrays){
System.out.print(array+" ");//1,2,3,4,5
}
int[] reverse = reverse(arrays);
printArray(arrays);
printArray(reverse);
}
//反转数组
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
//反转操作
for (int i = 0,j = result.length-1; i < result.length; i++,j--) {
result[j] = arrays[i];
}
return result;
}
//打印数组元素
public static void printArray(int[] arrays){
System.out.println();
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
}
}
多维数组
package com.ning.demo02;
public class Demo08 {
public static void main(String[] args) {
//[4][2]
/*
1,2 array[0]
2,3 array[1]
3,4 array[2]
4,5 array[3]
*/
int[][] array = {{1,2},{2,3},{3,4},{4,5}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]+" ");
}
}
}
}
Arrays类
Arrays类中的方法都是static修饰的静态方法,在使用的时候可以直接使用类名进行调用,而“不用”使用对象来调用(注意:是“不用”而不是“不能”)
具有以下常用功能:
1.给数组赋值:通过 fill 方法
2.对数组排序: 通过 sort 方法,按升序
3.比较数组: 通过 equals 方法比较数组中元素值是否相等
4.查找数组元素: 通过 binarySearch 方法能对排序好的数组进行二分查找法操作
package com.ning.demo02;
import java.util.Arrays;
public class Demo09 {
public static void main(String[] args) {
int[] a = {1,2,3,4,999,123,546};
//打印数组元素Arrays.toString()
System.out.println(Arrays.toString(a));//按住CTRL,点击toString可查看代码
//[1, 2, 3, 4, 999, 123, 546]
printArray(a);
//[1, 2, 3, 4, 999, 123, 546]
//数组进行排序
Arrays.sort(a);
System.out.println(Arrays.toString(a));//升序排序
//[1, 2, 3, 4, 123, 546, 999]
//数组填充
Arrays.fill(a,2,4,0);//2,4之间进行填充,改变a[2],a[3]
System.out.println(Arrays.toString(a));
//[1, 2, 0, 0, 123, 546, 999]
}
public static void printArray(int[] a){
for (int i = 0; i < a.length-1; i++) {
if(i==0){
System.out.print("[");
}
System.out.print(a[i] + ", ");
}
System.out.println(a[a.length-1] + "]");
}
}
冒泡排序
/*
1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置
2.每一次比较,都会产生出一个最大,或者最小的数字
3.下一轮比较则可以减少一次排序
4.依次循环,直到结束
*/
package com.ning.demo02;
import java.util.Arrays;
public class Demo10 {
public static void main(String[] args) {
int[] a = {2,8,7,65,4,5,7,8,9};
int[] sort = sort(a);//调用完我们写的排序方法后,返回一个排序后的数组
System.out.println(Arrays.toString(sort));
}
public static int[] sort(int[] array){
//临时变量
int temp = 0;
//外层循环,判断我们这个要走多少次
for (int i = 0; i < array.length-1; i++) {
boolean flag = false;//通过flag标识位减少没有意义的比较
//内层循环,比较判断两个数并交换位置
for (int j = 0; j < array.length-1-i; j++) {
if(array[j+1]<array[j]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = true;
}
}
if(flag==false){
break;
}
}
return array;
}
}
稀疏数组
当一个数组中大部分元素为0,或者为同一值的数组时,可以用稀疏数组来保存该数组
稀疏数组的处理方式:
1.记录数组一共有几行几列,有多少个不同值
2.把具有不同值的元素和行列机制记录在一个小规模的数组中,从而所小程序的规模
package com.ning.demo02;
public class Demo11 {
public static void main(String[] args) {
//1.创建一个二维数组11*11, 0:没有棋子 1:黑棋 2:白棋
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
//输出原始数组
System.out.println("请输出原始的数组:");
for(int[] ints : array1){//array1.for
for(int anInt : ints){//ints.for
System.out.print(anInt+"\t");
}
System.out.println();
}
System.out.println("--------------");
//转换为稀疏数组保存
//获取有效值个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if(array1[i][j]!=0){
sum++;
}
}
}
System.out.println("有效值个数:"+sum);
//2.创建一个稀疏数组的数组
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 i = 0; i < array2.length; i++) {
System.out.println(array2[i][0]+"\t"
+array2[i][1]+"\t"
+array2[i][2]+"\t"
);
}
System.out.println("-----------------");
//还原稀疏数组
System.out.println("还原稀疏数组");
//1.读取稀疏数组
int[][] array3 = new int[array2[0][0]][array2[0][1]];
//2.将数值还原给array3
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
//3.打印
for(int[] ints : array3){//array3.for
for(int anInt : ints){//ints.for
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}
请输出原始的数组:
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
--------------
有效值个数:2
输出稀疏数组:
11 11 2
1 2 1
2 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
进程已结束,退出代码0
回顾方法及加深
方法的定义
1.修饰符
2.返回类型
3.break 和 return
4.方法名: 注意规范(驼峰法) 见名知意
5.参数列表: (参数类型,参数名)…
6.异常抛出
package com.ning.demo03;
import java.io.IOException;
//Demo01 类
public class Demo01 {
//main方法
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(...){
//方法体
return 返回值
}
*/
//break:跳出switch,结束循环
//return:结束方法,返回一个结果
public String sayHello(){
return ("hello world");
}
public void hello(){
return;
}
public int max(int a,int b){
return a>b ? a : b;//三元运算符
}
//数组下标越界: Arrayindexoutofbounds
public void readFile(String file) throws IOException{
}
}
方法的调用: 递归
1.静态方法
2.非静态方法
package com.ning.demo03;
import java.security.PublicKey;
public class Demo02 {
public static void main(String[] args) {
//静态方法 static
Student.say();
//非静态方法
//实例化这个类 new
//对象类型 对象名 = 对象值
Student student = new Student();
student.say();
/*
//static 和类一起加载的
public static void a(){
b();//报错
}
//类实例化 之后才存在
public void b(){
}
*/
}
}
package com.ning.demo03;
//学生类
public class Student {
//静态方法
public static void say(){
System.out.println("说话。");
}
//非静态方法
public static void say1(){
System.out.println("说话。");
}
}
3.形参与实参
package com.ning.demo03;
public class Demo03 {
public static void main(String[] args) {
//实际参数和形式参数的类型要对应
int add = new Demo03().add(1,2);//实际参数
System.out.println(add);
}
public int add(int a,int b){//形式参数
return a+b;
}
}
4.值传递和引用传递
package com.ning.demo03;
//值传递
public class Demo04 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);
Demo04.change(a);
System.out.println(a);//值为1
}
//返回值为空
public static void change(int a){
a = 10;
}
}
package com.ning.demo03;
//引用传递: 对象,本质还是值传递
//
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);//null
Demo05.change(person);
System.out.println(person.name);
}
public static void change(Person person) {
//person是一个对象,指向的--->Person person = new Person();这是一个具体的人,可以改变属性
person.name = "me";
}
}
//定义了一个Person类,有一个属性:name
class Person{
String name;//null
}
5.this关键字
面向对象编程(Object-Oriented Programming,OOP)
对于描述复杂的事物,为了从宏观上把握,从整体上合理分析,我们需要使用面向对象的思路来分析整个系统。但是,具体到微观操作,仍需要面向过程的思路去处理。
面向对象的本质就是:以类的方式组织代码,以对象的组织(封装)数据。
三大特性:封装,继承,多态
类与对象的关系
类是一种抽象的数据类型,它是对某一类事物整体描述或定义,但并不能代表一个具体的事物。
对象是抽象概念的具体实例
创建与初始化对象
package com.ning.demo04;
//一个项目应该只存一个main方法
public class Application {
public static void main(String[] args) {
//类: 抽象的,实例化
//类实力恶化后会返回一个自己的对象
//student对象就是一个Student类的具体实例
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaoming.name = "小明";
xiaoming.age = 3;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
xiaohong.name = "小红";
xiaohong.age = 3;
System.out.println(xiaohong.name);
System.out.println(xiaohong.age);
}
}
package com.ning.demo04;
//学生类
public class Student {
//属性:字段
String name;
int age;
//
public void study(){
System.out.println(this.name+"studying");
}
}
构造器
类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的,并且构造器有以下两个特点:
1.必须和类的名字相同
2.必须没有返回类型,也不能写void
package com.ning.demo04;
//一个项目应该只存一个main方法
public class Application {
public static void main(String[] args) {
//new 实例化一个对象
Person person = new Person();//调用public Person()
//Person person = new Person(String name);//调用public Person(String name)
System.out.println(person.name);
}
}
package com.ning.demo04;
public class Person {
//一个类即使什么都不写,它也会存在一个方法
//显示的定义构造器
String name;
//实例化初始值
//1.使用new关键字, 本质是调用构造器
//2.构造器用来初始化值
public Person(){
this.name = "me";
}
//有参构造:一旦定义了有参构造,无参就必须显示定义
public Person(String name){
this.name = name;
}
//alt + insert 自动生成构造器
}
/*
构造器特点:
1.和类名相同
2.没有返回值
作用:
1.new 本质是再调用构造方法
2.初始化对象的值
注意点:
1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造
alt+ insert
this.
*/
创建对象内存分析
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MNAftMf4-1616834535082)(C:\Users\朱吉宁\AppData\Roaming\Typora\typora-user-images\image-20210119220928135.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mCMXIZVK-1616834535086)(C:\Users\朱吉宁\AppData\Roaming\Typora\typora-user-images\image-20210119221043314.png)]
类与对象小结
package com.ning.demo04;
//一个项目应该只存一个main方法
public class Application {
public static void main(String[] args) {
/*
1. 类与对象
类是一个模板: 抽象,对象是一个具体的实例
2. 方法
定义,调用
3. 对应的引用
引用类型: 基本类型(8)
对象是通过引用来操作的: 栈--->堆
4. 属性: 字段Field 成员变量
默认初始化:
数字: 0 0.0
char: u0000
boolean: false
引用: null
修饰符 属性类型 属性名 = 属性值
5. 对象的创建和使用
- 必须使用new 关键字创造对象,构造器 Person ning = new Person();
- 对象的属性 ning.name
- 对象的方法 ning.sleep()
6. 类:
静态的属性 属性
动态的行为 方法
*/
}
}
封装
设计程序追求:高内聚,低耦合。高内聚:就是类的内部数据操作细节自己就完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用
封装(数据的隐蔽):通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏
属性私有, get/set
package com.ning.demo05;
/*
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.提高系统的可维护性
*/
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.setName("ning");
System.out.println(student.getName());
student.setAge(999);//不合法,输出0
System.out.println(student.getAge());
}
}
package com.ning.demo05;
//类 private: 私有
public class Student {
//属性私有
private String name;
private int id;
private char sex;
private int age;
//get,set提供一些可以操作这个属性的方法
//提供一些public 的get,set方法
//get获得这个数据
public String getName(){
return this.name;
}
//set给这个数据设置值
public void setName(String name){
this.name = name;
}
//alt +insert
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 100 || age < 0) {
this.age = 0;
} else {
this.age = age;
}
}
}
继承
继承的本质是对某一批类的抽象
extends的意思是“扩展”,字类是父类的扩展
java中类只有单继承,没有多继承!一个儿子只能有一个父亲,但是一个父亲可以有多个子类
继承是类和类之间的一种关系,除此之外,类和类之间的关系还有依赖,组合,聚合等
继承关系的两个类,一个位子类(派生类),一个为父类(基类),子类继承父类,使用关键字extends来表示
子类和父类之间,从意义上讲应该具有“is a”的关系
object类
super: this调用子类,super调用父类
方法重写
继承:
package com.ning.demo06;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
System.out.println(student.getMoney());
}
}
package com.ning.demo06;
//学生 is 人 : 派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends People {
//Ctrl + H 全文件搜索
//Ctrl + O 查看本类中所有方法
//Ctrl + F3 查看父类方法
}
package com.ning.demo06;
//在JAVA中,所有的类,都默认直接或者间接继承Object类
//Person 人:父类
public class People {
//public
//protected
//default
//private
private int money = 10_0000_0000;
public void say(){
System.out.println("say");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
super:
package com.ning.demo06;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("NING");
student.test1();
}
}
package com.ning.demo06;
//学生 is 人 : 派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends People {
public Student(){
//隐藏代码(可以不写): 调用了父类的无参构造
super();//调用构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
private String name = "ning";
public void print(){
System.out.println("Student");
}
public void test(String name){
System.out.println(name);//NING
System.out.println(this.name);//ning
System.out.println(super.name);//宁
}
public void test1(){
print();//Student
this.print();//Student
super.print();//People
}
}
package com.ning.demo06;
//在JAVA中,所有的类,都默认直接或者间接继承Object类
//Person 人:父类
public class People {
public People(){
System.out.println("People无参执行了");
}
protected String name = "宁";
//私有的东西无法被继承
public void print(){
System.out.println("People");
}
}
super注意点:
-
super调用父类的构造方法,必须在构造方法的死一个
-
super必须只能出现在子类的方法或者构造方法中
-
super和this 不能同时调用构造方法
Vs this:
代表的对象不同:
this: 本身调用着这个对象
super: 代表父类对象的应用
前提:
this: 没有继承也可以使用
super: 只能在继承条件才可以使用
构造方法:
this() ; 本类的构造
super() ; 父类的构造
方法的重写 -->多态
package com.ning.demo07;
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()
}
}
package com.ning.demo07;
//继承
public class A extends B {
public static void test() {
System.out.println("A-->test()");
}
}
package com.ning.demo07;
//重写都是方法的重写,和属性无关
public class B {
public static void test(){
System.out.println("B-->test()");
}
}
重写:需要有继承关系,字类重写父类的方法
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大: public>protected>default>private
4.抛出的异常: 范围, 可以被缩小, 但是不能扩大; ClassNotFoundException --> Exception(大)
重写,子类的方法和父类必须要一致;方法体不同。
为什么要重写:
1.父类的功能,子类不一定需要,或者不一定满足
Alt +Insert: override
package com.ning.demo07;
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()
}
}
//静态方法是类的方法,而非静态是对象的方法,即b是A new出来的对象,因此调用了A的方法
package com.ning.demo07;
//继承
public class A extends B {
//Override 重写
@Override//注释:有功能的注释
public void test() {
System.out.println("A-->test" );
}
}
package com.ning.demo07;
//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B-->test()");
}
}
多态
1.即同一方法可以根据发送对象不同而采用多种不同的行为方式1.
2.一个对象是的实际类型是确定的,但可以指向对象的引用的类型有很多(父类,有关系的类)
3.多态存在的条件:
1.有继承关系
2.子类重写父类的方法
3.父类引用指向子类对象
4.注意:多态是方法的多态,属性没有多态性
5.instanceof (类型转换) 引用类型
package com.ning.demo08;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了: 父类的引用指向了子类
//Student 能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person 父类型,可以指向子类,但不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
s1.run();//son
s2.run();//son 子类重写了父类的方法
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
s1.eat();//eat
//s2.eat();不能执行,父类里没用此方法
((Student)s2).eat();//强制转换成Student类型,高转低
}
}
package com.ning.demo08;
public class Person {
public void run(){
System.out.println("run");
}
}
package com.ning.demo08;
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 强行转换报 类型转换异常 ClassCastException
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象 Father f1 = new Son();
4.不能重写的方法:
1.static 方法,属于类,它不属于实例
2.final 常量
3.private方法
instanceof
System.out.println(X instanceof Y);
//能不能编译通过,x与y是否有父子关系,输出true 或 false
Person obj = new Student();
//高转低 ,强制转换
//student将这个对象转为Student类型,我们就可以调用Student类型方法了
Student student = (Student) obj;
student.do();
//或者 ((Student)obj).go();
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型:直接转换 Person person =student; 可能会丢失一些方法
3.把父类转换为子类,向下转型:强制转换
4.方便方法的调用,减少重复的代码
static详解
package com.ning.demo12;
public class Student {
//2:赋初始值~
{
System.out.println("匿名代码块");
}
//1:只执行一次
static{
System.out.println("静态代码块");
}
//3
public Student(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Student student1 = new Student();
System.out.println("==========");
Student student2 = new Student();
}
}
输出:
静态代码块
匿名代码块
构造方法
==========
匿名代码块
构造方法
进程已结束,退出代码0
//静态导入包
import static java.lang.Math.random
抽象类
package com.ning.demo09;
//抽象类的所有方法,继承了他的子类,都必须要实现他的方法,除非这类也是抽象
public class A extends Action {
@Override
public void doSomething() {
}
}
package com.ning.demo09;
//abstract 抽象类:类 extends: 单继承 (接口可以实现多继承)
public abstract class Action {
//约束 有人来帮我们实现
//abstract , 抽象方法,只有方法名字,没有方法的实现
public abstract void doSomething();
//1.不能new这个抽象类,只能靠子类去实现他:约束
//2.抽象类中可以写普通方法
//3.抽象方法必须在抽象类中
//抽象的抽象: 约束
//提高开发效率
}
接口
普通类:只有具体实现
抽象类:具体实现和规范(抽象方法)都有
接口:只有规范!自己无法写方法专业的约束!约束和实现分离:面向接口编程
接口的本质是契约。
package com.ning.demo10;
public interface TimeServer {
}
package com.ning.demo10;
//interface 定义的关键字 , 接口都需要有实现类
public interface UserService {
//常量~ public static final
int age = 10;
//接口中的所有定义其实都是抽象的 public abstract
void add(String name);
void delete(String name);
void update(String name);
void select(String name);
}
package com.ning.demo10;
//抽象类: extends~
//类 可以实现接口 implements 接口
//实现了接口的类,就需要重写接口中的方法
//多继承~利用接口实现多继承
public class UserServiceImpl implements UserService,TimeServer {
@Override
public void add(String name) {
}
@Override
public void update(String name) {
}
@Override
public void select(String name) {
}
@Override
public void delete(String name) {
}
}
作用:
1.约束
2.定义一些方法,让不同的人实现~
3.public abstract
4.public static final
5.接口不能被实例化,接口中没有构造方法
6.implement可以实现多个接口
7.必须要重写接口中的方法
N种内部类
package com.ning.demo11;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性
public void getID(){
System.out.println(id);
}
}
//局部内部类
public void method(){
class Inner{
public void in(){
}
}
}
}
//一个java类中可以有多个class类,但只能有一个public class
class A{
}
package com.ning.demo11;
public class Application {
public static void main(String[] args) {
//new
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
inner.getID();
}
}
=========================================
package com.ning.demo11;
public class Test {
public static void main(String[] args) {
//没有名字初始化类,不用将实例保存到变量中
new Apple().eat();
UserService userService = new UserService(){
@Override
public void hello() {
}
};
}
}
class Apple{
public void eat(){
System.out.println("eat");
}
}
interface UserService{
void hello();
}
异常
Exception 异常
检查性异常
运行时异常
错误ERROR
抛出异常
捕获异常
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-01w8vXSV-1616834535090)(C:\Users\朱吉宁\AppData\Roaming\Typora\typora-user-images\image-20210325093625983.png)]
异常处理五个关键字:try,catch,finally,throw,throws
package com.ning.Exception;
import java.io.DataOutputStream;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
//捕获异常要从小到大
try{//try监控区域
if(b==0){//主动抛出异常 throw 假设这个方法中,处理不了这个异常,方法上抛出异常
throw new ArithmeticException();//主动抛出异常,一般在方法中使用
}
System.out.println(a/b);
}catch (Error e){
System.out.println("Error");
}
catch (Exception e){
System.out.println("Exception");//执行
}
catch (Throwable e){
System.out.println("Throwable");
}
finally {
System.out.println("finally");
}
//finally 可以不要finally, 假设IO,资源的关闭
try{//try监控区域
new Test().a();
}catch (Throwable e){//catch(想要捕获的异常类型!) 捕获异常
System.out.println("程序出现异常!");
}finally {
System.out.println("finally");
}
}
public void a(){
b();
}
public void b(){
a();
}
}
快捷键: CTRL alt t 选中输出项
自定义异常
package com.ning.Exception;
//自定义的异常类
public class MyException extends Exception {
//传递数字》10
private int detail;
public MyException(int a){
this.detail = a;
}
//toString:异常的打印信息
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
package com.ning.Exception;
import com.sun.jdi.PathSearchingVirtualMachine;
public class Test1 {
//可能会存在异常的方法
static void test(int a) throws Exception{
System.out.println("传递的参数为:"+a);
if(a>10){
throw new MyException(a);//抛出
}
System.out.println("OK");
}
public static void main(String[] args) {
try {
test(11);
} catch (Exception e) {
//增加一些处理异常的代码块
System.out.println("MyExcetion=>"+e);
}
}
}
输出结果:
传递的参数为:11
MyExcetion=>MyException{detail=11}
ASCALL表
常用字符与ASCII代码对照表
为了便于查询,以下列出ASCII码表:第128~255号为扩展字符(不常用)
ASCII码 | 键盘 | ASCII 码 | 键盘 | ASCII 码 | 键盘 | ASCII 码 | 键盘 |
---|---|---|---|---|---|---|---|
27 | ESC | 32 | SPACE | 33 | ! | 34 | " |
35 | # | 36 | $ | 37 | % | 38 | & |
39 | ’ | 40 | ( | 41 | ) | 42 | * |
43 | + | 44 | ’ | 45 | - | 46 | . |
47 | / | 48 | 0 | 49 | 1 | 50 | 2 |
51 | 3 | 52 | 4 | 53 | 5 | 54 | 6 |
55 | 7 | 56 | 8 | 57 | 9 | 58 | : |
59 | ; | 60 | < | 61 | = | 62 | > |
63 | ? | 64 | @ | 65 | A | 66 | B |
67 | C | 68 | D | 69 | E | 70 | F |
71 | G | 72 | H | 73 | I | 74 | J |
75 | K | 76 | L | 77 | M | 78 | N |
79 | O | 80 | P | 81 | Q | 82 | R |
83 | S | 84 | T | 85 | U | 86 | V |
87 | W | 88 | X | 89 | Y | 90 | Z |
91 | [ | 92 | \ | 93 | ] | 94 | ^ |
95 | _ | 96 | ` | 97 | a | 98 | b |
99 | c | 100 | d | 101 | e | 102 | f |
103 | g | 104 | h | 105 | i | 106 | j |
107 | k | 108 | l | 109 | m | 110 | n |
111 | o | 112 | p | 113 | q | 114 | r |
115 | s | 116 | t | 117 | u | 118 | v |
119 | w | 120 | x | 121 | y | 122 | z |
123 | { | 124 | | | 125 | } | 126 | ~ |