java day07

Java 常用类

  1. 包装类
  2. String类
  3. StringBuffer和StringBuilder类
  4. Date 和 DateFormat 类
  5. Calender 类
  6. JDK8 的日期类
  7. Math 和 Random 类
  8. 枚举
包装类

// 包装类实现基本类型的转换
Integer bb = new Integer(10); //包装类表示的数据
int aa = 10//基本类型的数据
//把String 类型的数据转成 int类型
int i = Integer.parseInt(a);

jdk1.5后 自动装箱和自动拆箱

///基本类型的数据和包装类之间的相互转化
int aa = 10;
//把基本类型的数据转为包装类
Integer bb = new Integer(aa);
//包装类转为基本类型
int cc = bb.intValue();
//自动装箱 基本类型 ---> 包装类
Integer a = 20;
//等价于 
int a = 20 ; Integer b = a ; //底层使用 Integer.ValueOf()
//自动拆箱 包装类 ---->基本类型
int c = b; //底层使用intValue()完成自动拆箱的过程
int a = 10 ;
Integer b = 10;//自动装箱
System.out.println(a==b);//+-*/ == 自动的拆箱操作  true
System.out.println(b.equals(a));//a会自动转包装类  true

Integer aa = new Integer(10);
Integer bb = new Integer(10);
System.out.println(aa==bb); // false == 比较的是内存地址
System.out.println(bb.equals(aa));//true

Integer aaa = 122; //自动装箱 调用valueof()
Integer bbb = 122;
System.out.println(aaa==bbb); // true
System.out.println(bbb.equals(aaa));//true

Integer aaaa = 258; //自动装箱 调用valueof() low=-128 high=127
Integer bbbb = 258;
System.out.println(aaaa==bbbb)// false 258 >255
字符串相关类

String类

string类是对象代表不可变的字符序列

String 类是final 类,不可修改不继承,底层是char数组,jdk9时string变为byte数组,节省空间,同时通过一个coder成员变量作为编码格式的标识

//书写字符串方式,创建
String a = "jjjjbf.hlk";//常量值,指向堆
String b = new String("jjjj");
/*********************字符串简单方**********************/
//操作string对象
System.out.println(a.length());//返回字符串长度
System.out.println(a.isEmpty());//判断是否为空
System.out.println(a.startsWith("j"));//以什么开始 Boolean
System.out.println(a.endsWith("bf"));//以什么结束
System.out.println(a.toUpperCase());//所有英文转大写,不改变a原始值,只是当前a转大写
System.out.println(a); // jjjjbf
System.out.println(a.toLowerCase());//所有英文转小写
...
/*********************字符串查找/截取*******************/
System.out.println(a.charAt(4));//string底层是一个char类型数组,返回的是指定下标对应的字符
System.out.println(a.indexOf("b"));//通过内容找下标,只返回查询的第一个元素下标,查不到返回-1
System.out.println(a.indexOf("b",4));//从指定位置查询下标
System.out.println(a.lastIndexOf("j"));//从最后一个位置查找
System.out.println(a.substring(5));//从下标为5的位置开始截取字符串 一直到结束
System.out.println(a.substring(5,7));//从5开区间,7闭区间,指定截取字符串的开始和结束下标
//文件名称
Sring uuid = UUID.randomUUID().toString();
...
/*********************字符串中转化方法*******************/
String str = "bjsxt";
byte[] bytes = str.getBytes();

System.out.println(Arrays.toString(bytes));

String str2 = new String(bytes,"GBK");//注意抛出异常,给指定字符串进行重新编码

/*****************字符串拼接、替换、查询*******************/
String str3 = "B-J-K";
System.out.println(str3.contains("B")); // true,是否包含指定字符串
System.out.println(str3.replace("J","h"));//替换指定值,replaceAll()替换所有
System.out.println(str3.concat("uu"));//字符串连接,与+有区别,效果是相同的
System.out.println(str3.split("-"));//分割,按指定字符进行分割,返回一个string数组

String str4 = " b c k ";
System.out.println(str4.trim());//去除字符串首尾空格,中间不会

/*****************字符串比较******************/
String w = "bas";
String x = "bas";
System.out.println(w.equals(x));//是否相等,比较值
System.out.println(w.compareTo(x));//比较大小,底层做减法,比较的是长度
System.out.println(w.compareToIgnoreCase(x));//比较大小忽略大小写

StringBuffer / StringBuilder类

均代表可变的字符序列,都是抽象类AbstractStringBuilder的字类,几乎一样;

StringBuffer 提供的类,线程安全,做线程同步检查,效率较低;

StringBuilder提供的类,线程不安全,不做线程同步检查,因此效率较高,推荐使用

/*****************StringBuilder使用方法***********************/

String str = "jjj"
StringBuilder builder = new StringBuilder("fdd");
builder.append(str); //拼接,比string.concat()效率高
builder.insert(3"d");//可以在指定位置追加内容
builder.charAt(2);//取出指定下标字符
builder.setCharAt(3,"k");//给指定位置设置字符
builder.replace();//替换
builder.deleteCharAt();//删除指定位置字符
builder.delete(1,3)//删除多个字符,1开3闭
builder.reverse();// 倒叙输出字符串   

