java lang和其他包_java-lang和commons-lang包

java-lang

java.lang包包含着Java最基础和核心的类,在编译时会自动导入。它提供了java中基础类。常用的有Object类、String类、基本类型的包装类、Math计算类等等。

包的位置:%JAVA_HOME%/jre/lib/rt.jar

包装类:

每个基本类型在java.lang包中都有一个相应的包装类

b324b6ff96d0

基本类型对应包装类.jpg

b324b6ff96d0

包装类.jpg

包装类的作用:

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

包装类的构造方法

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

如:Integer i = new Integer(1);

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

如:Integer i = new Integer("123");

注意事项:

1.Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false。(内部比较时用的是equalsIgnoreCase)

Boolean boolean1 = new Boolean("TRUE");

2.当Number包装类构造方法参数为String类型时,字符串不能为null,且该字符串必须可以解析为相应的基本数据类型的数据,否则编译通过,运行时NumberFormatException(数字格式)异常

Integer integer = new Integer("null");

自动拆装箱:

也解决了集合中不能放基本类型的问题

什么时候自动装箱:

Integer i =100;

相当于编译器自动为您作以下的语法编译:Integer i = Integer.valueOf(100);

什么时候自动拆箱:

自动拆箱(unboxing),也就是将对象中的基本数据从对象中自动取出

Integer i = 10; //装箱

int t = i; //拆箱,实际上执行了 int t = i.intValue();

Integer的自动装箱

//在-128~127 之外的数

Integer i1 = 200;

Integer i2 = 200;

System.out.println("i1==i2: " + (i1 == i2));

// 在-128~127 之内的数

Integer i3 = 100;

Integer i4 = 100;

System.out.println("i3==i4: " + (i3 == i4));

输出的结果是:

i1==i2: false

i3==i4: true

equals() 比较的是两个对象的值(内容)是否相同。

"==" 比较的是两个对象的引用(内存地址)是否相同,

int的自动装箱,是系统执行了Integer.valueOf(int i),源码:

public static Integer valueOf(int i) {

if(i >= -128 && i <= IntegerCache.high) //默认是127

return IntegerCache.cache[i + 128];

else

return new Integer(i);

}

对于–128到127(默认是127)之间的值,Integer.valueOf(int i) 返回的是缓存的Integer对象(并不是新建对象)

所以范例中,i3 与 i4实际上是指向同一个对象。

而其他值,执行Integer.valueOf(int i) 返回的是一个新建的 Integer对象,所以范例中,i1与i2 指向的是不同的对象。

Math类

floor: 取整,返回小于目标数的最大整数

ceil:取整,返回大于目标数的最小整数

round:四舍五入取整

System.out.println("Math.floor(1.2):" + Math.floor(1.2));//1.0

System.out.println("Math.ceil(1.2):" + Math.ceil(1.2));//2.0

System.out.println("Math.round(2.3):" + Math.round(2.3));//2

三角运算(正弦、余弦、正切等)

异常

一种严重的:Error,一种不太严重的:Exception

对于Error,一般不写针对性的代码进行处理

对于不太严重的,java对于Exception可以通过针对性的方式进行处理

特殊情况:try对应多个catch时,如果有父类的catch语句块,一定要放在下面。

java.lang.Throwable:

Throwable

| --- Error

| --- Exception

try、catch、finally使用:

try{}用来定义需要被检测的代码,catch(){}用来处理异常的代码,finally{}中是一定会执行的代码

注意:

finally中定义的通常是 关闭资源代码。因为资源必须释放。当执行到System.exit(0)时,finally不会执行。

JDK1.7异常处理新特性:

多个catch用一个catch替代

格式:

catch(异常1|异常2|异常3...变量名)

输出结果?

public class Testyc {

public static void main(String[] args) {

try {

int i = 1/0;

} catch (Exception e) {

System.out.println("catch");

return;

}finally{

System.out.println("finally");

}

}

}

catch

