希望你们能用到—— =—_—=
修饰符(访问权限)
-
public 公用的(都可访问)
-
protected 可在同一个包内或其子类的对象访问
-
defualt 只可再同一个包内访问
-
private 私用的(只能在自己类中访问)
字符类型
-
细节
-
字符常量是用单引号括起来的单个字符
-
java中还允许使用转义字符
-
java中,char本质是一个证书,再输出时,是unicode码对应的字符
-
可以直接给char赋一个整数,然后输出时会按照对unicode字符输出[97]
-
char类型时可以对进行运算的,相当于一个整数,因为他都有对应的unicode码
-
-
本质
-
字符型有对应的码
-
字符和码值是有相对应的表组成的
-
-
自动类型转换细节
-
有多种数据类型混合运算时,系统首先自动将数据转换为容量最大的数据类型,然后进行计算
-
当我们把精度大的数据类型复制给精度小的数据类型时,就会报错,反之会自动类型转换
-
(byte,short)和char之间不会相互自动转换
-
boolean不参与转换
-
自动提升规则:表达式结果的类型自动提升为操作书中最大的类型
-
java输入
-
import java.util.* // Scanner类表示见到的文本扫描器 Scanner myScnner = new Scanner(System.in); // 接收用户输入 String name = myScanner.next(); // Scanner除去next方法以外还有很多的方法
switch的使用
switch(使用条件){ case 'a': break; case 'b': break; default: System.out.println("如果都没有选项东的话使用这个语句"); }
数组的创建
//1. 创建5个double类型数组 double scores[] = new double[5]; // 也可以(必须指定创建数组大小) double[] scores = new double[5]; // 2. 声明数组再创建空间 double scores[]; scores = new scores[5]; // 数组赋值的机制 int[] arr = {1,2,3} // 这个时候赋值的是arr的地址 int arr2[] = arr; // 这个时候修改则都会改变 arr2[0] = 10;
接下来开始拷贝数组
int[] arr0 = {1,2,3}; int[] arr = new arr[arr0.length]; for(int i=0; i<=arr0.length; i++){ arr[i1] = arr0[i]; }
数组的反转
int[] arr = {1,2,3,4,5,6,7,8,9}; for(int i = 0; i <= arr.length/2; i++){ int temp; temp = arr[i]; arr[i] = arr[i.length-1-i] arr[i.length-1-i] = temp; }
冒泡排序
int[] arr = {5,1,2,6,8,4,1,3} // 交换变量 int temp; for(int i = 0; i < arr.length; i++){ for(int j = i; j <= arr.length-1; i++){ if (arr[i] > arr[j]){ temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } }
二维数组创建
int arr[][] = {{1,1,2,3},{4,5,6,2}} int arr2[][] = new arr[10][12]; int[][] arr3 = new arr[10][12];
数组的length
int[] temp = new int[10]; temp = {1,2,3,4,5,6} // 如果此时遍历,输出的是temp整个数组的大小,包括没有东西的数组 for(int i = 0; i < temp.length; i++){ // 这是如果加一个判断语句给他过滤掉的话,就不会显示 if (temp[i] == null){ break; // 当然这样也是有问题的,如果中间没有的话就会都被过滤掉,可以考虑使用continue } System.out.println(temp[i]); }
时间
// 创建日期变量 Date date = null; // 获取当前时间 date = new Date(); // 获取改变日期格式类,将日期改为相应格式的字符串 SimpleDateFormat sdf = null; // 新建日期格式 y是年M是月d是日 H是小时m是分钟s是秒 sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 使用日期转换格式类转换日期 String dateString = sdf.format(date);
// 此方法可以获得当前时间,毫秒 long start = System.currentTimeMillis();
随机数
// 在数学函数中有一个random方法可以实现0-1的一个小数随机数 // 这个方法可以实现一个小数随机数字 double rd = Math.random(); // 使用此方法即可获得1-9的整数(方法是乘10取整) System.out.println((int)(rd*10))
使用String方法保留两位小数
String num = String.format("%.2f",10.146644); System.out.println(num);
判断类型
-
instanceof判断类型关键字
-
这里涉及接口
// Usb此时是一个接口,而Phone和Camera都是实现了接口的实体类 Usb[] usbs = new Usb[2]; usbs[0] = new Phone(); usbs[1] = new Camera(); for (int i = 0; i < usbs.length; i++){ usbs[i].work(); // 接口都有这个方法 // 通过instanceof判断是否是一个类型,如果是的话,返回true if(usb[i] instanceof Phone){ usbs[u].call(); // call方法是Phone实体类独有的 } }