java字符串包装类_java学习笔记——包装类(Wrapper)、字符串处理类 及 其他常用类的使用-Go语言中文社区...

f33456584e90c687eda0133332da41d0.png

6a7588aa246948ad2524fc6cdb2bbcd7.png

482f3387581cdc60e1b86642635776c5.png

93e37b19d6a646e168ee1627855b0102.png

128b2ecdae88335b450d0686329bac24.png

* 二、基本数据类型与包装类之间的转换

* 装箱:将基本数据类型转换成对应的包装类。

* ①使用对应包装类的构造器

* ②使用对应包装类的静态方法 valueOf()

*

* 拆箱:将包装类转换成对应的基本数据类型。

* ①通过对应包装类对象的 xxxValue().    xxx: 对应的基本数据类型

* 三、基本数据类型、包装类  与  String 之间的转换

* 1. 基本数据类型、包装类  转  String

* ①String str = i + "";

* ②通过对应包装类的静态方法 toString()

* ③通过 String 类的静态方法 valueOf()

*

* 2. String 转 基本数据类型、包装类

* ①使用对应包装类的构造器]

* ②使用对应包装类的静态方法 parseXxx() 方法。 Xxx:基本数据类型  注意:没 parseChar()

* ③使用对应包装类的静态方法 valueOf()

四、自动装箱与自动拆箱(jdk1.5后)

Byte Short Integer Long Character 提供的一个小的缓存,该缓存的取值范围在(-128~127)之间

若需要装箱的值在该取值范围内,则从缓冲中取一个实例

若需要装箱的值超出该取值范围内,则重新 new 一个新的实例。

//自动装箱

Integer num1 = 100;

//自动拆箱

int i = num1;

Integer num2 = 100;

System.out.println(num1 == num2);//true

Integer num3 = 128;

Integer num4 = 128;

System.out.println(num3 == num4);//false

8e26d047c2c0946886cc379c923e4e2f.png

fbd35d36e865b74a544823ac67067f2d.png

13c3b9de855d753e17dab400e581fe79.png

一、java.lang.String 类:不可变的字符序列

String str1 = "abc";

String str2 = new String("abc");

str1:代表一个对象,至少在内存中开辟一块内存空间

str2:代表两个对象,至少在内存中开辟两块内存空间

* 1. 获取字符串的方法:

*         ①String concat(String str):串接字符串

*         ②String substring(int beginIndex):获取取字符串的子串

*           String substring(int beginIndex, endIndex) : 包含头不包含尾

*         ③String toLowerCase()和String toUpperCase():转换为小写/大写

*         ④String trim():删除首尾空格或制表符

* 2. 搜索方法:

*         ①int indexOf(int ch) : 获取指定字符在字符串中的位置,若没有指定的字符,返回 -1

*          int indexOf(int ch, int fromIndex) : 从指定位置开始搜索

*          int indexOf(String str)

*          int indexOf(String str, int fromIndex)

*          int lastIndexOf(int ch) : 反向获取指定字符位置

* 3. 判断方法:

*      ① boolean equals(Object obj):判断是否相等

*         boolean equalsIgnoreCase(String str):判断是否相等,不考虑大小写

*      ② boolean contains(String str) :判断是否包含某字符串

*           ③ boolean startsWith(String str)和 boolean endsWith(String str):判断是否以指定字符串开始/结尾

*           ④ boolean isEmpty():判断字符串是否为空

* 4. 其它方法:

*         ①length():返回字符串长度

*         ②char charAt(int index):返回索引处的字符

*         ③将字符数组转换为字符串

*             构造器:

*                   String(char[] ch)

*                   String(char[] ch, offset, count) : 将数组中一部分转换为字符串

*             静态方法:

*                   static String copyValueOf(char[] ch)

*                   static String copyValueOf(char[] ch, offset, count)

*                   static String valueOf(char[])

*          将字符串转换字符数组: char[] toCharArray()

*         ④String replace(char oldCahr, char newCahr) : 替换字符串中字符

*           String replace(String oldStr, String oldStr):替换字符串中字符串

*        ⑤String[] split(String r):根据指定符号切割

*

二、StringBuffer 和  StringBuilder  : 可变的字符序列

StringBuffer 和  StringBuilder 的区别? 二者具备兼容 API

StringBuffer 是线程安全的,因此效率低

StringBuilder 是线程不安全的,因此效率高

* StringBuffer 和 StringBuilder 的常用方法:

删除,替换,截取全部是左闭右开

*     ① StringBuffer append(String str) : 添加

*      StringBuffer insert(int offset, String str) : 插入

*      StringBuffer replace(int start, int end, String str):替换

*

*  ② int indexOf(String str) :返回子串的位置索引

*      int lastIndexOf()

*

*  ③ String substring(int start, int end):取子字符串序列

*  ④ StringBuffer delete(int start, int end):删除一段字符串

*      StringBuffer deleteCharAt(int index):删除指定位置字符

*  ⑤ String toString():转换为String对象

ecddc67ac5b1ae82c87273e54a7765f7.png

6f9bb94c6013478f93947bd9ac8554c3.png

770d9f54cfd1b4c1b13f9ce9e2b84481.png

a5d62863a4763dbb876d3a818421929c.png

其他常用类

一、

【重要】

java.util.Date : 表示时间/日期,表示一个特定的瞬间,精确到毫秒

java.text.DateFormat : 用于解析/格式化时间或日期,但是是一个抽象类

|--java.text.SimpleDateFormat : 是 DateFormat 的子类

Date date = new Date();

System.out.println(date); //Wed Apr 19 09:04:45 CST 2017

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS E");

String strDate = sdf.format(date);

System.out.println(strDate);//2017-04-19 09:06:59 701 星期

Date newDate = sdf.parse(strDate);

System.out.println(newDate);

二、

java.lang.Math :用于数学运算

double ceil(double d) : 返回不小于d的最小整数

double floor(double d): 返回不大于d的最大整数

int round(float f) : 返回最接近f的int型(四舍五入)

long round(double d):返回最接近d的long型

double abs(double d):

double max(double d1, double d2) : 返回较大值

int min(int i1, int i2) : 返回较小值

double random() : 返回一个大于等于0.0并且小于1.0的随机数

三、

java.lang.System :

java.util.Calendar : 日历类

java.math.BigInteger : 支持任意精度的整数

java.math.BigDecimal : 支持任意精度的定点数

c77ab7c2a5747182cd78b7f311ff22dd.png

b61b83eab7da7a68ef9cba6df2129d03.png

89c1d54711c71d562ca7f95423097605.png

e921f86bfe92e35e4ac9f1596818ddbd.png

59204c02d0626e1eb3f0bc366749a6e7.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值