finally

catch里面有return语句,finally里面的语句还会执行,在return前执行。

线程

定义线程

1.继承java.lang.Thread类

此类重写Thread类的run()方法。

public class TestxcThread extends Thread {

public void run() {

for (int i = 0; i < 5; i++) {

System.out.println(getName()+":"+i);

}

}

}

public class TestThread {

public static void main(String[] args) {

TestxcThread xc1 = new TestxcThread();

TestxcThread xc2 = new TestxcThread();

xc1.setName("线程1");

xc2.setName("线程2");

xc1.start();

xc2.start();

}

}

开启线程:start(),这个方法其实做了两件事情,第一,让线程启动,第二,自动调用run()方法

2.实现java.lang.Runnable接口

重写run()方法

public class TestxcThread extends Thread {

public void run() {

for (int i = 0; i < 5; i++) {

System.out.println(getName()+":"+i);

}

}

}

public class TestRunnable {

public static void main(String[] args) {

TestxcRunnable test1 = new TestxcRunnable();

TestxcRunnable test2 = new TestxcRunnable();

Thread xc1 = new Thread(test1);

Thread xc2 = new Thread(test2);

xc1.setName("线程1");

xc2.setName("线程2");

xc1.start();

xc2.start();

}

}

//start->start0(调用 run)->target(new Tread 时会把target附上值,会执行test1.run();

public void run() {

if (target != null) {

target.run();

}

}

线程生命周期

b324b6ff96d0

生命周期.png

线程的生命周期,把图转化为文字就是:

线程通过new方法创建,调用start()方法,线程进入就绪状态,等待系统的调度。当系统调度,进入运行状态。正常结束或者异常退出,进程进入死亡状态。

处于运行状态的线程yieId()方法,线程转为就绪状态。

处于运行状态的线程遇到wait()方法,线程处于等待状态,需要notify()/notifyAll()来唤醒线程,唤醒后的线程处于锁定状态,获取了“同步锁”之后,线程才转为就绪状态。

wait()、notify()、notifyAll()

wait()、notify()、notifyAll()是三个定义在Object类里的方法,可以用来控制线程的状态。

这三个方法最终调用的都是jvm级的native方法。随着jvm运行平台的不同可能有些许差异。

如果对象调用了wait方法就会使持有该对象的线程把该对象的控制权交出去,然后处于等待状态。

如果对象调用了notify方法就会通知某个正在等待这个对象的控制权的线程可以继续运行。

如果对象调用了notifyAll方法就会通知所有等待这个对象控制权的线程继续运行。

其中wait方法有三个方法:

wait()

wait(long)

wait(long,int)

wait方法通过参数可以指定等待的时长。如果没有指定参数,默认一直等待直到被通知。

wait和sleep的区别

分析这两个方法:从执行权和锁上来分析:

wait:可以指定时间也可以不指定时间。不指定时间,只能由对应的notify或者notifyAll来唤醒。

sleep:必须指定时间,时间到自动从冻结状态转成运行状态(临时阻塞状态)。

wait:线程会释放执行权,而且线程会释放锁。

Sleep:线程会释放执行权,但不是不释放锁。

commons-lang

Apache Commons lang包提供了标准Java库函数里没有提供的Java核心类的操作方法。Apache Commons Lang为java.lang API提供了大量的辅助工具,尤其是在String操作方法。

StringUtils

如何判断字符串为空的情况?

String a=null;

if(a!=null && a!=""){}

需要判断的地方有很多,代码累赘

StringUtils : 该类主要提供对字符串的操作,对null是安全的,主要提供了字符串查找,替换,分割,去空白,去掉非法字符等等操作

// 1.public static boolean isEmpty(String str)

// 判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0

// 下面是示例:

System.out.println(StringUtils.isEmpty(null));// true

System.out.println(StringUtils.isEmpty(""));// true

System.out.println(StringUtils.isEmpty(" "));// false

