Java—实用类

一.包装类

为什么要使用包装类?

1.可以实现基本类型之间的转换

2.便于函数传值(泛型时不可以传基本数据类型)

3.在某处用到Object类型是时,方便将基本数据类型转换。

作用:

1.包装类把基本数据类型转换为对象(每个基本类型的数据在java.lang包中都有一个相应的包装类。)

2.提供了一系列的实用方法。

3.集合不允许存放基本数据类型的数据,存放数字时,要用包装类型。

构造方法:

1.所有的包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例。

//所有的包裝类都可将与之对应的基本数据类型作为参数,来构造他们的实例
Integer i = new Integer(12);
Double d = new Double(12.1);
Boolean b = new Boolean(true); //不分大小写true,否则是false
Character c = new Character('a');
System.out.println(i+" "+d+" "+b+" "+c);

2.除Character类外,其他包装类可将一个字符串作为参数构造它们的实例

 
//除了Character外,其他包装类可以以一个字符串作为参数来构造它的实例
   Integer i = new Integer("12");
   Double d = new Double("12.1");
   Boolean b = new Boolean("true"); //不分大小写true,否则是false
   //Character c = new Character("a");  错误的
   System.out.println(i+" "+d+" "+b);
 

3.注意:

1.Boolean类构造方法参数为String类型时,若该字符串内容为
true(不考虑大小写),则该Boolean对象表示true,否则表示false。
2.当Number包装类构造方法参数为String 类型时,字符串不能为null,
且该字符串必须可解析为相应的基本数据类型的数据,否则编译通过,运行时NumberFormatException异常。

 Integer i = new Integer(null);
   Double d = new Double("包装类");
   System.out.println(i+" "+d);

常用的方法:

1.XXX.valueOf()基本数据类型转换成包装类(String类型参数时除Character外)

//除Character类外,其他包装类valueOf(String s)
   intValue=Integer.valueOf("32");
   //bool=Boolean.valueOf("true");
   bool=Boolean.valueOf("love");
   //编译错误
   //Character c=Character.valueOf("a");
   System.out.println(intValue);
   System.out.println(bool);
   System.out.println("*************************");

2.xxValue():将包装类转换成基本数据类型

//xxValue() 将包装类转换为基本数据类型的值
   Integer intValue=new Integer(12);
   int intValue2 = intValue.intValue();
   System.out.println(intValue2);

3.toString()以字符串形式返回包装对象表示的基本类型数据

//toString()以字符串的形式返回包装对象表示的基本数据类型
   String id = Integer.toString(12);
   String c = Character.toString('c');
   System.out.println(id+" "+c);
   
   //和字符串相拼接会变成字符串的形式
   String sex='男'+"";
   String num=99+"";
   System.out.println(sex+" "+num);

4.parseXX():将字符串转换成相应的基本数据类型的数据(Character除外)

 //parseXX()把字符串转换成相应的基本数据类型数据(Character除外)
    int parseInt = Integer.parseInt("21");
    System.out.println(parseInt);
 

5.装箱、拆箱。

装箱:将基本数据类型转化为包装类;拆箱:是将包装类转化为基本类型。

 //装箱
   Integer  intObj=5;
   //拆箱
   int value=intObj;

特点:

1.所有的包装类都是final类型、不可以创建它们的子类

2.JDK1.5后,允许基本类型和包装类进行混合数学运算。

3.包装类并不是用来取代基本数据类型的:在基本数据类型需要对象时使用。

String类主要包含:

1.length();获取字符串的长度。

2.equals():比较内容是否相同。

3.字符串常用的提取方法:

 4.split():截取字符串()

String words="长亭外 古道边 芳草碧连天 晚风扶 柳笛声残 夕阳山外山";
   String[] word=new String[300];
   System.out.println("原歌词形式
"+words);
   System.out.println("拆分后的样式为:");
   word=words.split(" ");
   for (int i = 0; i < word.length; i++) {
    System.out.println(word[i]+" ");
   }

5.StringBuffer类;

1.声明:

StringBuffer str1= new StringBuffer();
StringBuffer str2= new StringBuffer("aaa");

2.使用:

sb.toString(); //转化为String类型
sb.append("**"); //追加字符串
sb.insert (1, "**"); //插入字符串

String与StringBuffer的比较:

String是不可变对象
经常改变内容的字符串最好不要使用String
StringBuffer是可变的字符串
字符串经常改变的情况可使用StringBuffer,更高效
JDK1.5后提供了StringBuilder,等价StringBuffer
 

Math类:

1.作用:提供了常用的数学方法和两个静态常量(E和PI)

2.在java.lang.Math包中

3.常用的方法:

Math.abs(-3.5); //返回3.5  
Math.max(2.5, 90.5);//返回90.5
int random = (int) (Math.random() * 10); //生成一个0-9之间的随机数

Random类:

1.在java.util.Random包中

2.产生随机数:

Random rand=new Random(); //创建一个Random对象
for(int i=0;i<20;i++){//随机生成20个随机整数,并显示
        int num=rand.nextInt(10);//返回下一个伪随机数,整型的
    System.out.println("第"+(i+1)+"个随机数是:"+num);
} 
 

操作日期类型:

1.java.uile.Date包中

2.表示日期和时间

3.提供操作日期和时间的各个组成方法。

4.Calendar类

抽象类,java.util.Calendar

用于设置和获取日期/时间数据的特定部分

equals()   和  ==   的区别

 ==比较的是地址值。

equals() 比较的是内容。


原文地址:https://www.cnblogs.com/haoxz258988/p/12952055.html