一 回顾
1.项目打包 导包 删除依赖包
2.Object
概念:Object的所有类的直接父类或者是间接父类
构造:public Object()
常用的方法:
public String toString()
A.返回对象的字符串表示形式
B.没有重写返回的是对象的地址值 重写之后返回的是对象的属性值
public boolean equals(Object obj)
A.比较两个对象是否是同一个对象 两个栈引用是否指向同一个堆内存
B.重写之后:比较的的对象的属性值是否相同
C.equals与== 区别
equals 范围
只能比较引用数据类型 == 可以比较引用数据类型 也可以比较基本数据类型
equals 规则
没有重写比较对象的地址值 重写之后 比较的的对象的属性值是否相同
==比较基本数据类型比较的值 比较引用数据类型比较的是地址值
3.String
概述:
A.String 类代表字符串
B.字符串是常量;它们的值在创建之后不能更改
C.字符串字面值 存在堆内存中常量池中 new 字符串 都存在堆内存中
D.字符串的底层是以字节数组来进行存储
构造方法:
String s="abc";
public String()
public String(byte[] bytes)
public String(char[] value)
常用的方法: 判断性 得到性 转换性
二作业1
step01 需求
step02 分析
1.定义一个方法判断一个数是否是对称数
2.正的字符串与反的字符串 就是对称数
3.遍历数组调用方法
step03代码
package com.qf.demo11;
import com.sun.javaws.IconUtil;
public class Test {
public static void main(String[] args) {
String [] arrays = new String[]{"010","3223","666","7890987","123123"};
int count = 0;
for (String array : arrays) {
char[] chars = array.toCharArray();
String s = "";
for (int i = chars.length-1; i >=0; i--) {
s = s + chars[i];
}
if (s.equals(array)){
System.out.println(array);
count++;
}
}
System.out.println(count);
}
}
四 作业3
step01 需求
step02 分析
1.使用死循环 遇到 end停止循环
2.使用字符串进行拼接
3.统计字母的个数
step03 代码
package com.qf.work01;
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String w="";
while (true){
System.out.println("请输入字符串");
String s =input.next();
if (s.equals("end")){
break;
}else{
w+=s;
}
}
String k="";
int count=0;
//将字符串转换为字符数组
char[] ch = w.toCharArray();
for (int i=0;i<ch.length;i++) {
if (ch[i] >='a' && ch[i]<='z'){
k+=(ch[i]+"").toUpperCase();
count++;
}else if(ch[i] >='A' && ch[i]<='Z'){
k+=(ch[i]+"").toLowerCase();
count++;
}else {
k+="*";
}
}
System.out.println(k);
System.out.println(count);
}
}
五 作业4
step01 需求
step02 代码
package com.qf.work01;
public class Test03 {
public static void main(String[] args) {
//定义一个数组
int [] arrays ={1,2,3};
String w="";
for (int i=0;i<arrays.length;i++){
if (i ==0){
w+="["+arrays[i]+",";
}else if(i ==arrays.length-1){
w+=arrays[i]+"]";
}else {
w+=arrays[i]+",";
}
}
System.out.println(w);
}
}
六 StringBuffer(重点)
6.1 简介
1.概念:线程安全的可变字符序列 个类似于 String 的字符串缓冲区
但通过某些方法调用可以改变该序列的长度和内容
append(boolean b) insert(int offset, boolean b)
2.String 与StringBuffer区别:
String 不可以变字符串 StringBuffer 是可变的字符串
String 操作字符串效率低 StringBuffer效率高 sql拼接
3.StringBuiler 与 StringBuffer的区别:
A.版本不一样
StringBuiler1.5 StringBuffer 1.0
B.效率
StringBuiler 效率高 StringBuffer 低
C.线程安全性
StringBuiler 安全性低 StringBuffer 安全性高
6.2 构造方法
| 构造方法的名称 | 构造方法的描述 |
|---|---|
| public StringBuilder() | 实例化StringBuilder对象 初始化容量是16 |
| public StringBuffer(int capacity) | 实例化StringBuilder对象 指定初始化容量大小 |
| public StringBuffer(String str) | 将String构建成一个StringBuffer对象 初始化容量是字符串长度+16 |
代码
package com.qf.demo01;
public class Test01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());
StringBuffer sb1 = new StringBuffer(20);
System.out.println(sb1.capacity());
StringBuffer sb2 = new StringBuffer("张三");
System.out.println(sb2.capacity());
}
}
6.3 普通方法
| 方法的名称 | 方法的描述 |
|---|---|
| public StringBuffer append(String s ) | 在字符串末尾进行添加 |
| public StringBuffer insert(int offset, String str) | 在字符串的指定的位置进行添加 |
| public int capacity() | 获取StringBuffer的容量 |
| public StringBuffer deleteCharAt(int index) | 根据索引来进行删除 |
| public StringBuffer delete(int start, int end) | 指定区间来进行删除 |
| public StringBuffer reverse() | 控制反转 |
代码
package com.qf.demo01;
public class Test02 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("谢顺");
sb.append("广州最靓的仔");
System.out.println(sb);
sb.insert(1,"叼毛");
System.out.println(sb);
sb.deleteCharAt(3);
System.out.println(sb);
sb.delete(3,5);
System.out.println(sb);
sb.reverse();
System.out.println(sb);
}
}
6.4 StringBuffer 与String相关转换
1.StringBuffer 转换为String
调用 toString()
利用构造方法 public String(StringBuffer buffer)
2.String 转换 StringBuffer
利用构造方法 public StringBuffer(String str);
调用方法 new StringBuffer().append("");
代码
package com.qf.demo01;
public class Test03 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("陈亮宇");
System.out.println(sb.toString());
System.out.println(new String(sb));
String s="何权龙";
System.out.println(new StringBuffer(s));
System.out.println(new StringBuffer(s).append(""));
}
}
6.5案例
step01 需求
step02 分析
1.对数组进行遍历
2.使用StringBuffer来进行拼接
3.调用控制反转的方法 最后进行拼接
step03 代码
package com.qf.demo01;
public class Test04 {
public static void main(String[] args) {
//定义一个数组
int[] arrays ={20,40,90,100};
StringBuffer sb = new StringBuffer();
for (int i=arrays.length-1;i>=0;i--) {
sb.append( arrays[i]+",");
/* if (i==0){
sb.append(""+arrays[i]+",");
}else if (i == arrays.length-1){
sb.append(arrays[i]);
}else {
sb.append(arrays[i]+",");
}*/
}
System.out.println(new StringBuffer("[").toString()+sb.deleteCharAt(sb.length()-1)+new StringBuffer("]"));
}
}
七 包装类
7.1简介
1.概念:将基本数据类型包装成引用数据类型 将基本数据类型封装成一个类
2.好处:
A.包装类可以调用类的属性与方法
B. 集合中的泛型必须使用包装类
3.罗列:(重点)
特点:
A.包装类就是基本数据累的首字母变为大写 除了 int 以及char类型
| 基本数据类型 | 包装类 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
| char | Character |
7.2 Integer
7.2.1概念
1.概念:
A.Integer 类在对象中包装了一个基本类型 int 的值
B.该类提供了多个方法,能在 int 类型和 String 类型之间互相转换
7.2.2 常量
| 常量的名称 | 常量的描述 |
|---|---|
| public static final int MAX_VALUE | 它表示 int 类型能够表示的最大值 |
| public static final int MIN_VALUE | 它表示 int 类型能够表示的最小值 |
| public static final Class TYPE | 表示基本类型 int 的 Class 实例 |
| public static final int SIZE | 用来以二进制补码形式表示 int 值的比特位数 |
代码
package com.qf.demo02;
public class Test {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.SIZE);
System.out.println(Integer.TYPE);
}
}
7.2.3 构造方法(构造方法转数据)
| 构造方法的名称 | 构造方法的描述 |
|---|---|
| public Integer(int value) | 将int类型包装Integer类型 |
| public Integer(String s) | 将String类型转换为Integer类型 |
代码
package com.qf.demo02;
public class Test01 {
public static void main(String[] args) {
Integer in = new Integer(20);
System.out.println(in);
Integer inte = new Integer("12.5");
System.out.println(inte);
}
}
7.2.4 常用的成员方法
| 方法的名称 | 方法的描述 |
|---|---|
| public static int parseInt(String s) 重点 | 将字符串转换为int类型 |
| public xxx xxxValue() | 将Integer数据类型转换为各种基本数据类型 |
| public static Integer valueOf(int i) public static Integer valueOf(String s) | 将int包装成一个Integer类型,将String类型转换为Integer类型 |
| public static String toBinaryString(int i) | 以二进制的形式进行表示 |
| public static String toOctalString(int i) | 以八进制的形式进行表示 |
| public static String toHexString(int i) | 以十六进制的形式进行表示 |
代码
package com.qf.demo02;
public class Test02 {
public static void main(String[] args) {
int num = Integer.parseInt("123");
System.out.println(num);
Integer in = new Integer(12);
short i = in.shortValue();
System.out.println(i);
Integer inte = Integer.valueOf("12");
System.out.println(inte);
String s1 = Integer.toBinaryString(10);
System.out.println(s1);
String s2 =Integer.toOctalString(10);
System.out.println(s2);
String s3 = Integer.toHexString(10);
System.out.println(s3);
}
}
7.3 自动装箱与拆箱
1.装箱:将基本数据类型包装包装类 ===> 快递
2.拆箱:将包装类拆成基本数据类型 ==>拆快递
3.在jdk1.5之后 会自动自动装箱与拆箱
代码
package com.qf.demo02;
public class Test03 {
public static void main(String[] args) {
Integer num =5; //自动装箱的过程
int n = num; //自动 拆箱的过程
}
}
7.4 案例
step01 需求
step02 分析
1.根据空格来进行拆分 split(" ")
2.遍历数组将数据存入Integer数组中 parseInt()
3.排序 打印
step03 代码
package com.qf.demo02;
import java.util.Arrays;
public class Test04 {
public static void main(String[] args) {
String str ="10 20 -1 70 100 50 30";
//对字符串进行分割
String[] arrays = str.split(" ");
Integer [] integers = new Integer[arrays.length];
//遍历
for (int i=0;i<arrays.length;i++) {
integers[i]= Integer.parseInt(arrays[i]);
}
//排序
Arrays.sort(integers);
System.out.println(Arrays.toString(integers));
}
}
八 BigInteger
1.概述:不可变的任意精度的整数 BigInteger 描述整数的类型比int 类型范围更加大(大数据类型)
2.常量
| 常量的名称 | 常量的描述 |
|---|---|
| public static final BigInteger ONE | 1 |
| public static final BigInteger TEN | 10 |
| public static final BigInteger ZERO | 0 |
3.构造方法
| 构造方法 | 构造方法描述 |
|---|---|
| public BigInteger(String val) | 将字符串以十进制的格式转换为BigInteger |
| public BigInteger(String val, int radix) | 将字符串安装指定进制数转化十进制 |
代码
package com.qf.demo03;
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
BigInteger big = new BigInteger("123456789012345");
System.out.println(big);
BigInteger big01 = new BigInteger("10",2);
System.out.println(big01);
}
}
4.常用的方法
| 方法的名称 | 方法的描述 |
|---|---|
| public BigInteger abs() | 取绝对值 |
| public BigInteger negate() | 取反 |
| public BigInteger add(BigInteger val) | 加法 |
| public BigInteger subtract(BigInteger val) | 减法 |
| public BigInteger multiply(BigInteger val) | 乘法 |
| public BigInteger divide(BigInteger val) | 除法 |
| public BigInteger remainder(BigInteger val) | 取余数 |
代码
package com.qf.demo03;
import java.math.BigInteger;
public class Test01 {
public static void main(String[] args) {
BigInteger big01 = new BigInteger("-10");
System.out.println(big01.abs());
System.out.println(big01.negate());
System.out.println("===============================");
BigInteger big02 = new BigInteger("12");
BigInteger big03 = new BigInteger("5");
System.out.println(big02.add(big03));
System.out.println(big02.subtract(big03));
System.out.println(big02.multiply(big03));
System.out.println(big02.divide(big03));
System.out.println(big02.remainder(big03));
}
}
九 BigDecimal(重点)
1.概念:不可变的、任意精度的有符号十进制数
2.构造方法
| 方法的名称 | 方法的描述 |
|---|---|
| public BigDecimal(double val) | 将double类型转化BigDecimal |
| public BigDecimal(int val) | 将int类型转换为BigDecimal |
| public BigDecimal(String val) | 将String转换为BigDecimal |
代码
package com.qf.demo03;
import java.math.BigDecimal;
public class Test02 {
public static void main(String[] args) {
BigDecimal big01 = new BigDecimal("1234567890123423");
System.out.println(big01);
BigDecimal big02 = new BigDecimal(123456789);
System.out.println(big02);
BigDecimal big03 = new BigDecimal(1234567890.23);
System.out.println(big03);
}
}
1234567890123423
123456789
1234567890.230000019073486328125
3.常用的方法
| 方法的名称 | 方法的描述 |
|---|---|
| public BigDecimal add(BigDecimal augend) | 加法 |
| public BigDecimal divide(BigDecimal divisor,int scale, int roundingMode) | 除法 |
| public BigDecimal multiply(BigDecimal multiplicand) | 乘法 |
| public BigDecimal subtract(BigDecimal subtrahend) | 减法 |
| public BigDecimal remainder(BigDecimal divisor) | 取余数 |
代码
package com.qf.demo03;
import java.math.BigDecimal;
public class Test03 {
public static void main(String[] args) {
BigDecimal big01 = new BigDecimal(10);
BigDecimal big02 = new BigDecimal(2);
System.out.println(big01.add(big02));
System.out.println(big01.subtract(big02));
System.out.println(big01.multiply(big02));
System.out.println(big01.divide(big02));
System.out.println(big01.remainder(big02));
BigDecimal big03 = new BigDecimal(10);
BigDecimal big04 = new BigDecimal(3);
//向上ROUND_UP //ROUND_DOWN 向下 ROUND_HALF_UP 四舍五入
System.out.println(big03.divide(big04,2,BigDecimal.ROUND_UP));
System.out.println(big03.divide(big04,2,BigDecimal.ROUND_DOWN));
System.out.println(big03.divide(big04,3,BigDecimal.ROUND_HALF_UP));
}
}
十 Math(查看api了解)
1.概念:Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数 数学的工具类
2.常量
| 常量名称 | 常量描述 |
|---|---|
| public static final double E | 自然对数的底数 |
| public static final double PI | 圆周率 |
3.静态的方法
| 方法的名称 | 方法的描述 |
|---|---|
| public static double abs(double a) | 绝对值 |
| public static double max(double a, double b) | 返回两个 double 值中较大的一个 |
| public static int min(int a, int b) | 返回两个 int 值中较小的一个 |
| public static double pow(double a, double b) | 返回第一个参数的第二个参数次幂的值 |
| public static double random() | 该值大于等于 0.0 且小于 1.0随机数 |
| public static long round(double a) | 四舍五入 |
| public static double ceil(double a) | 向上取整 |
| public static double floor(double a) | 向下取整 |
| public static double cbrt(double a) | 开立方根 |
代码
package com.qf.demo04;
public class Test01 {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.E);
System.out.println(Math.abs(-1));
System.out.println(Math.abs(1));
System.out.println(Math.max(10,20));
System.out.println(Math.min(10,20));
System.out.println(Math.pow(2.1,2.1));
System.out.println(Math.random());
System.out.println(Math.round(2.4));
System.out.println(Math.round(2.6));
System.out.println(Math.ceil(2.2));
System.out.println(Math.ceil(2.6));
System.out.println(Math.floor(2.6));
System.out.println(Math.floor(2.2));
System.out.println(Math.cbrt(8));
}
}
十一 System
1.概述:System 类包含一些有用的类字段和方法。它不能被实例
2.常量
| 常量名称 | 常量描述 |
|---|---|
| public static final PrintStream err | “标准”错误输出流。 |
| public static final InputStream in | 标准”输入流(关联键盘) |
| public static final PrintStream out | 标准输出流 |
代码
package com.qf.demo05;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入");
String line = input.next();
System.out.println("顶顶.....");
System.err.println("哈哈哈");
}
}
3.方法
| 方法的名称 | 方法描述 |
|---|---|
| public static void gc() | 手动调用运行垃圾回收器 |
| public static void exit(int status) | 终止当前正在运行的 Java 虚拟机,非 0 的状态码表示异常终止 |
| public static long currentTimeMillis()(重点) | 返回以毫秒为单位的当前时间 1970 年 1 月 1 日 |
代码
package com.qf.demo05;
public class Test02 {
public static void main(String[] args) {
System.out.println("开始.....");
System.gc();
System.out.println("结束.....");
//System.exit(-1);
System.out.println("ddddd");
//获取系统毫秒值
long time = System.currentTimeMillis();
System.out.println(time);
}
}
十二 Date
1.概念:类 Date 表示特定的瞬间,精确到毫秒 表示的是java中的时间对象
2.构造方法
| 构造方法的名称 | 构造方法的描述 |
|---|---|
| public Date()(重点) | 实例化Date 表示当前系统的时间 |
| public Date(long date) | 实例化Date对象 设置date的时间 |
代码
package com.qf.demo06;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);
Date d1 = new Date(1000);
System.out.println(d1);
}
}
3.常用的方法(重点)
| 方法的名称 | 方法的描述 |
|---|---|
| public void setTime(long time) | 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点 |
| public long getTime() | 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数 |
代码
package com.qf.demo06;
import java.util.Date;
public class Test01 {
public static void main(String[] args) {
Date d = new Date(1000);
d.setTime(3000);
System.out.println(d);
System.out.println(d.getTime());
//System.out.println(d.getTime());
}
}
十三 Calendar(重点)
1.概念:Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法
2.获取Calendar ==>Calendar rightNow = Calendar.getInstance();
3.常用的方法
| 方法的名称 | 方法的描述 |
|---|---|
| public abstract void add(int field, int amount) | 根据日历的规则,为给定的日历字段添加或减去指定的时间量 |
| public int get(int field) | 返回给定日历字段的值 |
| public static Calendar getInstance() | 获取Calendar对象 |
| public final Date getTime() | 将Calendar转换为Date对象 |
| public void set(int field,int value) | 将给定的日历字段设置为给定值 |
代码
package com.qf.demo06;
import java.util.Calendar;
import java.util.Date;
public class Test02 {
public static void main(String[] args) {
//实例化Calendar对象
Calendar cal =Calendar.getInstance();
//获取年
System.out.println(cal.get(Calendar.YEAR));
//获取月
System.out.println(cal.get(Calendar.MONTH)+1);
//获取日
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
//获取时
System.out.println(cal.get(Calendar.HOUR));
//获取分
System.out.println(cal.get(Calendar.MINUTE));
//获取秒钟
System.out.println(cal.get(Calendar.SECOND));
// cal.add(Calendar.DAY_OF_MONTH,-5);
// Date d = cal.getTime();
// System.out.println(d);
cal.set(Calendar.MONTH,10);
System.out.println(cal.get(Calendar.MONTH));
}
}
十四 SimpleDateFormat(重点)
1.概念:格式化和解析日期的具体类
2.格式: yyyy-MM-dd ==>年月日 yyyy-MM-dd hh:mm:ss 年 月 日 时 分 秒
3.构造方法:public SimpleDateFormat(String pattern) 参数表示传递格式的类型
4.常用的方法:
public StringBuffer format(Date date)==> 将Date 对象格式化指定时间格式
public Date parse(String text)==>将指定格式字符串时间转换为Date对象
代码
package com.qf.demo06;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test03 {
public static void main(String[] args) throws ParseException {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = sdf.format(d);
System.out.println(time);
Date d1 = sdf.parse(time);
System.out.println(d1);
}
}
十五案例
step01 键盘录入生日 统计出生了多少天
step02 分析
1.键盘录入的 年 月 日生日 转换为 Date类型
2.调用getTime() 获取毫秒值
3.系统的毫秒值 - 生日的毫秒值
step03 代码
public static void main(String[] args) throws ParseException {
Scanner input = new Scanner(System.in);
System.out.println("请输入日期,格式yyyy-mm-dd");
String next = input.next();
Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(next);
System.out.println(parse.getTime());
System.out.println(System.currentTimeMillis());
long l = System.currentTimeMillis() - parse.getTime();
long s = l / 1000 / 60 / 60 / 24;
System.out.println(s);
}
package com.qf.demo06;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Test05 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入出生日期");
String line =input.next();// 1968-11-23
//对字符串进行拆分
String[] arrays = line.split("-");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,Integer.parseInt(arrays[0]));
cal.set(Calendar.MONTH,Integer.parseInt(arrays[1]));
cal.set(Calendar.DAY_OF_MONTH,Integer.parseInt(arrays[2]));
Date date = cal.getTime();
System.out.println(date);
long time = date.getTime();
//获取到系统毫秒值
long t = System.currentTimeMillis();
System.out.println((t-time)/1000/60/60/24/365);
}
}
\