Java包装类
为什么需要包装类(Wrapper Class)
java语言是一个面向对象的语言,但是java中的基本数据类型却不是面向对象的,我们在实际使用中经常将基本数据类型转换成对象,便于操作。比如,集合的操作中,这时,我们就需要将基本类型数据转化成对象!
包装类和基本数据类型的关系
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
long | Long |
int | Integer |
char | Character |
float | Float |
double | Double |
boolean | Boolean |
包装类的基本操作
以Integer为例
package com.pojo;
public class TestInteger {
public static void main(String[] args) {
Integer i1=new Integer(123);
Integer i2=new Integer("123");
System.out.println("i1=i2:"+(i1==i2));
System.out.println("i1.equals(i2):"+i1.equals(i2));
System.out.println("i2:"+i2);
System.out.println("i2.toString():"+i2.toString());
//返回一个整数的16进制形式字符串
int i=54321;
System.out.println("Integer.toString(int i,int radix):"+Integer.toString(i,16));
Integer i3=new Integer(128);
//int compareTo(Integer anotherInteger)
// 比较两个整数。相等时返回0;小于时返回负数;大于时返回正数
System.out.println("i1与i3的值的比较:"+i1.compareTo(i3));
System.out.println("i1与i2的值的比较:"+i1.compareTo(i2));
System.out.println("i3与i2的值的比较:"+i3.compareTo(i2));
//Integer-->int 包装对象.intValue()
int ii=i1.intValue();
System.out.println("ii====="+ii);
System.out.println(Integer.max(10,20));
//int-->Integer
Integer i4=Integer.valueOf(123);
System.out.println("i4==="+i4);
//String-->int 包装类类名.parseInt(String s)
Integer iii=Integer.parseInt("234");
System.out.println("iii===="+iii);
//int-->String
String str=ii+"";
String s=String.valueOf(ii);
System.out.println("s====="+s);
//String --> Integer
Integer i5=new Integer("13");
//Integer --> String
String sss=i5.toString();
System.out.println("sss===="+sss);
}
}
String字符串类
package com.pojo;
public class TestString {
public static void main(String[] args) {
String s=new String("write once, run anywhere!");
String ss=new String("run");
//找到第一个‘r’的索引下标
System.out.println("s.indexOf('r'):"+s.indexOf('r'));
//找到第二个‘r’的索引下标
System.out.println("s.indexOf('r',2):"+s.indexOf('r',2));
//
System.out.println("s.indexOf(ss):"+s.indexOf(ss));
String s1=new String("abcdefghij");
String s2=new String("ghij");
//判断s1是否是s2字符串的结尾
System.out.println("s1.endsWith(s2):"+s1.endsWith(s2));
//获取从指定位置起的子串复制到字符数组中,比如 s1.getChars( 0, 5, charArray, 0 );四个参数的含义:
// 1)被拷贝字符在字串中的起始位置。
// 2)被拷贝的最后一个字符在字串中的下标再加1。
// 3)目标字符数组。
// 4)拷贝的字符放在字符数组中的起始下标。
char[] s3={'I',' ','i','o','v','e',' ','h','e','r','!'};
String s4=new String("you!");
s4.getChars(0,3,s3,7);
System.out.println(s3);
//字符串的不变性
String t1=new String("hello");
//将save连接在t1的后边
t1.concat("save");
System.out.println("t1==="+t1);
//使用replace方法把t2中的字符o替换为u
String t2=new String("good morning");
t2.replace('o','u');
//重新定义变量接受 t2的值才能被改变。。。。
System.out.println("t2=="+t2);
}
}
StringBuffer,StringBuilder字符缓冲类
(1):StringBuffer:效率低,安全性高;StringBuilder:效率高,安全性低;
(2):StringBuilder 的常用方法使用
-
append();
-
delete();
-
insert();
-
StringBuilder();
package com.pojo; public class TestStringBuffer { public static void main(String[] args) { StringBuilder sb=new StringBuilder(); //字符串的追加 sb.append("hello"); sb.append(true); sb.append('你'); sb.append(100); System.out.println("sb.toString():"+sb.toString()); sb.delete(3,5);//含头不含尾; System.out.println("sb===="+sb); sb.deleteCharAt(1);//删除指定位置上的字符 System.out.println("sb--------"+sb); sb.insert(2,'好'); System.out.println("sb++++"+sb); System.out.println(sb.indexOf("t")+"\t"+sb.indexOf("k")); } }
Random随机类
package com.pojo;
import java.util.Random;
public class TestRandom {
public static void main(String[] args) {
Random r1=new Random(50);
System.out.println("第一个种子为50的Random对象");
System.out.println("r1.nextBoolean():\t"+r1.nextBoolean());
System.out.println("r1.nextInt():\t"+r1.nextInt());
System.out.println("r1.nextDouble():\t"+r1.nextDouble());
System.out.println("r1.nextGaussian():\t"+r1.nextGaussian());
}
}
Date时间类
package com.pojo;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
Date date1 = new Date();
Date date2 = new Date(1233997578421L);
//输出date1、date2对象所对应的毫秒数
System.out.println(date1.getTime());
System.out.println(date2.getTime());
//查看date1是否在date2后
boolean isAfter = date1.after(date2);
System.out.println("is date1 after date2:"+isAfter);
date1.setTime(1133997578421L);
isAfter = date1.after(date2);
System.out.println("is date1 after date2:"+isAfter);
}
}
Calendar日历类
-
Calendar 抽象类定义了足够的方法,让我们能够表述Gregorian Calendar的规则。它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并提供了一些方法操作日历字段(例如获得下星期的日期)
-
由于Calendar类是一个抽象类,所以不能直接通过new关键字创建Calendar类的实例,可以借助于该类提供的静态方法getInstance()来获得一个Calendar对象
package com.pojo;
import java.util.Calendar;
import java.util.Date;
public class TestCalendar {
public static void main(String[] args) {
//创建Calendar实例
Calendar ca=Calendar.getInstance();
System.out.println("year is:"+ca.get(Calendar.YEAR));
ca.add(Calendar.YEAR,2);//为年份增加2
System.out.println("year is:"+ca.get(Calendar.YEAR));
ca.set(Calendar.YEAR,2009);//给ca年份
System.out.println("year is:"+ca.get(Calendar.YEAR));
//今天是今年的第几天
System.out.println("day of year:"+ca.get(Calendar.DAY_OF_YEAR));
//今天是本周的第几天,注意默认情况下周日是第一天
System.out.println("day of week:"+ca.get(Calendar.DAY_OF_WEEK));
//获取对应的Date对象
Date date=ca.getTime();
System.out.println("date time:"+date.getTime());
System.out.println("calendar time:"+ca.getTimeInMillis());//获取毫秒的时间
}
}
Math算术运算类
-
Math类中的静态方法帮助我们完成了基本的数学运算,其定义为
public final class Math extends Object{
}
public static void main(String[] args) {
System.out.println("绝对值:"+Math.abs(23)+"\t"+Math.abs(-23)+"\t"+Math.abs(0));
System.out.println("向上取整,再转double:"+Math.ceil(23.0001)+"\t"+Math.ceil(-9.9999));
System.out.println("向下取整再转double"+Math.floor(23.9999)+"\t"+Math.floor(-23.0001));
System.out.println("最值:"+Math.max(20, 10)+"\t"+Math.min(2, 40));
System.out.println("5的2次方:"+Math.pow(5, 2)+"5的立方"+Math.pow(5, 3));
System.out.println("随机数[0,1):"+Math.random());
int ran=(int)(Math.random()*9000)+1000;
System.out.println("1000-9999之间的随机数:"+ran);
System.out.println("四舍五入:"+Math.round(34.566)+"\t"+Math.round(34.345));
System.out.println("开方:"+Math.sqrt(55));
}
File类
(1):文件和目录路径名的抽象表示形式。可以代表一个文件或目录
-
可以实现获取文件和目录属性等功能。
-
可以实现对文件和目录的创建、删除等功能。
(2):常用的方法有:
-
createNewFile() //有则不创建 返回false,没有则创建 返回true
-
delete() //删除成功 返回true ,失败返回false
-
getAbsoluteFile() //返回绝对路径
-
getPath() //返回相对路径
-
getName() //返回文件名
-
isFile() //是否为文件 boolean
-
length //长度
-
mkdir() //创建单层目录
-
mkdirs() //创建多级目录
-
delete() //只能删除空目录
-
list() //返回一个字符串数组,命名由此抽象路径名表示的目录中的文件和目录。
-
listFiles() //返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件和目录。