JDK、JRE、JVM

https://www.jetbrains.com/zh-cn/idea/download/ 下载连接
注释
//单行注释
//输出一个hello world
/*
* 块注释 注释多行
* */
/**
* @Dsecription helloworld
* @Author lxf
*/
标识符
关键字
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t0ixQFZW-1650418499891)(../../图片/typora/1650417569316.png)]](https://img-blog.csdnimg.cn/5086076b19724029be1d6395471c4a29.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_15,color_FFFFFF,t_70,g_se,x_16)
数据类型-强类型语言
基本类型
public class Demo02 {
public static void main(String[] args) {
//8大基本数据
int num1 = 1;
byte num2 = 20;
short num3 = 3;
long num4 = 4L;
//小数
float mun5 = 5;
double num6 = 6;
//字符
char name = 'a';
//字符串
String a="hellowrold";
//布尔值
boolean flag = true ;
}
}
public class Demo03 {
public static void main(String[] args) {
//整数进制 二进制ob 十进制 八进制o 十六禁止0x
int i = 10;
int i2 = 010;
int i3 = 0x10;
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("=====================");
//=============================================
//银行业务 BigDecimal 数学类工具
//float 有限 离散 舍入误差 大约 接近但不等于
// double
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);
float d1 = 232222222222222222F;
float d2 = d1 + 1;
System.out.println(d1==d2); // true
//=================================================
//字符
//==================================================
System.out.println("=====================");
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);
System.out.println(c2);
System.out.println((int) c2);
//所有字符都是数字
//编码 unicode 表 :(a=97) 2字节 0-65535 Excel 2^16=65535
//U000 UFFFF
char c3 ='\u0061';
System.out.println(c3);
//转移字符
//\t 制表符
//\n 换行
//。。
System.out.println("=====================");
}
}
public class Demo04 {
public static void main(String[] args) {
int i = 128 ;
byte b = (byte)i ;
//强制转换 高精度 到 低精度
//自动转换 低精度 到 高精度
System.out.println(i);
System.out.println(b);
/*
* 1.不能对布尔类型强制转换
* 2.不能把对象类型转换为不相干的类型
* */
System.out.println("=============================");
System.out.println((int)23.7);
System.out.println((int)-45.67f);
System.out.println("=============================");
char c = 'a';
int d = c + 1;
System.out.println(d);
System.out.println((char) d);
}
}
引用类型
变量规范
运算符
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123123123123L;
int b = 123 ;
short c = 10 ;
byte d = 8 ;
System.out.println(a+b+c+d);
System.out.println(b+c+d);
// System.out.println((String)(c+d));
System.out.println(Math.pow(2,3));
System.out.println(++d);
System.out.println(d++);
System.out.println(d);
int q = 10 ;
int w = 20 ;
int e = 5 ;
//字符串连接 + String
System.out.println(""+q+w);
System.out.println(q+w+"");
System.out.println(q<w?"及格":"不及格");
System.out.println(q+w*e);
}
}
包
一般使用公司域名的倒置作为包名
package com.lxf.operator.base;
import com.lxf.base.*;
javadoc\
方法1 命令模式 javadoc -encoding UTF-8 -charset UTF-8 Doc.java

方式2 通过IDEA
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0QzmKciN-1650418499892)(../../图片/typora/1649307307142.png)]](https://img-blog.csdnimg.cn/958ecd32d2c64056889888a628e48bb3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_8,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fxKIvkv6-1650418499893)(../../图片/typora/1649307325277.png)]](https://img-blog.csdnimg.cn/7844462cd8174c409fbc7eec840b7c9e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_12,color_FFFFFF,t_70,g_se,x_16)
Scanner
package com.lxf.ui;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//输入多个数据并求和 多个数据之间通过回车确认 输入非数字结束输入并输出结果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0 ;
//数据个数
int m = 0 ;
//通过循环判断是否有输入,并计算结果
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m + 1 ;
sum = sum + x;
}
System.out.println(m+"个数字的和为:"+ sum);
System.out.println(m+"个数字的平均数为:"+ (sum/m));
scanner.close();
}
}
if
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()){
int a = scanner.nextInt();
System.out.println("你输入的为:"+ a );
}
switch
char grade = 'C';
switch (grade){
case 'A' :
System.out.println("优秀");
break;
case 'B' :
System.out.println("良好");
break;
case 'C' :
System.out.println("及格");
break;
default:
System.out.println("未知");
}
反编译

