您的位置:首页 > 编程语言 > Java开发

Java学习笔记之常用API学习笔记3

2015-05-04 16:46 721 查看
Java学习笔记之常用API学习笔记3
一、基本数据类型的包装类

基本数据类型的包装类与基本数据类型的对应关系



1、Integer类

1.1、简介:
Integer 类在对象中包装了一个基本类型 int 的值,该类提供了多个方法,能在 int 类型和String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法
1.2、常用构造方法

Integer(int value) //把int基本类型的数据封装成Integer对象

Integer(String s) //把String类型的数字字符串(必须是数字)封装成Integer对象

1.3、常用字段

public static final int MAX_VALUE //最大值

public static final int MIN_VALUE //最小值

1.3、常用方法

publicstatic
StringtoBinaryString(int i)
//把十进制数转换成二进制数返回String类型

publicstatic
StringtoHexString(int i)
//把十进制数转换成十六进制数,返回String类型

publicstatic
StringtoOctalString(int i)
//把十进制数转换成八进制数,返回String类型

实例代码:

publicclass IntegerDemo {
public staticvoid main(String[]args) {
//把基本类型数据包装成Integer对象
Integerin =
new
Integer( 12345 );
System.out.println(in.toString());
//把String类型的数字字符串包装成Integer对象
Integerin2 =
new
Integer("1234567890");
//Integer in2 = new Integer("hello");
//错误,类型不匹配,必须是数字字符串
System.out.println(in2);
System.out.println(in2.toString());
//Integer类重写了Object类的toString方法
//10十进制转2进制
Strings1 =Integer.toBinaryString(12);//1100
System.out.println(s1);
//10十进制转16进制
Strings2 =Integer.toHexString(16);
System.out.println(s2);
//10十进制转8进制
Strings3 =Integer.toOctalString(8);
System.out.println(s3);
System.out.println( Integer.MAX_VALUE );//最大值
System.out.println( Integer.MIN_VALUE );//最小值
}
}

结果是:

12345
1234567890
1234567890
1100
10
10
2147483647
-2147483648

1.4、String 转换到 int的方式:

1、通过Integer类的Integer(String s)构造方法

2、通过Integer类的intValue()方法

3、通过Integer类的parseInt()方法

4、通过Integer类的valueOf()方法

实例代码:

public
class
IntegerDemo3 {
public
static void
main(String[]
args) {

//String --> Integer --> int
Integerin2 =new Integer("123456");
//把数字字符串包装成Integer对象
intnum =in2.intValue();
//把Integer对象转换成int类型
System.out.println(num);
System.out.println(Integer.parseInt("123"));
//把数字字符串解析成String类型
System.out.println(Integer.valueOf("1234").intValue());
//先把数字字符串装换成Integer对象,在把Integer对象转成int基本数据类型
}
}

结果是:

123456
123
1234

1.5、int 转换到 String的方式:

1、通过连接符的方式,例如:10 + “cats” = “10cats”

2、通过Integer类的方法Integer(int value)构造方法

3、通过Integer类的toSting()方法

4、通过String类的valueOf()方法(该方法可以把各种类型转换成String类型)

示例代码:

public
class
IntegerDemo3 {
public
static void
main(String[]
args) {
//方式1
inti = 10;
System.out.println(i+"cats");
//方式2构造方法
//int --> Integer --> String
Integerin =
new
Integer(20);
System.out.println(in.toString() );
//方式3
System.out.println(Integer.toString(12345));
//方式4
System.out.println(String.valueOf(67890));

}
}

结果是:

10cats

20
12345
67890

1.6、JDK1.5新特新

自动装箱:把基本数据类型封装成对象,例如:int → Integer

自动拆箱:把对象还原成基本数据类型,例如:Integer → int

实例代码:

public
class
IntegerDemo4 {
public
static void
main(String[]
args) {
//jdk1.5之前
Integer in =new Integer(123);//把int型封装成Integer对象
intnum =in.intValue();//把Integer对象还原成int型数据
System.out.println(num); //打印还原后的数据
//实现2个数相加
intsum =num + 10;
System.out.println(sum); //133
System.out.println("-----------jdk1.5之后---------");
Integer in2 = 123; //直接把int类型数据赋值给Integer类的对象,
//自动装箱
//相当于:Integer in2 = Integer.valueOf(123);
intsum2 =in2 + 10;//相当于int sum2= in2.intValue()
+ 10;
//把Integer类对象in2自动拆箱为基本数据类型
System.out.println(sum2);//打印基本数据类型sum2
Integerin3 = 123;//自动装箱
//相当于Integer in3 = Integer.valueOf(123);
in3 =
in3 + 10; //自动拆箱
/*
相当于:
in3= in3.intValue() + 10;//先把Integer对象自动拆箱为基本类型
in3= 123 + 10; //然后把基本类型的数据参与运算
in3= 133;//把计算后的结果自动装箱in3 = Integer.valueOf(133)
*/
System.out.println(in3);//打印对象Integer类的对象in3
}
}

练习题,Byte常量池问题:

public
class
IntegerTest {
public
static void
main(String[]
args) {
Integeri1 =
new
Integer(127);
Integeri2 =
new
Integer(127);
System.out.println(i1 ==i2);//
两个对象的地址:false
System.out.println(i1.equals(i2));//两个对象的内容:true
//注意:Integer重写了Object类的equals()方法
Integeri3 =
new
Integer(128);
Integeri4 =
new
Integer(128);
System.out.println(i3 ==i4);//false
System.out.println(i3.equals(i4));//true
Integeri5 = 128;// Integer.valueOf(128)
Integeri6 = 128;
System.out.println(i5 ==i6);//false
System.out.println(i5.equals(i6));//true
Integeri7 = 127;
//Integer.valueOf(127)
Integeri8 = 127;
System.out.println(i7 ==i8);//true:
System.out.println(i7.equals(i8));//true
}
}

Byte常量池问题
在java的常量池中,byte范围内(-128~127)的所有数据,都存储在常量池中,所以,当访问的数据在byte范围的时候,直接在常量池中获取,当数据范围超过byte范围时,本例中需要new出对象(new
Integer(128))。
2、Character类

概述

Character 类在对象中包装一个基本类型 char 的值

此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写.

方法:

public static boolean isDigit(charch)确定指定字符是否为数字。

public static booleanisLowerCase(char ch)确定指定字符是否为小写字母。

public static booleanisUpperCase(char ch)确定指定字符是否为大写字母

实例:键盘输入字符串,判断该字符串中有多个少大写、小写、数字的个数。

public
class
CharacterDemo {
public
static void
main(String[]
args) {
//键盘输入
Scannersc =
new
Scanner(System.in);
System.out.println("请输入字符串:");
Stringstr =
sc.nextLine();
//定义3个变量
intnumberCount = 0;
intmaxCount = 0;
intminCount = 0;
//遍历每一个字符
for (inti = 0;i <
str.length();i++) {
//获取到每一个字符
charch =str.charAt(i);
//Character c = ch;//自动装箱
//Character c = str.charAt(i);
//判断
if (Character.isDigit(ch)) {//是否为数字。
numberCount++; //是数字,则计数器+1
}elseif (Character.isLowerCase(ch)) {//否为小写字母
minCount++;
}elseif (Character.isUpperCase(ch)) {//否为大写字母
maxCount++;
}
}
//打印
System.out.println("numberCount:" +numberCount );
System.out.println("maxCount:" +maxCount );
System.out.println("minCount:" +minCount );
}
}

二、正则表达式

1、概述

Regular expression正则表达式:是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。

