java基础
快捷键
shift+delete 永久删除,windows + r cmd命令,+e 打开我的电脑
ALT+f4 关闭窗口,ctrl +x 剪切,shift+ctrl +esc 任务管理器
截图工具 windows + shift +s
任意地方按住shift + 右键打开命令行窗口 powershell,
已管理员方式打开,在右下角
常用的dos (磁盘操作系统)命令
#盘符切换c:,d:
#查看当前目录下的所有文件 dir
#切换目录 cd change directory
#返回上一层 cd..
#清理屏幕cls(clear screen)
#退出终端 exit
#查看ip ipconfig
#打开应用
calc
mspaint
notepad
#ping命令,ping www.baidu.com
md:make directory
cd/d 进入当前文件
cd>文件名
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FWGKlXpU-1636275536112)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1631417267393.png)]
java文件,后缀名为.java
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D9c00zMx-1636275536129)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1631423351310.png)]
public class hello{
public static void main(String[] args){
System.out.print("Hello,WORLD");
}
}
- 从文件位置头进cmd
- 代码public class 后与文本名字一样,即类与文件名必须一致
- javac进行编译 带后缀.java
- java 直接运行
-
大小写敏感
-
sting 字符串
基本数据类型
-
整数 byte 1字节(-128——127内的数 ),short 2,int 4,long 8
-
浮点类型 float 4,double 8
-
字符 char 2
- 位 bit 计算机 内部数据 存储的最小单位
- 字节 计算机 数据处理 的基本单位 ,B
- 1B = 8 bit,1B = 00000001bit
- 字符 计算机中的 字母 数字 符号
字符与字符串的-差别
char name = ‘六’;
char mame3 = ‘百’;
String name1 = “六百”;
进制
二进制 0b,八进制 0 十六进制 0x
不要用浮点型比较,有舍入误差,float double xxxxxxxxx
银行用,BigDecimal 数学工具类
数字间可以加下划线 100_000_000;
强制类型转换
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qp2ktHTO-1636275536136)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1631450096146.png)]
编码 Unicode char = c3 = ’\u0061‘
打印出 ,a;
-
不能对布尔值转换
-
可能存在内存溢出,小数精度
-
都是高到底,int 到byte 低到高不用
char c = 'a'; int d = c+1; 输出结果,d = 98; (char)d = b;
int money = 1000000000; int years = 20; int total = money*years;//不行,数据溢出 long total = money*years;//不行,默认是int 在转换前就已经溢出了 long total = money*((long)years);//在计算前进行转换ok
转义字符
制表符\t
换行\n
String sa = new String(original:"hello");
String sb = new String(original:"hello");
String sc = "hello";
String sd = "hello";
sa != sb,但 sc == sd
布尔值
boolean flag = ture;
if(flag == ture){}
if(flag){} //效果一样,代码要精简
变量
static 类变量 static
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SeQJM5n3-1636275536142)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1631453512246.png)]
final
常量,不变
- 类成员变量 ,首字母小写 驼峰原则 lastName,
- 常量 final 大写加_
- 类名,首字母大写与 驼峰原则 HelloWorld
- 方法名 runRun()
ctrl +d,复制当前行到下一行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HQN4IEQW-1636275536148)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1631496711976.png)]
幂运算,工具类 ,Math.pow(3,2)
字符串链接
int a = 10;
int b = 20;
System.out.println(""+a+b);
System.out.println(a+b+"");
结果第一行为,,1020,被链接,二行为,30
包的导入
package com.operation;
import com.kuang.*/全部导入;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hExWvV5J-1636275536151)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1631531269767.png)]
byte 运算时被转换成 int
byte a = byte b + byte c 是错误的
反编译
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ddXSvhWE-1636275536154)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1631670174033.png)]
在 show in explorer 里直接加文件
数组
Int 【】 num = new int【10】,声明时并不存在,创建时才存在;数组在栈里
关于new 的东西,都在堆里面
类与数组
public class homeWork {
public static void main(String[] args) {
Student[] student = new Student[20];
//Student student1 = new Student();
for (int i = 0; i < 20; i++) {
//两者差不多
student[i] = new Student((int)(1+Math.random()*(20-1+1)),(int)(1+Math.random()*(9-1+1)),(int)(50+Math.random()*(100-50+1)));
// Student s = new Student((int)(1+Math.random()*(20-1+1)),(int)(1+Math.random()*(9-1+1)),(int)(50+Math.random()*(100-50+1)));
//这段并没有什么意义,只是在此赋值
student[i].state = (int)(1+Math.random()*(9-1+1));
student[i].number = (int)(1+Math.random()*(20-1+1));
student[i].score = (int)(50+Math.random()*(100-50+1));
} }}
class Student{
public int number=19063216;
public int state=19;
public int score=100;//
public Student() {//
System.out.println("我的属性"+number+" "+state+" "+score);//
System.out.println();// }
public Student(int number, int state, int score) {
this.number = number;
this.state = state;
this.score = score;
if(this.state==3) {
System.out.println("学生属性:年级"+state+'\n'+"学号"+number+" "+"成绩"+score); System.out.println(); }
}}
易错小知识点
-
byte 运算时被转换成 int
byte a = byte b + byte c 是错误的,包括char
面试
位运算
计算2*8的快速方法,,== 2<<3
直接用位(二进制)运算,直连底层
属性加方法(C语言函数)等于类
继承
java 只有单继承,没有多继承,一个儿子只有一个爸爸;
object 都默认继承。======== Ctrl + h 查看继承图
super 与 this
-
super 调用父亲的构造方法(alt + insert),必须在构造方法第一个
-
super 必须只能出现在子类的方法或者构造方法之中
-
super和this 不能同时使用构造方法
this 本身的对象
super 代表父类对象的应用;只能在继承条件中使用
方法重写
-
需要继承关系,非static 状态下进行(Ait + Inseat)
-
方法名相同
-
参数列表,(否则就变为重载了,而不是重写)
-
修饰符:范围可以变大,但不能小。Public》 Protected》Default》Private
-
抛出的异常,范围可以被变小,但不能大
-
子类的方法与父类一致,方法体不同,即内容改变
为什么需要,父类的功能,子类不一定满足
多态
多态是方法的多态
public void main(String[] args) { //Person 与 Student的不同即是多态 Person s1 = new Student(); Student s2 = new Student(); s1.run(); //关于方法,子可调父,反之不可 // 两方法 名一样。有 static(类),,final(常量),,,private(方法) 则永远是父的方法,不能继承。 反之则子 //子类重回父类
instanceof 与类型转换
instanceof 判断两个类是否有继承关系,后再进行转换。
- 父类引用指向子类的对象。
- 子类-》父类,下-》上。
- 父–》子,向下。强制转换
- 方便方法的调用,减少重复的代码
–static–
package com.kuang.manyTai;import static java.lang.Math.random; //import java.lang.Math.random;//想省前面的math 必须,加上 static public class Static { public static void main(String[] args) { System.out.println(random()); } }
抽象类
- 抽象类的所有方法,有继承了它的子类去实现
- 不能 new 这个抽象类,只能靠子类去实现: 约束 如同 人间的法律
- 抽象类 中 可以 写 普通的方法,但抽象的方法必须写在抽象类里
- 抽象的抽象
接口 interface
public class UserSeriveimpl implements UserService { // 约束 //定义一些方法,interface中所有方法 //都是抽象类 abstract 所有常量都是 //public static final的 //接口不能被实例化。。。接口也没有构造方法,如抽象类 //implement 可以实现多个接口 //必须重写接口中的方法
-
异常
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pHbFZ8aP-1636275536157)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1632048419789.png)]
Ctrl + Ait + T;出现
try 监控区域,,catch 捕获异常 finally 善后工作 throw /s 抛出异常
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7KvrpiSC-1636275536160)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1632048483323.png)]
throws 后异常扔存在,只是先不管他,继续运行,仍被catch到
public static void main(String[] args) {
try{ method();}
catch (IOException e)
{
System.out.println("NN");
return;
}
finally {
System.out.println("sssss");
}
}
public static void method() throws IOException{
throw new IOException();
}
}
get 和 set的使用
public class get {
public static void main(String[] args) {
set set = new set();
set.setAge(10);
System.out.println(set.getAge());
}}
//sout==================================
public class set {
public String getStudent() {
return Student;
}
public void setStudent(String student) {
Student = student;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String Student;
private int age;
小结
- 类与对象 类是一个模板:抽象,对象是一个具体的实例
- 方法:定义,调用
- 对应的引用 引用类型: 基本类型(8)种 对象是通过引用来操作的: 栈—》堆
- 属性 :字段 fiel 成员变量 , 默认初始化:数字 = 0 0.0 ; char : u0000 boolean false 引用 null
- 对象创建与使用 用 new : Person me = new Person ; 对象的属性 me. name 对象的方法 me. say();
- 类 静态的属性 属性 动态的行为 方法