字符串以及一些基本类型与引用类型的转换

字符串

理解:字符串为不可改变的量,一般存储在方法区中方便内存的优化

用法:

  1. 用=直接赋值 ,结果同一储存在方法区中,不用开辟新的空间
String a= 10;
String str = "abc"+"d";
  1. 使用运算符运算结果,直接在堆中直接开辟空间,地址不同 必须得用equals才能判断其中的内容是否相等
String a = "abcd";
String b ="abc"+"d";
a.eqauls(b);
  1. 总:字符串为不可改变的量,存在方法区中,
    1. 推荐省略的形式,避免创建多个对象
    2. 变量操作运算,都会在堆中开辟对象
    3. 推荐使用,访问常量池的地址intern() 一般来说,字符串使用常量池
String msg2 ="abcd";

**split **
截取字符串,结果储存在一个数组中,类似于从中间截取2断

String msg ="laope@sxt.con";
String [] arr = msg.split("@");
System.out.println(arr[0]);
System.out.println(arr[1]);

包装类

理解:基本类型装换成引用类型

	基本类型	    引用类型
    int	        Integer
    double	    Double
    char	    Character
    boolean	    Boolean
    byte	    Byte
    short	    Short
    long	    Long
    float	    Float

装箱

int -> Integer 通过其中的valueof() 来转换

int num=5;
Integer numInt= Integer.valueOf(num);

int ->String  //在运算时带有字符串
String ->Integer
numInt=Integer.valuesOf("5555");

拆箱

把引用类型数据转化为基本类型数据,通过intValueI()方法实现

//自动装箱
Integer numInt = 5; //Integer.valueOf(5)
//自动拆箱
int num = numInt; //numInt.intValue()

在这里要注意一下拆箱失败(空指针异常)

//拆箱失败 编译蜜糖
Integer numInt2 = null;
num = numInt2.intValue(); //拆箱 java.lang.NullPointerException

IntegerCache: 缓存 [-128,127] 整数对象放到缓存池中,不需要每次new. .

含义:常用的放入池中,不常用用的时候再创建

日期

理解:在计算机中日期一般用长整型来表示
jdk8之前

  1. Date : new Date() getTime() setTime()

  2. SimpleDateFormat: yyyy-MM-dd HH:mm:ss

  3. 1)、format(): 格式化为字符串
    2)、parse(): 转成日期
    jdk1.8之后

LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String nowStr = now.format(format); //格式化日期字符串
		System.out.println(nowStr);
		
		//转成日期
		String dateStr= "2020-01-11 11:23:04";
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime date= LocalDateTime.parse(dateStr, formatter);
		System.out.println(date.getDayOfWeek());

枚举

常量的集合,比如星期,月份啊,游戏之类的
用法:定义类为
enum 常量数据类型且不可修改
1)、继承体系
2)、构造器:
有: new
不让用: 不需要的对象 | 有方法返回对象
3)、方法: 是否为静态 方法签名(名称+类型)+返回值 作用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值