System.out.println(StringUtils.isEmpty(" "));// false

System.out.println(StringUtils.isEmpty("bob"));// false

System.out.println(StringUtils.isEmpty(" bob "));// false

// 2.public static boolean isNotEmpty(String str)

//判断某字符串是否非空,等于!isEmpty(String str)

//下面是示例:

System.out.println(StringUtils.isNotEmpty(null));// false

System.out.println(StringUtils.isNotEmpty(""));// false

System.out.println(StringUtils.isNotEmpty(" "));// true

System.out.println(StringUtils.isNotEmpty(" "));// true

System.out.println(StringUtils.isNotEmpty("bob"));// true

System.out.println(StringUtils.isNotEmpty(" bob "));// true

//3. public static boolean isBlank(String str)

//判断某字符串是否为空或长度为0或由空白符(whitespace)构成

///下面是示例:

StringUtils.isBlank(null);// true

StringUtils.isBlank("");// true

StringUtils.isBlank(" ");// true

StringUtils.isBlank(" ");// true

System.out.println(StringUtils.isBlank("\t \n \f \r"));// true

System.out.println(StringUtils.isBlank("\b"));// false

System.out.println(StringUtils.isBlank(" bob "));// false

//4. public static boolean isNotBlank(String str)

//判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成,

//等于!isBlank(String str)

// 下面是示例:

System.out.println(StringUtils.isNotBlank(null)); // false

System.out.println(StringUtils.isNotBlank("")); // false

System.out.println(StringUtils.isNotBlank(" ")); // false

System.out.println(StringUtils.isNotBlank(" ")); //false

System.out.println(StringUtils.isNotBlank("\t \n \f \r")); // false

System.out.println(StringUtils.isNotBlank("\b")); // true

System.out.println(StringUtils.isNotBlank("bob")); // true

System.out.println(StringUtils.isNotBlank(" bob ")); // true

//5. public static String trim(String str)

//去掉字符串两端的控制符(control characters, char <= 32)

//如果输入为null则返回null

//下面是示例:

System.out.println(StringUtils.trim(null)); // null

System.out.println(StringUtils.trim("")); // ""

System.out.println(StringUtils.trim(" ")); // ""

System.out.println(StringUtils.trim(" \b \t \n \f \r ")); // ""

System.out.println(StringUtils.trim(" \n\tss \b")); // "ss"

System.out.println(StringUtils.trim(" d d dd ")); // "d d dd"

System.out.println(StringUtils.trim("dd ")); // "dd"

System.out.println(StringUtils.trim(" dd ")); // "dd"

//6.public static String trimToNull(String str)

//去掉字符串两端的控制符

//如果变为null或"",则返回null

//下面是示例:

StringUtils.trimToNull(null); // null

StringUtils.trimToNull(""); // null

StringUtils.trimToNull(" "); // null

StringUtils.trimToNull(" \b \t \n \f \r "); // null

StringUtils.trimToNull(" \n\tss \b"); // "ss"

StringUtils.trimToNull(" d d dd "); // "d d dd"

StringUtils.trimToNull("dd "); // "dd"

StringUtils.trimToNull(" dd "); // "dd"

System.out.println(StringUtils.contains("defg", "ef"));//检查一字符串是否包含另一字符串.

System.out.println(StringUtils.containsOnly("ef", "defg"));//检查一字符串是否为另一字符串的子集

System.out.println("去除字符中的空格.");

System.out.println(StringUtils.deleteWhitespace("aa bb cc"));//aabbcc

System.out.println("分隔符处理成数组.");

String[] strArray = StringUtils.split("a,b,,c,d,null,e", ",");

System.out.println(strArray.length);//6

System.out.println("缩短到某长度,用...结尾.");

System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 10));//The qui...

System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 15, 10));//... fox...

ArrayUtils

ArrayUtils 提供了数组的复制,查找,获取子数组,反转等功

//判断数组是否为空(null和length=0的时候都为空)