2、正则表达式的语法规则:
字符(一个字符)
x 字符 x , x为任意字符
如:a代表字符a,b代表字符b
\x 转义字符, x任意字符
\\ 反斜线字符, 代表就是一个 \
\t 制表符 Tab 键
\n 新行(换行)符
\r 回车符
字符类(多个字符)
[abc] 代表 a、b 或 c中的其中一个字符
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-z] 代表字符a 到 字符z 之间的任意字符
[a-zA-Z] 代表所有字母中的一个
[0-9] 代表0-9之间的一个数字
预定义字符类:
. 代表任何的一个字符
就是表示一个.怎么办? \.
\w 单词字符:[a-zA-Z_0-9]
\d 数字:[0-9]
边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界代表不是单词字符的字符,就是单词边界
hello java,android!Javaee
Greedy数量词
注意:这里的X 可以代表一个字符,也可以代表一组字符(字符串)
X? 代表X所代表的字符,出现一次或一次也没有
X* 代表X所代表的字符,出现零次或多次
X+ 代表X所代表的字符,出现一次或多次
X{n} 代表X所代表的字符,出现恰好 n次
X{n,} 代表X所代表的字符,出现至少 n次
X{n,m} 代表X所代表的字符,出现至少 n次,但是不超过 m 次

举例:校验qq号码,要求必须是5-15位数字,0不能开头
方式1:普通方法
public class RegexDemo{
public staticvoid main(String[] args) {
//键盘录入
Scanner sc = newScanner(System.in);
System.out.println("请输入5-15位QQ号码:");
String qq = sc.nextLine();//保存键盘输入
boolean flag = checkQQ(qq);//检查QQ号是否输入正确
system.out.println(flag);//打印测试结果
}
//验证qq号码
public staticboolean checkQQ(String qq) {
boolean flag = true;//记录qq号码是否正确
//判断长度是否在5~15位之间
if(qq.length() >= 5 && qq.length() <= 15) {
//判断是否以0开头
if (qq.startsWith("0")){
flag = false;
} else{ //不是0开头
for (int i = 0; i< qq.length(); i++) {
//获取到每一位字符
char ch = qq.charAt(i);
//判断当前字符 是否为数字
if (Character.isDigit(ch)){
//这位是数字,继续判断下一位字符
} else {
//这位不是数字,说明qq账号不正确
flag = false;
break;//终止for循环
}
}
}
} else {
//长度错误
flag = false;
}
return flag;
}
}
方式二:正则表达式
publicclass RegexDemo {
publicstaticvoid main(String[]args) {
//键盘录入
Scanner sc =new Scanner(System.in);
System.out.println("请输入5-15位QQ号码:
");
Stringqq =sc.nextLine();
//正则表达式规则
Stringregex ="[1-9][0-9]{4,14}";
//[1-9]:表示第一位只能是1-9之间的数字
//[0-9]:表示从第二位起,每一位上的数字可以是0-9之间任何一个数
//[4,14]:表示最少为4位数,最大为14位数
booleanflag =qq.matches(regex);//和规则比较
System.out.println(flag);
}
3、String类中正则表达式常用方法:
3.1、判断功能:publicboolean matches(String regex)告知此字符串是否
匹配给定的正则表达式。
测试实例1、判断是否是一个合法的手机号码
要求手机号码为:
13X XXXX XXXX
14X XXXX XXXX
15X XXXX XXXX
17X XXXX XXXX
18X XXXX XXXX
分析:
1:键盘输入手机号码
2:定义正则表达式字符串,用来定义判断的规则
3:使用正则表达式字符串验证手机号码
4:显示结果
public
class
RegexDemo {
public
static void
main(String[]
args) {
// 1:键盘输入手机号码
Scanner sc =new Scanner(System.in);
System.out.println("请输入手机号码:");
String number =sc.nextLine();
//2:定义正则表达式字符串,用来定义判断的规则
String regex ="1[34578][0-9]{9}";
//3:使用正则表达式字符串验证手机号码
booleanflag =number.matches(regex);
//显示结果
System.out.println(flag);
}
}
测试实例2、校验邮箱,例如:
2423424@qq.com
lcy@itcast.cn
18500535346@gmail.com
public
class
RegexTest {
public
static void
main(String[]
args) {
//键盘录入
Scannersc =new Scanner(System.in);
System.out.println("请输入邮箱地址");
Stringemail =sc.nextLine();
//指定规则
Stringregex ="\\w+@\\w+(\\.\\w+)+";
//验证
booleanflag =email.matches(regex);
System.out.println(flag);
}
}
3.2、匹配功能
public String[]split(String regex)根据给定正则表达式的匹配拆分此字符串,返回一个String类型的数组
例如把:aa,bb,cc拆成aa bbcc
实例代码:
public
class
RegexDemo {
public
static void
main(String[]
args) {
//定义规则
String regex = ",";
//定义需要分割的一个字符串
String s = "aa,bb,cc";
//根据给定正则表达式的匹配拆分此字符串。
String[]arr =s.split(regex);
//查看结果
for (inti = 0;i <
arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
}
输出结果是:aa bb cc
3.3、替换功能,将满足规则的字符串 用给定的字符串替换,
public String replaceAll(String regex,Stringreplacement);
实例代码:
public
class
RegexTest {
public
static void
main(String[]
args) {
//键盘输入
Scannersc =
new
Scanner(System.in);
System.out.println("请输入内容:");
Stringstr =
sc.nextLine();//获得键盘输入
//定义规则
//String regex ="[0-9]";//数字出现一回
Stringregex =
"[0-9]+";//数字出现多回
//使用规则替换 数字字符
Stringresult =
str.replaceAll(regex,
"*");//用*号替换数字
//输出结果
System.out.println(result);
}
}
输出结果:
请输入内容:
123sfjklasdfh2134214
*sfjklasdfh*
3.4、Matcher类中的获取功能:
public String group()返回由以前匹配操作所匹配的输入子序列。
group()相当于nextXxx()获取满足条件的数据
public boolean find()尝试查找与该模式匹配的输入序列的下一个子序列。
find()相当于hasNextXxx()是否有下一个元素
实例代码:
public class PatternTest {
public staticvoid main(String[]args) {
//定义规则 由三个字符组成的单词
Stringregex =
"\\b[a-z]{3}\\b";//注意:”\b”是单词边界
//定义字符串
Strings ="da jia ting wo shuo,jin tian yao xiayu,bu shang wanzi
xi,gao xing bu?";
//把正则表达式字符串,编译成对象
Patternp =Pattern.compile(regex);
//使用正则表达式对象与数据进行匹配,将结果封装到匹配器对象中
Matcherm =
p.matcher(s);
//判断是否有满足条件的数据
while (m.find()) {
//find()尝试查找与该模式匹配的输入序列的下一个子序列
//获取满足条件的数据
System.out.println(m.group());
}
}
}
结果是:
二、Math:

包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

字段:

publicstatic final double E比任何其他值都更接近 e(即自然对数的底数)的 double 值。

publicstatic final double PI比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。

方法:

publicstatic double abs(double a) 返回double 值的绝对值

publicstatic double cbrt(double a)返回double 值的立方根

publicstatic double ceil(double a)返回比double值大的最小整数

publicstatic double floor(double a)返回比double值小的最大整数

publicstatic double max(double a, double b)返回两个 double 值中较大的一个

publicstatic long round(double a) 四舍五入, 取整

publicstatic double sqrt(double a)返回正确舍入的 double 值的正平方根

publicstatic double random() : 返回0.0到1.0之间的随机小数,包含0.0,不包含1.0

三、Random:

随机数类 此类用于产生随机数

构造方法:

public Random()创建一个新的随机数生成器对象, 默认的种子是当前系统时间的毫秒值

public Random(long seed) 使用单个 long 种子创建一个新的随机数生成器。如果种子相同,每次得到随机数相同

方法:

public int nextInt() int范围内的随机数

public int nextInt(int n) 在0-n之间的随机数

四、System类:

Java不支持全局函数和变量,Java设计者将一些系统相关的重要函数和变量收集到了一个统一的类中,这就是System类。System类中的所有成员都是静态的,它不能被实例化。

成员方法

public static void gc() : 运行垃圾回收器

public static void exit(intstatus) : 退出正在运行的Java虚拟机

public static longcurrentTimeMillis(): 获得当前时间的毫秒值

(1970年1月1日 00:00:00 到当前时间之间的毫秒差值)

public static voidarraycopy(Object src,int srcPos,Object dest,int destPos,int length)

//数组复制

五、BigInteger类

可以让超过Integer范围内的数据进行运算

构造方法

public BigInteger(Stringval)

常用方法:

public BigIntegeradd(BigInteger val) 加法

public BigIntegersubtract(BigInteger val) 减法

public BigIntegermultiply(BigInteger val) 乘法

public BigIntegerdivide(BigInteger val) 除法

public BigInteger[]divideAndRemainder(BigInteger val) 取商 + 余数

示例代码:

public
class
BigIntegerDemo {
public
static void
main(String[]
args) {
BigIntegerbi1 =
new
BigInteger("100");
BigIntegerbi2 =
new
BigInteger("50");
System.out.println(bi1.add(bi2));//加法:150
System.out.println(bi1.subtract(bi2));//减法:50
System.out.println(bi1.multiply(bi2));//乘法:5000
System.out.println(bi1.divide(bi2));//除法:2
BigInteger[]b =
bi1.divideAndRemainder(bi2);//取余
System.out.println(b[0]);//2
System.out.println(b[1]);//0
}
}

六、BigDecimal类

不可变的、任意精度的有符号十进制数。

构造方法

public BigDecimal(Stringval)

常用方法:

public BigDecimaladd(BigDecimal augend) 加法

public BigDecimalsubtract(BigDecimal subtrahend) 减法

public BigDecimalmultiply(BigDecimal multiplicand) 乘法

public BigDecimaldivide(BigDecimal divisor) 除法

public BigDecimaldivide(BigDecimal divisor,int scale,int roundingMode)

//除法,保留指定位数的小数,指定进位方式

示例代码:

public
class
BigDecimalDemo {
public
static void
main(String[]
args) {
BigDecimalbd1 =
new
BigDecimal("0.09");
BigDecimalbd2 =
new
BigDecimal("0.01");
System.out.println(bd1.add(bd2));//0.10
BigDecimal bd3 =new BigDecimal("1.0");
BigDecimalbd4 =
new
BigDecimal("0.32");
System.out.println(bd3.subtract(bd4));//0.68
BigDecimalbd5 =
new
BigDecimal("1.015");
BigDecimalbd6 =
new
BigDecimal("100");
System.out.println(bd5.multiply(bd6));//101.5
BigDecimalbd7 =
new
BigDecimal("1.501");
BigDecimalbd8 =
new
BigDecimal("100");
System.out.println(bd7.divide(bd8));//0.01501
System.out.println(bd7.divide(bd8, 2,BigDecimal.ROUND_HALF_UP));////0.02
}
}

七、Date: 日期类

Date类用于表示日期和时间

构造方法:

public Date(): 获取当前系统时间

public Date(long time): 获取1970年1月1日0点 加上 给定的毫秒值后,所对应的时间

Date 与 long类型的相互转换

publicvoid setTime(long time) : 给当前的日期对象,设置时间

publiclong getTime() : 获取当前日期对象 距离1970年1月1日0点 的毫秒值

八、DateFormat类概述

DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat

SimpleDateFormat构造方法

publicSimpleDateFormat()

publicSimpleDateFormat(String pattern)

成员方法

public final Stringformat(Date date) : 将给定的日期对象 转换成 字符串对象

public Date parse(Stringsource) :将给定的字符串 转换为 日期对象

九、Calendar类概述

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

成员方法

public static Calendar getInstance() 通过日历类中的静态方法,返回日历对象

public int get(int field) : 获取日历对象中的成员

public void add(int field,int amount) 对日历对象中,指定的成员进行时间调整

public final void set(int year,int month,intdate)// 设置日历 年月日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: