Java之常用类

常用类

包装类

当要使用只针对对象设计的API或新特性(例如泛型),那么基本数据类型的数据就需要用包装类来包装。

序号

基本数据类型

包装类

1

byte

Byte

2

short

Short

3

int

Integer

4

long

Long

5

float

Float

6

double

Double

7

char

Character

8

boolean

Boolean

9

void

Void

装箱与拆箱

JDK1.5之后,可以自动装箱与拆箱。

注意:只能与自己对应的类型之间才能实现自动装箱与拆箱。

Integer i = 1;

Double d = 1;//错误的,1int类型

装箱:把基本数据类型转为包装类对象。

转为包装类的对象,是为了使用专门为对象设计的API和特性

拆箱:把包装类对象拆为基本数据类型。

转为基本数据类型,一般是因为需要运算,Java中的大多数运算符是为基本数据类型设计的。比较、算术等

总结:对象(引用数据类型)能用的运算符有哪些?

1instanceof

2=:赋值运算符

3==!=:用于比较地址,但是要求左右两边对象的类型一致或者是有父子类继承关系。

4)对于字符串这一种特殊的对象,支持“+”,表示拼接。

包装类的一些API

1、基本数据类型和字符串之间的转换

1)把基本数据类型转为字符串

int a = 10;

//String str = a;//错误的

//方式一:

String str = a + "";

//方式二:

String str = String.valueOf(a);

2)把字符串转为基本数据类型

int a = Integer.parseInt("整数的字符串");

double a = Double.parseDouble("小数的字符串");

boolean b = Boolean.parseBoolean("truefalse");

2、数据类型的最大最小值

Integer.MAX_VALUEInteger.MIN_VALUE

Long.MAX_VALUELong.MIN_VALUE

Double.MAX_VALUEDouble.MIN_VALUE

3、转大小写

Character.toUpperCase('x');

Character.toLowerCase('X');

4、转进制

Integer.toBinaryString(int i) 

Integer.toHexString(int i)

Integer.toOctalString(int i)

 字符串

 字符串的特点

1、字符串String类型本身是final声明的,意味着不能继承String

2、字符串的对象也是不可变对象,意味着一旦进行修改,就会产生新对象

我们修改了字符串后,如果想要获得新的内容,必须重新接受。

如果程序中涉及到大量的字符串的修改操作,那么此时的时空消耗比较高。可能需要考虑使用StringBuilderStringBuffer

3String对象内部是用字符数组进行保存的

JDK1.9之前有一个char[] value数组,JDK1.9之后byte[]数组

4String类中这个char[] values数组也是final修饰的,意味着这个数组不可变,然后它是private修饰,外部不能直接操作它,String类型提供的所有的方法都是用新对象来表示修改后内容的,所以保证了String对象的不可变。

5、就因为字符串对象设计为不可变,那么所以字符串有常量池来保存很多常量对象

常量池在方法区。

如果细致的划分:

1JDK1.6及其之前:方法区

2JDK1.7:堆

3JDK1.8:元空间

字符串对象的比较

1==:比较是对象的地址

只有两个字符串变量都是指向字符串的常量对象时,才会返回true

String str1 = "hello";
String str2 = "hello";
str1 == str2//true

2equals:比较是对象的内容,因为String类型重写equals,区分大小写

只要两个字符串的字符内容相同,就会返回true

String str1 = new String("hello");
String str2 = new String("hello");
str1.equals(strs) //true

3equalsIgnoreCase:比较的是对象的内容,不区分大小写

String str1 = new String("hello");
String str2 = new String("HELLO");
str1.equalsIgnoreCase(strs) //true

4compareToString类型重写了Comparable接口的抽象方法,自然排序,按照字符的Unicode编码值进行比较大小的,严格区分大小写

String str1 = "hello";
String str2 = "world";
str1.compareTo(str2) //小于0的值

5compareToIgnoreCase:不区分大小写,其他按照字符的Unicode编码值进行比较大小

String str1 = new String("hello");
String str2 = new String("HELLO");
str1.compareToIgnoreCase(str2)  //等于0

空字符的比较

1、哪些是空字符串

String str1 = "";
String str2 = new String();
String str3 = new String("");

空字符串:长度为0

2、如何判断某个字符串是否是空字符串

if("".equals(str))

if(str!=null  && str.isEmpty())

if(str!=null && str.equals(""))

if(str!=null && str.length()==0)

字符串的对象的个数

1、字符串常量对象

String str1 = "hello";//1个,在常量池中

2、字符串的普通对象

String str2 = new String();
String str22 = new String("");
//两个对象,一个是常量池中的空字符串对象,一个是堆中的空字符串对象

3、字符串的普通对象和常量对象一起

String str3 = new String("hello");
//str3首先指向堆中的一个字符串对象,然后堆中字符串的value数组指向常量池中常量对象的value数组

字符串拼接结果

原则:

1)常量+常量:结果是常量池

2)常量与变量 变量与变量:结果是堆

3)拼接后调用intern方法:结果在常量池

@Test
public void test06(){
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";

String s4 = (s1 + "world").intern();//把拼接的结果放到常量池中
String s5 = (s1 + s2).intern();

System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//true
}

@Test
public void test05(){
final String s1 = "hello";
final String s2 = "world";
String s3 = "helloworld";

String s4 = s1 + "world";//s4字符串内容也helloworlds1是常量,"world"常量,常量+ 常量 结果在常量池中
String s5 = s1 + s2;//s5字符串内容也helloworlds1s2都是常量,常量+ 常量 结果在常量池中
String s6 = "hello" + "world";//常量+ 常量 结果在常量池中,因为编译期间就可以确定结果

System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//true
System.out.println(s3 == s6);//true
}

@Test
public void test04(){
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";

String s4 = s1 + "world";//s4字符串内容也helloworlds1是变量,"world"常量,变量 + 常量的结果在堆中
String s5 = s1 + s2;//s5字符串内容也helloworlds1s2都是变量,变量 + 变量的结果在堆中
String s6 = "hello" + "world";//常量+ 常量 结果在常量池中,因为编译期间就可以确定结果

System.out.println(s3 == s4);//false
System.out.println(s3 == s5);//false
System.out.println(s3 == s6);//true
}

 字符串的API

1boolean isEmpty()

2int length()

3String concat(xx):拼接,等价于+

4boolean contanis(xx)

5int indexOf():从前往后找,要是没有返回-1

6int lastIndexOf():从后往前找,要是没有返回-1

7char charAt(index)

8new String(char[] ) new String(char[] ,int, int)

9char[] toCharArray()

10byte[] getBytes():编码,把字符串变为字节数组,按照平台默认的字符编码进行编码

       byte[] getBytes(字符编码方式):按照指定的编码方式进行编码

11new String(byte[] ) new String(byte[], int, int):解码,按照平台默认的字符编码进行解码

new String(byte[],字符编码方式 ) new String(byte[], int, int,字符编码方式):解码,按照指定的编码方式进行解码

12String subString(int begin):从[begin]开始到最后

String subString(int begin,int end):从[begin, end)

13boolean matchs(正则表达式)

14String replace(xx,xx):不支持正则

String replaceFirst(正则,value):替换第一个匹配部分

String repalceAll(正则, value):替换所有匹配部分

15String[] split(正则):按照某种规则进行拆分

16boolean startsWith(xx):是否以xx开头

boolean endsWith(xx):是否以xx结尾

17String trim():去掉前后空白符,字符串中间的空白符不会去掉

18String toUpperCase():转大写

19String toLowerCase():转小写

可变字符序列

1、可变字符序列:StringBuilderStringBuffer

StringBuffer:老的,线程安全的(因为它的方法有synchronized修饰)

StringBuilder:线程不安全的

2、常用的APIStringBuilderStringBufferAPI是完全一致的

1append(xx):拼接,追加

2insert(int index, xx):插入

3delete(int start, int end)

deleteCharAt(int index)

4set(int index, xx)

5reverse():反转

日期时间API

JDK1.8之后

java.time及其子包中。

1LocalDateLocalTimeLocalDateTime

1now():获取系统日期或时间

2of(xxx):或者指定的日期或时间

3)运算:运算后得到新对象,需要重新接受

plusXxx():在当前日期或时间对象上加xx

minusXxx() :在当前日期或时间对象上减xx

方法

描述

now() / now(ZoneId zone)

静态方法,根据当前时间创建对象/指定时区的对象

of()

静态方法,根据指定日期/时间创建对象

getDayOfMonth()/getDayOfYear()

获得月份天数(1-31) /获得年份天数(1-366)

getDayOfWeek()

获得星期几(返回一个 DayOfWeek 枚举值)

getMonth()

获得月份, 返回一个 Month 枚举值

getMonthValue() / getYear()

获得月份(1-12) /获得年份

getHours()/getMinute()/getSecond()

获得当前对象对应的小时、分钟、秒

withDayOfMonth()/withDayOfYear()/withMonth()/withYear()

将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象

with(TemporalAdjuster t)

将当前日期时间设置为校对器指定的日期时间

plusDays(), plusWeeks(), plusMonths(), plusYears(),plusHours()

向当前对象添加几天、几周、几个月、几年、几小时

minusMonths() / minusWeeks()/minusDays()/minusYears()/minusHours()

从当前对象减去几月、几周、几天、几年、几小时

plus(TemporalAmount t)/minus(TemporalAmount t)

添加或减少一个 Duration 或 Period

isBefore()/isAfter()

比较两个 LocalDate

isLeapYear()

判断是否是闰年(在LocalDate类中声明)

format(DateTimeFormatter t)

格式化本地日期、时间,返回一个字符串

parse(Charsequence text)

将指定格式的字符串解析为日期、时间

2DateTimeFormatter:日期时间格式化

该类提供了三种格式化方法:

预定义的标准格式。如:ISO_DATE_TIME;ISO_DATE

本地化相关的格式。如:ofLocalizedDate(FormatStyle.MEDIUM)

自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)

@Test
public void test10(){
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter df = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
String str = df.format(now);
System.out.println(str);
}

@Test
public void test9(){
LocalDateTime now = LocalDateTime.now();

DateTimeFormatter df = DateTimeFormatter.ISO_DATE_TIME;
String str = df.format(now);
System.out.println(str);
}


@Test
public void test8(){
LocalDateTime now = LocalDateTime.now();

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd HHmmss SSS毫秒 E 是这一年的D");
String str = df.format(now);
System.out.println(str);
}


@Test
public void test7(){
LocalDate now = LocalDate.now();
LocalDate before = now.minusDays(100);
System.out.println(before);
}

@Test
public void test06(){
LocalDate lai = LocalDate.of(2019, 5, 13);
LocalDate go = lai.plusDays(160);
System.out.println(go);
}

@Test
public void test05(){
LocalDate lai = LocalDate.of(2019, 5, 13);
System.out.println(lai.getDayOfYear());
}



@Test
public void test04(){
LocalDate lai = LocalDate.of(2019, 5, 13);
System.out.println(lai);
}


@Test
public void test03(){
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
}


@Test
public void test02(){
LocalTime now = LocalTime.now();
System.out.println(now);
}


@Test
public void test01(){
LocalDate now = LocalDate.now();
System.out.println(now);
}

 

猜你喜欢

转载自blog.csdn.net/Brevity6/article/details/92851854