//String类转stringBuilder类型  
StringBuilder builder1 = new StringBuilder(str);
//stringBuilder类转String类型 
string s = builder.toString();  
               
/**********************StringBuilder源码*************************/   
// StringBuilder底层默认char数组长度为16
StringBuilder builder = new StringBuilder("12345");// str.length + 16
System.out.println(builder.length());// 5 真实内容长度
System.out.println(builder.capacity());//21 底层数组长度
// StringBuilder底层扩容机制 原有数组长度*2+2
builder.append("6");
System.out.println("底层扩容长度:"+builder.capacity());//44

String 常用面试题

String a ="a",b="b",c="c",d="d";
String str = "abcd";
//字符串拼接时,若全部时常量,和直接写一个结果的含义时一致
String str2 = "a"+"b"+"c"+"d";// 创建了1个对象

String str3 = a+b+c+d;// 创建了1个对象,底层是StringBuilder优化机制
StringBuilder builder = new StringBuilder();
builder.append(a);
builder.append(b);
builder.append(c);
builder.append(d);

System.out.println(str == Str2);// true
System.out.println(str2 == Str3);// false
日期类

Date类,已过时

/*******************unit包下****************************/
Date date = new Date();//底层 this(System.currentTimeMillis())
System.out.println(date);
System.out.println(date.getYear());//1900开始
System.out.println(date.getMonth());//0-11
System.out.println(date.getDate());//当前月第几天
System.out.println(date.getDay());//此日期表示的一周的某一天
System.out.println(date.getHour());//小时 24h
System.out.println(date.getMinutes());//分
System.out.println(date.getSeconds());//秒

System.out.println(date.getTime());//1970开始到现在的秒数
System.out.println(date.toLocaleString());//年月日时分秒

/****************date中三个字类****************************/
/*
*sql包下的类:
*Date:只有年月日
*Time:只有时分秒
*TimeStamp:时间戳,年月日时分秒
*/
java.sql.Date date1 = new java.sql.Date(System.currentTimeMillis());
System.out.println(date1);// 2022-05-09

String d = "2019-02-16";
Date date = Date.valueOf(d);//字符串转date

DateFormat类

//DateFormat作用:格式化日期

/********************Date转string********************************/
Date date = new Date();
//设置时间格式
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//调用格式化方法
String format = dateformat.format(date);
System.out.println(format);

/********************String转date********************************/
String str = "2019-04-09 09:08:07";
DateFormat dateformat1 = = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date parse = dateformat1.parse(str); //要抛出异常
System.out.println(parse);

Calendar日历类(jdk9,过时)

//Calendar 抽象类
Calendar calender = new GregorianCalendar();
System.out.println(calender);//打印包含元素

System.out.println(calender.get(1));//1 YEAR,2 MONTH,...
System.out.println(calender.get(Calendar.YEAR));
System.out.println(calender.get(Calendar.MONTH));
System.out.println(calender.get(Calendar.DAY_OF_MONYH));
System.out.println(calender.get(Calendar.HOUR));

calender.set(1,2022); //修改年

int max = calender.getActualMaximun(Calendar.DATE);// 本月多少天

/*********Calendar与Date类的相互转化******************/
Date date = new Date();
calender.setTime(date);//日期类转日历类

Date time = calender.getTime();//日历类转日期类

JDK8 新日期

//instant类
Instant now = Instant.now();
System.out.println(now);
System.out.println(now.MAX);
System.out.println(now.MIN);
System.out.println(now.EPOCH);
//LocalDate类 这个日期只包含年月日
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
//DateTimeFormatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String format = localDate.format(dtf);//LocalDate转String
LocalDate parse = LocalDate.parse("2022年05月30日");//String转LocalDate
Math类

提供一系列静态方法用于科学计算,其方法的参数和返回值类型一般为double型。

//数学类对象
System.out.println(Math.PI);//圆周率
System.out.println(Math.E);//log
System.out.println(Math.round(3.12));//四舍五入 3
System.out.println(Math.ceil(3.13));//向上取整 4
System.out.println(Math.floor(3.9));//向下取值 3
System.out.println(Math.random());//随机数[0,1)
System.out.println(Math.sqrt(64));//开平方
System.out.println(Math.pow(2,5));//幂
System.out.println(Math.abs(-5));//绝对值
System.out.println(Math.max(30,40));//最大值
System.out.println(Math.min(30,40));//最小值
Random类
//产生一个随机数,0-9内的随机整数
Math.floor(Math.random() * 10);
// 只要种子数(new Random(10))和nextInt()中的参数一致,每次生成随机数都是一样的(伪随机数)
Random random = new Random();
random.nextInt(10);//0-10内随机整数
random.nextInt(9000) + 1000; //获得随机4位整数,验证码
...
Enum类
//枚举,每个被枚举的成员实质就是一个枚举类型的实例,默认static final修饰
//建枚举类
public enum Gender{
    男,女
}
//建学生类
public class Student{
   private Gender sex;
    
   public Student(Gender sex){
       this.sex = sex;
   }
   
   public Gender getSex(){
       return sex;
   }
   
   public void setSex(Gender sex){
       this.sex = sex;
   }
}
//测试运行
public static void main(String[] args){
    Student s = new Student(Gender.男)
}
posted @   MrGeng  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示