需求:
分析:
代码:
package com.qf.test.test01;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
//实例化Scanner
Scanner input = new Scanner(System.in);
System.out.println("请输入一串字符");
String line = input.next();
//将字符串转换为字符数组
char[] chars = line.toCharArray();
//定义一个字符串变量进行拼接
String s = "";
//使用循环遍历数组
for (int i=0;i='a'&&chars[i]<='z'){
//将字符转换为大写
s += (chars[i]+"").toUpperCase();
}else if (chars[i]>='A'&&chars[i]<='Z'){
//将字符转换为小写
s += (chars[i]+"").toLowerCase();
}else {
//将字符转换为*
s += "*";
}
}
//打印输出s
System.out.println(s);
}
}
3.字符串的比较 二、StringBuffer(重点) 1.简介
A.线程安全的可变字符序列。一个类似于String的字符串缓冲区
B.StringBuffer 上的主要操作是append和insert方法,可以改变该序列的长度和内容
C.StringBuffer在多线程中线程安全 效率低
2.StringBuffer与String比较A.String是不可变的字符序列 StringBuffer是可变的字符序列
B.String操作字符串的效率比较低 StringBuffer操作字符串效率比较高
3.StringBuffer 与StringBuilder 的比较A.版本不一样 StringBuffer 从jdk1.0开始 StringBuilder从jdk 1.5开始
B.多线程安全性 StringBuffer 多线程安全 StringBuilder多线程不安全
C.操作字符串效率 StringBuffer效率低 StringBuilder效率高
4.构造方法 5.常用方法 6.案例需求:
分析:
代码:
package com.qf.test.test01;
public class Test02 {
public static void main(String[] args) {
//实例化一个StringBffuer对象
StringBuffer sb = new StringBuffer();
//定义一个数组
int [] nums = {10,20,30,40,50};
//使用循环遍历数组
for (int i=0;i
7.StringBuffer与String之间的转换
1.StringBuffer ==>String
A. public String toString () toStrinig()方法
B. public String(StringBuffer buffer) 使用构造方法
2.String ==> StringBuffer
A. public StringBuffer(String str ) 使用构造方法
B. new StringBuffer().append(String str); 使用StringBuffer的方法
三、包装类
1.简介
1.概念:将基本数据类型的数据封装成一个类 可以使用类的方法已经属性
2.罗列:
3.特点 :int类型与char类型的包装类比较特殊外,其他的基本数据类型的包装类都是其首字母变为大写
2.Integer
1.简介
A.Integer类在对象中包装了一个基本数据类型int的值
B.该类提供了多个方法,能在int类型和String类型之间进行相互转换
C.提供了处理int类型时非常有用的其他一些常量和方法
2.常量
3.构造方法
4.常用方法
5.方法测试
package com.qf.test.test01;
import javax.sound.midi.Soundbank;
public class Test03 {
public static void main(String[] args) {
//获取int最大值
System.out.println(Integer.MAX_VALUE);
//获取int最小值
System.out.println(Integer.MIN_VALUE);
//获取int类型比特位 一个字节占8个bit
System.out.println(Integer.SIZE);
//获取int类型的class实例
System.out.println(Integer.TYPE);
//通过Integer构造方法将int类型包装诚Integer类型
Integer in = new Integer(10);
//通过Integer构造方法将String类型的整数值包装诚Integer类型
Integer in1 = new Integer("2012");
//实例化一个Integer对象
Integer inn = new Integer(12);
//调用xxxvalue方法 将integer类型转换为 各种基本数据类型的整数值 如转换为double类型
System.out.println(inn.doubleValue());
//返回此Integer的哈希码 它的值即为该 Integer 对象表示的基本 int 类型的数值
System.out.println(inn.hashCode());
//将字符串参数作为有符号的十进制整数进行解析
System.out.println(Integer.parseInt("12345"));
//以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。 还可以返回 8 16 等进制的
System.out.println(Integer.toBinaryString(10));
//将某进制转换为int类型的整数 第一个字符可以为-号
System.out.println(Integer.parseInt("1010", 2));
}
}
6.自动拆箱与自动装箱
1.装箱:将基本数据类型包装成包装类(引用数据类型)的过程
2.拆箱:将包装类(引用数据类型)拆成基本数据类型的过程
3.在jdk1.5之后 有了自动拆装箱
4.自动装箱(将基本数据类型 ==>引用数据类型):也就是可以将基本数据类型变量 赋值给包装类的引用
5.自动拆箱(将引用数据类型 ==>基本数据类型):将包装类的引用 赋值给基本数据类型的变量
package com.qf.test.test01;
public class Test005 {
public static void main(String[] args) {
//定义一个包装类
Integer in = new Integer(5);
int inn = in;//自动拆箱
Integer i = inn;//自动装箱
}
}
7.案例
需求:
分析:
代码:
package com.qf.test.test01;
import java.util.Arrays;
public class Test04 {
public static void main(String[] args) {
//定义一个字符串
String s ="100 15 30 123 413 154 -45 631 71 31 12";
//根据空格分隔字符串
String [] arrays = s.split(" ");
//定义一个Integer数组接收字符串数组
Integer [] in = new Integer[arrays.length];
//使用循环给in数组赋值
for (int i=0;i
四、Math
1.简介
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数 数学函数的操作类
2.常量
3.常用方法
4.方法测试代码
package com.qf.test.test01;
public class Test06 {
public static void main(String[] args) {
//绝对值
System.out.println(Math.abs(-12.5));
//立方根
System.out.println(Math.cbrt(9));
//E值
System.out.println(Math.E);
//π值
System.out.println(Math.PI);
//向下取整
System.out.println(Math.floor(12.5));
//向上取整
System.out.println(Math.ceil(12.5));
//最大值
System.out.println(Math.max(12,94));
//最小值
System.out.println(Math.min(12,15));
//求一个数的几次幂
System.out.println(Math.pow(2,3));
//返回一个0到1之间的随机数 包头不包尾
System.out.println(Math.random());
//四舍五入
System.out.println(Math.round(2.4));
System.out.println(Math.round(2.5));
}
}
五、System
1.简介
A.System 类包含一些有用的类字段和方法。它不能被实例化 因为只提供了一个私有的构造方法
B.在System类提供的方法中,有标准输入、标准输出和标准错误输出流
2.常量
3.常用方法
4.方法测试代码
package com.qf.test.test01;
import java.util.Scanner;
public class Test07 {
public static void main(String[] args) {
//标准输出流
System.out.println("阿京打篮球");
//标准错误输出流
System.err.println("阿京喜欢美女");
//标准输入流 先实例化Scanner
// Scanner input = new Scanner(System.in);
// System.out.println("请输入");
// String lin = input.next();
// System.out.println(lin);
//终止当前正在运行的 Java 虚拟机。非 0 的状态码表示异常终止。
//System.exit();
//获取返回以毫秒为单位的当前时间 当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。
long time = System.currentTimeMillis();
System.out.println(time/1000/60/60);
}
}
六、Date
1.简介
类Date表示特定的瞬间,精确到毫秒 是一个时间类
2.构造方法
构造方法的名称 构造方法的描述 public Date() 初始化date 表示当前系统的时间(重点) public Date(long date) 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数
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 对象表示的毫秒数。
七、Calendar(重点)
1.简介
Calendar类是一个抽象类,他为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法
瞬间可用毫秒值来表示 从1970 年 1 月 1 日的 00:00:00开始计算
2.常量
常量名称 常量的描述 public static final int YEAR 年 public static final int MONTH 月 public static final int DAY_OF_MONTH 日 public static final int HOUR_OF_DAY 时(24) public static final int MINUTE 分 public static final int SECOND 秒
3.常用方法
方法名称 方法描述 public static Calendar getInstance() 返回的 Calendar 基于当前时间 public final Date getTime() 将 Calendar 转换为Date public final void setTime(Date date) 使用给定的 Date 设置此 Calendar 的时间 public void set(int field,int value) 将给定的日历字段设置为给定值 public int get(int field) 返回给定日历字段的值 public abstract void add(int field,int amount) 根据日历的规则,为给定的日历字段添加或减去指定的时间量
4.方法的测试代码
package com.qf.test.test02;
import java.util.Calendar;
import java.util.Date;
public class Test01 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
//获取年
int y = cal.get(Calendar.YEAR);
//获取月 因为系统里面索引为从0-11 所以要+1
int m = cal.get(Calendar.MONTH)+1;
//获取日
int d = cal.get(Calendar.DAY_OF_MONTH);
//获取时
int h = cal.get(Calendar.HOUR_OF_DAY);
//获取分
int mi = cal.get(Calendar.MINUTE);
//获取秒
int s = cal.get(Calendar.SECOND);
//打印输出时间
System.out.println(y+"-"+m+"-"+d+" "+h+":"+mi+":"+s);
//将时间格式set为Data类型 输出的格式比较难看
cal.setTime(new Date());
Date time = cal.getTime();
System.out.println(time);
//设置时间的偏移 例如减少日期中的五天
cal.add(Calendar.DAY_OF_MONTH,-5);
//获取当前日期减掉五天后的数值
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
}
}



