如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
hello
执行 javac Hello.java 生成 Hello.class
1 2 3 4 5 6 7 8 |
// Hello 要与文件名相同 public class Hello{ // 程序执行的起点 public static void main(String[] args){ System.out.print("Hello world"); } } |
数据类型
1 2 3 4 5 6 7 8 |
1.基本类型 整数型 byte short int long 浮点型float double 字符 char 布尔boolean 2.引用数据类型 字符串 数组 类 接口 |
关键字/标识符/常量
1 2 3 4 5 6 7 8 9 10 11 12 13 |
关键字 标识符 不使用数字开头 常量 字符串常量 双引号 'aaaaa' 整数常量 浮点数常量 字符常量 单引号引起来的 只能有一个 'a' 'b' 布尔常量 true false 空常量 null |
变量
- 变量名称不能重复
- float和long类型变量 F L 不能丢
- byte short 数值不能超过范围
- 没有赋值的变量不能直接使用
- 变量使用不能超过作用域范围
12345678910111213141516171819202122232425262728293031public class Bianliang{public static void main(String[] args) {int num1,num2;// int num1=1,num2=2;num1 =10;num2 = num1 +10;System.out.println(num2);System.out.println("-----------");byte num3 = 30;System.out.println(num3);short num4 = 10;System.out.println(num4);long num5 = 20000;System.out.println(num5);float num6 = 2.5F;System.out.println(num6);double num7 = 2.5F;System.out.println(num7);char zf1 = 'A';System.out.println(zf1);boolean var1 =true;System.out.println(var1);}}
类型
自动转换
* 从小->大
强制转换
int num=(int)100L;
(要转换的类型)范围大的数据
- 注意:
强制使用不推荐使用
运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
加减乘除取余 + - * / % 自增自减 ++ -- 赋值运算符 = += -= *= /= %= 比较运算符 == < > <= > = ! = 逻辑运算符 && 与 || 或 ! 非 三元运算符 数据类型 变量名称=条件判断?表达式a:表达式b |
方法
1 2 3 4 5 |
public static void 方法名称(){ } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Fangfa{ public static void main(String[] args) { //调用方法 farmer(); } // 定义方法 public static void farmer(){ System.out.println("111"); System.out.println("222"); System.out.println("333"); } public static void coo(){ System.out.println("cook"); System.out.println("321"); System.out.println("123"); } } |
流程(if/switch/for/while)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
if(表达式){ }else{ } if(表达式){ }else if(表达式){ }else{ } switch(表达式){ case 常量值: 语句体; break; default: 语句体; break; } for(;;){ } while(条件判断){ } do{ }while(条件判断); |
方法调用
方法调用:
1. 单独调用
2. 打印调用
3. 赋值调用
重载
1 2 3 4 |
名称相同, 1. 参数列表不同 2. 参数类型不同 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
public class Fangfa0111{ public static void main(String[] args) { int a=1; int b=2; System.out.println("-----"); System.out.println(summ(a, b));//两数相加 System.out.println(sum_lian(100));//1-某数的之和 get_num_hello(5); } public static int summ(int a,int b){ int c = a+b; return c; } public static int sum_lian(int a){ int c=0; for(int i=0;i<=a;i++){ c = c+i; } return c; } public static void get_num_hello(int a){ for(int i=1;i<=a;i++){ System.out.println("Hello");//1-某数的之和 } } // 重载(名称相同,参数列表不同) public static int cz_sum(int a,int b){ int c = a+b; return c; } public static int cz_sum(int a,int b,int c){ int dd = a+b+c; return dd; } public static int cz_sum(int a,int b,int c,int d){ int dd = a+b+c+d; return dd; } } |
数组arr
- 是一种引用数据类型
- 数组中的多个数据,类型必须统一
- 数组长度不可改变
初始化
- 动态初始化(指定长度)
- 静态初始化(指定内容)
数据类型[] 数组名称 = new 数据类型[数据长度]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class Arr{ public static void main(String[] args) { // 动态初始化 int[] arrayA = new int[300]; double[] arrayB = new double[10]; String[] arrayC = new String[5]; //静态初始化 int[] arrayD = new int[] {1,2,4,5,6,7}; int[] arrayE = {1,2,4,5,6,7}; //读取 System.out.println(arrayE[1]); //赋值 arrayA[1] = 50; System.out.println(arrayA[5]);//默认0 '' 0.0 System.out.println(arrayA[1]);//50 //获取长度 System.out.println(arrayD.length); //数组遍历 for(int i=0;i<=arrayD.length;i++){ System.out.println(arrayD[i]); } //数组的最值 //数组反转(使用第三方) } public static void get_sum(int[] arrayA){ } } |
object
导入包
import 包名称.类名称
如果同目录下不用写
Student stu = new Student()
stu.a
stu.a()
1 2 3 4 5 6 7 8 9 10 |
<br />public class Obj{ public static void main(String[] args) { int[] arrayD = new int[] {1,2,4,5,6,7}; // System.out.println(arrayD.toString()); Student stu = new Student(); stu.aa(); System.out.println(stu.bb); } } |
random
random 随机数
int r = rand.nextInt();////随机生成正负
int r2 = rand.nextInt(10);//0-9
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Random1{ public static void main(String[] args) { // 生成随机数() Random rand = new Random(); int r = rand.nextInt(); System.out.println("随机数是:"+r); int r2 = rand.nextInt(10); System.out.println("随机数是:"+r2);//0-9 } } |
scanner
键盘输入
1.导包
import
2.创建
3.使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; public class Scann{ public static void main(String[] args) { String str = "dsds"; //创建 Scanner sc = new Scanner(System.in); //使用 //获取键盘输入数据 // int num = sc.nextInt(); // System.out.println(num); //求两个数的和 System.out.print("请输入num1:"); int num1 = sc.nextInt(); System.out.print("请输入num2:"); int num2 = sc.nextInt(); int c = num1+num2; System.out.println(c); } } |
arraylist idea
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import zf.com.Student; import java.util.ArrayList; /** arraylist 使用 1. 自定义学生类 2. 创建集合 ,用来存学生对象 3. 根据类创建4个学生对象 4.将4个学生对象加到 add 5.遍历集合 for size get */ public class Arraylist_demo { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<Student> (); Student one = new Student("a",20); Student two = new Student("b",22); Student three = new Student("c",24); Student four = new Student("d",25); list.add(one); list.add(two); list.add(three); list.add(four); for (int i = 0; i < list.size(); i++){ Student stu = list.get(i); System.out.println("姓名:"+stu.getName()+",年龄:"+stu.getAge()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<br />import java.util.ArrayList; /** Arraylist System.out.println(list);//[aa, bb, cc] printList(list);//{aa@bb@cc} */ public class Arraylist_demo1 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String> (); list.add("aa"); list.add("bb"); list.add("cc"); System.out.println(list);//[aa, bb, cc] printList(list);//{aa@bb@cc} } /** 定义方法三要素 1.返回值名称 2.方法名称 参数列表 */ public static void printList(ArrayList<String> list){ System.out.print("{"); for (int i = 0; i < list.size(); i++){ String name = list.get(i); if(i==list.size()-1){ System.out.print(name); System.out.print("}"); break; } System.out.print(name+"@"); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<br />import java.util.Random; import java.util.ArrayList; /** 一个大集合存入20个随机数字,筛选其中偶数元素, 放入小集合中 */ public class Arraylist_demo2 { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer> (); Random r = new Random(); for (int i = 0; i < 20; i++) { list.add(r.nextInt(100+1)); } ArrayList<Integer> smallList = getsmalllist(list); System.out.println(smallList); System.out.println(smallList.size()); } /** * 接受大集合,返回小集合 */ public static ArrayList<Integer> getsmalllist(ArrayList<Integer> biglist){ ArrayList<Integer> smalllist = new ArrayList<Integer> (); for (int i = 0; i < biglist.size(); i++) { int num = biglist.get(i); if (num %2==0){ smalllist.add(num); } } return smalllist; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import zf.com.Person; import java.util.ArrayList; /** 定义一个数组,用来存储3个Person对象 数组缺点:一旦创建,长度不可改变 ArrayList 集合 ArrayList<Integer> list = new ArrayList<Integer> (); <> 泛型 定义数据的类型 长度可随意改变 ArrayList 打印,直接是值 向集合中加数据 .add 获取数据 .get 删除 .remove 获取长度 .size() */ public class Arraylist_person { public static void main(String[] args) { Person[] array = new Person[3]; Person one = new Person("'迪丽热巴'",18); Person two = new Person("'鹿晗'",23); Person three = new Person("'陈赫'",22); array[0] = one; array[1] = two; array[2] = three; System.out.println(array[0]);//地址值 System.out.println(array[1]); System.out.println(array[2]); System.out.println(array[0].getName());//'迪丽热巴' ArrayList<String> list2 = new ArrayList<> (); list2.add("aa"); list2.add("bb"); list2.add("cc"); System.out.println(list2); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.Scanner; /** 输入一个字符串,统计各种字符串出现的次数 1. 输入Scanner 2.键盘输入的字符串 String str = sc.next() 3.定义四个变量,用来代表各种字符穿心的次数 4.对字符串一个个进行检查 String->char[] 方法: toCharArray() 5.遍历char[] 进行判断 6.打印出现的次数 */ public class Scanner_test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一个字符串"); String input = sc.next(); int dx = 0; int xx = 0; int sz = 0; int qt = 0; char[] charArray= input.toCharArray(); for (int i = 0; i <charArray.length; i++ ){ char c =charArray[i]; if ('A'<=c && c<='Z' ){ dx++; }else if ('a'<=c && c<='z'){ xx++; }else if ('0'<=c && c<='9'){ sz++; }else { qt++; } } System.out.println("大写字母有:"+dx); System.out.println("小写字母有:"+xx); System.out.println("数字有:"+sz); System.out.println("其他有:"+qt); } } |
static
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import zf.com.Static_t; import java.util.Arrays; /** * Arrays工具类 Arrays.sort(arr1); 排序 toString(数组) 数组->字符 sort 排序 (小到大) */ public class Static_demo { public static void main(String[] args) { Static_t a = new Static_t(); Static_t b = new Static_t(); String str = "sdijsncdkncksdmco324"; char[] arr1 = str.toCharArray(); Arrays.sort(arr1); for (int i = 0; i < arr1.length; i++) { System.out.println(arr1[i]); } } } |
string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/** String字符串类 equals 比较 equalsIgnoreCase 比较 忽略大小写 length 长度 concat 拼接字符串相当于+ charAt 获取指定索引位置的单字符 indexof 字符串首次出现的位置 substring 截取 tocharArray() 字符串改为字节数组 getBytes() 当前字符串底层的字节数组 replace 替换 split 切割 字符串->数组 数组->字符串 */ public class String_test { public static void main(String[] args) { String a = "Hello"; String b = "Hello"; String c = "hello"; String d = "hello,asasakndsknd,k4ji2o3n435no4n532o532"; System.out.println(a.equals(b));//true System.out.println(a.equals(c));//false System.out.println(a.equalsIgnoreCase(c));//true 忽略大小写 System.out.println(a.length()); System.out.println(a.concat("'aaaa'"));//Hello'aaaa' System.out.println(a.charAt(2));//l System.out.println(a.indexOf("o"));//4 System.out.println(d.substring(5));// asasakndskndk4ji2o3n435no4n532o532 System.out.println(d.substring(5,10));// asas // cahr[] aa = d.toCharArray(); String dd = d.replace("hello","mc"); System.out.println(dd); System.out.println("-----"); String[] arr1 = d.split(","); System.out.println(arr1.length); System.out.println(arr1[1]); } } |
面向对象
面向对象
封装/继承/多态
继承
1. 单继承
2. 多级继承
1 2 3 4 5 6 7 8 |
<br />public class Demo { public static void main(String[] args) { Zi zi = new Zi(); zi.show(); zi.method(); } } |
继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Zi extends Fu{ int num = 20; //重写方法 @Override public void method() { super.method();//调用父类的 System.out.println("子类方法"); } public void show() { int num = 30; System.out.println(num);//30 当前 System.out.println(this.num);//20 子类的 System.out.println(super.num);//10 父类的 } } |
Math
Math.abs(double num) 取绝对值
.ceil 向上取整
.floor 向下取整(抹零)
.round 四舍五入
PI 圆周率
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class MathTest { public static void main(String[] args) { double min =-10.8; double max =5.9; for (int i = (int) min ; i < max; i++) { int abs = Math.abs(i); if (abs > 6 || abs < 2.1){ System.out.println(i); } } } } |
抽象方法
抽象方法
abstract
抽象类和方法的使用
1. 不能直接创建new抽象对象
2. 必须用一个子类继承父类抽象父类
3.子类覆盖重写父类当中的抽象方法 去掉abstract关键字
4. 创建子类对象进行使用
final 最后赋值
1 2 3 4 5 6 7 8 |
public abstract class Chouxiang { //抽象方法 public abstract void eat(); } |
public > protected > default > private