循环结构
while
int i = 0 ;
while ( i < 100 ){
++i ;
System.out.println(i);
}
do while
for
for (int i = 0 ; i < 100 ; i++){
System.out.println(i);
}
public class IfDemo02 {
public static void main(String[] args) {
/*
打印9*9乘法表
*/
for (int i = 1; i < 10; i++) {
for (int j = i; j < 10; j++) {
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
for (int i = 1; i < 10; i++) {
for (int j=1; j <= i; j++) {
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SBkPfBTu-1650418499894)(../../图片/typora/1649313451091.png)]](https://img-blog.csdnimg.cn/d1e12a1feeaa4d088b99c5080b715fa4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_14,color_FFFFFF,t_70,g_se,x_16)
break continue
break 跳出循环
continue 退出本次循环
goto 跳转到tag位置继续执行
//打印三角形
public class TestDemo {
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();
}
}
}
修饰符 返回值类型 方法名(参数类型 参数名) {
……
方法体
……
return 返回值;
}
return 0 // 结束方法
public class StringBase {
public static void main(String[] args) {
int c = 66; //c 叫做实参
String d = "hello"; //d 叫做实参
StringBase stringBase = new StringBase();
stringBase.test5(c, d); // 此处 c 与 d 叫做实参
System.out.println("c的值是:" + c + " --- d的值是:" + d);
}
public void test5(int a, String b) { // a 与 b 叫做形参
a = 55;
b = "no";
}
package com.oop;
//引用传递:对象本质还是值传递
public class Demo02 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
Demo02.change(person);
System.out.println(person.name);
}
//定义一个person 类 有一个属性name
static class Person{
String name ;
}
public static void change(Person person){
person.name = "lxf";
}
}
【运行结果】
c的值是:66 — d的值是:hello
可以看出通过方法传递后,int 类型与 String 类型的原值并没有受到前面 test5 方法执行后的影响,还是输出了原值。这种形为通常被说成值传递。如果原值经过 test5 方法后被改变了,这种形为通常被描述为引用传递。
定义
- **值传递**:指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数进行修改,将不会影响到实际参数。
- **引用传递**:是指在调用函数时将实际参数的地址直接传递到函数中(的形参),那么在函数中对参数所进行的修改,将影响到实际参数。
- **引用传递**:形参为指向实参地址的指针,当对形参的指向操作时,就相当于对实参本身进行的操作。(下面文章中 C++ 的定义,我觉得这样说更精简形象一些,所以放了两个定义,其实意思是一样的)
方法的重载
package com.lxf.method;
public class Demo01 {
public static void main(String[] args) {
int max = max(1,2);
System.out.println(max);
double max1 = max(10.0,20.0);
System.out.println(max1);
}
public static int max(int a , int b){
if (a > b){
return a;
}else {
return b;
}
}
public static double max(double a , double b){
if (a > b){
return a;
}else {
return b;
}
}
}
重载就是在一个类中,有相同的函数名称,但形参不同的函数。
方法的重载的规则:
实现理论:
递归
递归机构包含两个部分
递归体:什么时候需要调用自身的方法。
递归头:什么时候不调用自身的方法,如果没有头,将先陷入死循环。
package com.lxf.array;
public class ArrayDemo02 {
public static void main(String[] args) {
int[] b = {1,2,3,4,25,6,7,8,9,10};
//打印数组
for (int arrary : b){
System.out.print(arrary+" ");
}
System.out.println();
printArray(b);
System.out.println();
reserse(b);
}
//打印数组
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
}
//反转数组
public static void reserse(int[] args) {
int a = 0;
for (int i = 0; i < args.length/2; i++) {
a = args[i];
args[i] = args[args.length-1-i];
args[args.length-1-i]=a;
}
printArray(args);
}
}
多维数组
package com.lxf.array;
public class ArrayDemo03 {
public static void main(String[] args) {
int[][] b = {{1,2},{1,2},{3,4},{25,6},{7,8},{9,10}};
//打印数组
System.out.println();
printArray(b);
}
//打印数组
public static void printArray(int[][] arrays){
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length;j++)
System.out.print(arrays[i][j]+" ");
}
}
}
冒泡排序
package com.lxf.array;
import java.util.Arrays;
public class ArrayDemo05 {
public static void main(String[] args) {
int[] a = {12,32,44,767,2222,12,2,222,1245};
int[] sort= sort(a);
System.out.println(Arrays.toString(sort));
}
public static int[] sort(int[] array){
int c = 0;
boolean flag = false;
for (int i = 0; i < array.length-1; i++) {
for (int j=0; j < array.length-1-i;j++){
if (array[j]>array[j+1]){
c = array[j];
array[j]=array[j+1];
array[j+1]=c;
flag = true ;
}
}
if (flag = true){
break;
}
}
return array;
}
}
稀疏数组
package com.lxf.array;
import java.util.Arrays;
public class ArrayDemo06 {
public static void main(String[] args) {
// 11*11的棋盘 1:黑棋 2:白棋
int[][] arrary = new int[11][11];
arrary[1][2] = 1;
arrary[2][3] = 2;
System.out.println("输出原始数组");
for (int[] ints :arrary){
for (int anInt:ints){
System.out.print(anInt+"\t");
}
System.out.println();
}
int sum = 0 ;
for (int i = 0; i < 11; i++) {
for (int j = 0 ; j < 11; j++)
if (arrary[i][j]!=0){
sum = sum + 1;
}
}
System.out.println("有效值个数为:"+sum);
int[][] arrary2 = new int[sum + 1][3];
arrary2[0][0] = 11;
arrary2[0][1] = 11;
arrary2[0][2] = sum;
//便利数组将非零数存到稀疏数组中
int count = 0;
for (int i = 0; i < arrary.length; i++) {
for (int j = 0; j < arrary.length; j++) {
if (arrary[i][j] != 0){
count++;
arrary2[count][0] = i;
arrary2[count][1] = j;
arrary2[count][2] = arrary[i][j];
}
}
}
//输出
System.out.println("输出稀疏数组");
for (int i = 0; i < arrary2.length; i++) {
System.out.println(arrary2[i][0]+"\t"+arrary2[i][1]+"\t"+arrary2[i][2]+"\t");
}
System.out.println("=======还原数组=============");
int[][] arrary3 = new int[arrary2[0][0]][arrary2[0][1]];
for (int i = 1; i < arrary2.length; i++) {
arrary3[arrary2[i][0]][arrary2[i][1]]=arrary2[i][2];
}
System.out.println("打印还原数组");
for (int[] ints :arrary3){
for (int anInt:ints){
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}

以类的方式组织代码,以对象的组织(封装数据)
三大特性:封装、继承、多态
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9nOCi8ym-1650418499896)(../../图片/typora/1649379496506.png)]](https://img-blog.csdnimg.cn/d48524385a694bada279ba2705548337.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_20,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rHJdpiId-1650418499898)(../../图片/typora/1649383140946.png)]](https://img-blog.csdnimg.cn/1c54941fd713478f90f5cad42bd34b14.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_20,color_FFFFFF,t_70,g_se,x_16)
package com.oop.Demo03;
// java ------> calss
public class Person {
//一个类即使什么都不屑 他也会存在一个方法
//显示的定义构造器
String name ;
int age ;
/*
实例化初始值
使用 new 关键字必须要有构造器 本质实在调研构造器
alt insert 构造函数快捷键
*/
public Person(){
}
//有参的构造函数
public Person( String name){
this.name = "asd";
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(int age) {
this.age = age;
}
}
package com.oop.Demo03;
public class Application {
public static void main(String[] args) {
//new 关键字生成一个class 文件 实例化一个对象
// Person person = new Person( qqq);
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XDSRSB1h-1650418499898)(../../图片/typora/1649416941981.png)]](https://img-blog.csdnimg.cn/7c2865a93cc5431ea5d91affc7fd5be5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_20,color_FFFFFF,t_70,g_se,x_16)
package com.oop;
public class Demo05 {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("lxf");
System.out.println(s1.getName());
s1.setAge(100);
System.out.println(s1.getAge());
}
public static class Student {
/*
名字、学号、性别、学习、睡觉
*/
private String name;
private int id;
private int age;
private char sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
}
}
public static class Student extends Person{
/*\
pulic protected default priovate
*/
}
//java所有类 都默认继承object 类
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jA7EPRwz-1650418499899)(../../图片/typora/1649420710144.png)]](https://img-blog.csdnimg.cn/06b10e099df849db8907250efb1e155f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_20,color_FFFFFF,t_70,g_se,x_16)
重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。即外壳不变,核心重写!
重写的好处在于子类可以根据需要,定义特定于自己的行为。 也就是说子类能够根据需要实现父类的方法。
重写方法不能抛出新的检查异常或者比被重写方法申明更加宽泛的异常。例如: 父类的一个方法申明了一个检查异常 IOException,但是在重写这个方法的时候不能抛出 Exception 异常,因为 Exception 是 IOException 的父类,抛出 IOException 异常或者 IOException 的子类异常。
在面向对象原则里,重写意味着可以重写任何现有方法。实例如下:
class Animal{
public void move(){
System.out.println("动物可以移动");
}
}
class Dog extends Animal{
public void move(){
System.out.println("狗可以跑和走");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal 对象
Animal b = new Dog(); // Dog 对象
a.move();// 执行 Animal 类的方法
b.move();//执行 Dog 类的方法
}
}
重载(overloading) 是在一个类里面,方法名字相同,而参数不同。返回类型可以相同也可以不同。
每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表。
最常用的地方就是构造器的重载。
重载规则:
public class Overloading {
public int test(){
System.out.println("test1");
return 1;
}
public void test(int a){
System.out.println("test2");
}
//以下两个参数类型顺序不同
public String test(int a,String s){
System.out.println("test3");
return "returntest3";
}
public String test(String s,int a){
System.out.println("test4");
return "returntest4";
}
public static void main(String[] args){
Overloading o = new Overloading();
System.out.println(o.test());
o.test(1);
System.out.println(o.test(1,"test3"));
System.out.println(o.test("test4",1));
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-USMykZix-1650418499901)(../../图片/typora/1649422420825.png)]](https://img-blog.csdnimg.cn/d99e3ddf8aa34f4bb90bc7527364ec8c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_20,color_FFFFFF,t_70,g_se,x_16)
public abstract class A{
}
约束和实现分离
接口本质的契约 interface
没有构造方法
public interface InterfaceDemo01 {
void run ();
}
public class UserServiceImpl implements InterfaceDemo01{
@Override
public void run() {
}
检查性异常、运行时异常、错误、
抛出异常
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jp6Lj95K-1650418499902)(../../图片/typora/1649428027163.png)]](https://img-blog.csdnimg.cn/57ce4ba5a7ee4bad94b5a7779b4a3cef.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyf5b6F5pyq5p2l55qE55S35a2p,size_20,color_FFFFFF,t_70,g_se,x_16)
捕获异常
关键字: try、catch、finally、throws
package com.oop1.Demo02;
public class Demo02 {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {//监控 :要发生异常的代码块
System.out.println(a/b);
}catch (ArithmeticException e){ // 捕获异常
System.out.println("程序出现异常1");
}catch (Exception e){ // 捕获异常
System.out.println("程序出现异常2");
}catch (Throwable e){ // 捕获异常
System.out.println("程序出现异常3");
}finally {
System.out.println("finall");
}
}
}
ctrl alt t