ArrayUtils.isEmpty(new int[0]);// true

ArrayUtils.isEmpty(new Object[] { null });// false

//合并两个数组

ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 });// {1,3,5,2,4}

//删除数组中某个位置上的数据

ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}

// 删除数组中某个对象(从正序开始搜索,删除第一个)

ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}

//Null处理,如果输入的两个数组都为null时候则返回true

ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false

ArrayUtils.isEquals(null, null);// true

//查询某个Object是否在数组中

ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true

//输出数组中的元素内容

ArrayUtils.toString(new int[] { 1, 4, 2, 3 });// {1,4,2,3}

ArrayUtils.toString(new Integer[] { 1, 4, 2, 3 });// {1,4,2,3}

ArrayUtils.toString(null, "I'm nothing!");// I'm nothing!

DateUtils和DateFormatUtils

DateUtils 主要提供了对日期的操作,包括日期加减,日期格式化,日期比较等。它们在org.apache.commons.lang.time包下。

1.与SUN的SimpleDateFormat相比 ,其主要优点是:线程安全。

2.对应于SimpleDateFormat的format()的方法,是DateFormatUtils 的format系列方法,常用的就是:

public static String format(Date date, String pattern)

3.对应与SimpleDateFormat的parse()的方法,是DateUtils的parseDate方法,即:

public static Date parseDate(String dateValue) throws DateParseException

4.日期舍入与截整,DateUtils的truncate()方法可以将日期按照任意范围截整,关键看第二个参数。

public static Date truncate(Date date, int field)

第二个参数取自Calendar的常量,可以是MONTH、DATE、HOUR等多种;

5.判断是否是同一天,DateUtils的isSameDay()方法

public static boolean isSameDay(Date date1, Date date2)

6.DateFormatUtils定义了很多内置的固定日期格式,均为FastDateFormat类型,比如 ISO_DATE_FORMAT。使用 FastDateFormat的format()方法可以直接将日期格式化为内置的固定格式。

public String format(Date date)

//常用日期格式的格式化操作:

//以 yyyy-MM-dd 格式化:

System.out.println(DateFormatUtils.ISO_DATE_FORMAT.format(new Date()));//2017-06-17

//以 yyyy-MM-dd'T'HH:mm:ss 格式化:

System.out.println(DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date()));//2017-06-17T12:51:18

//以 HH:mm:ss 格式化:

System.out.println(DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(new Date()));//12:51:18

//自定义日期格式的格式化操作:

// 以 yyyy-MM-dd HH:mm:ss 格式化Date对象:

System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));//2017-06-17 12:51:18

//以 yyyy-MM-dd HH:mm:ss 格式化Calendar对象:

System.out.println(DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));//2017-06-17 12:51:18

//以 yyyy-MM-dd HH:mm:ss 格式化TimeInMillis:

System.out.println(DateFormatUtils.format(Calendar.getInstance().getTimeInMillis(), "yyyy-MM-dd HH:mm:ss"));//2017-06-17 12:51:18

BooleanUtils

BooleanUtils用来操作基础布尔或者布尔对象,很多方法在工作中可以经常使用,下面是该工具类的一个蓝图:

b324b6ff96d0

image.png

Boolean[] arr = new Boolean[]{true,true};

//一假即假

BooleanUtils.and(arr); // true

System.out.println(BooleanUtils.isTrue(Boolean.TRUE));//true

System.out.println(BooleanUtils.isTrue(Boolean.FALSE));//false

System.out.println(BooleanUtils.isTrue(null));//false

System.out.println(BooleanUtils.isNotTrue(Boolean.TRUE));//false

System.out.println(BooleanUtils.isNotTrue(Boolean.FALSE));//false

System.out.println(BooleanUtils.isNotTrue(null));//true

Apache Commons只是工具类库,并不能解决所有的问题,但是可以提高代码的效率。Commons的工具类还有很多,还是需要大家在工作中不断慢慢的积累。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值