一、基本数据类型的包装类
包装类是将基本数据类型封装成一个类,包含属性和方法
使用:
在使用过程中,会涉及到自动装箱和自动拆箱
装箱:将基本数据类型转换成包装类型
拆箱:将包住类型转换成基本数据类型
示例
package com.msbline.basic.common;
public class IntegerDemo {
public static void main(String[] args) {
int a = 10;
Integer i = new Integer(10);
//通过方法进行类型的转换
Integer i2 = Integer.valueOf(a);
int i3 = i2.intValue();
System.out.println(a == i);
Integer b1 = 100;
Integer b2 = 100;
Integer b3 = 200;
Integer b4 = 200;
//这两个结果的不一致,是因为在自动拆箱的时候,
//如果value不在规定范围内(-128到127)的时候,会 new Interger, //导致比较的时候两个地址不一致
//Interger 中自动拆箱的方法:
//public static Integer valueOf(int i) {
// if (i >= IntegerCache.low && i <= IntegerCache.high) // return IntegerCache.cache[i + (-IntegerCache.low)]; // return new Integer(i); // } System.out.println(b1 == b2);//true
System.out.println(b3 == b4);//false
}
}
二、字符串相关类
1、不可变字符序列:String
注意:
1、常量池在jdk1.7之后放在了对空间中
2、字符串的本质
--字符串的本质是字符数组或者叫字符序列
--String类使用final修饰,不可以被继承
--使用equals方法比较的是字符数组的每个位置的值
示例
package com.msbline.basic.common;
public class StringDemo {
public static void main(String[] args) {
String str = "abc";
String str1 = new String("abc");
System.out.println(str == str1);
System.out.println(str.equals(str1));
str1 = str1.intern();
System.out.println(str == str1);
System.out.println(str1.charAt(0));
System.out.println(str1.concat("def"));
System.out.println("-------------");
String a = "abc";
String b = "def";
String c = "abcdef";
String d = a+b;
// String d = (a+b).intern();
String e = "abc"+"def";
System.out.println(c == d);//false
System.out.println(c == e);//true
System.out.println("-------------");
String f = "a"+"b"+"c";//一个对象
//如下则是4个对象
String a1 = "a";
String a2 = "b";
String a3 = "c";
String f1 = a1+a2+a3;
}
}
2、可变字符序列:StringBuffer、StringBuilder
StringBuffer 线程安全 效率低
StringBuilder 线程不安全 效率高
示例
package com.msbline.basic.common;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(1)
.append(1.234)
.append("abcaaaaaaaaaaaaa")
.append(true);
System.out.println(stringBuffer);
System.out.println(stringBuffer.charAt(1));
System.out.println(stringBuffer.length());
System.out.println(stringBuffer.capacity());
System.out.println("-----------------------------");
//StringBuilder与StringBuffer使用大致一样
StringBuilder stringBuilder = new StringBuilder();
}
}
小结
三、时间处理相关类
1、Date、DateFormat、SimpleDateFormat、Calender
示例
package com.msbline.basic.common;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) throws ParseException {
Date date = new Date();
System.out.println(date);
//过时的方法
System.out.println(date.getYear());
//返回当前日期的毫秒值
System.out.println(date.getTime());
//yyyy-MM-dd HH:mm:ss
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//将date类按照规范转换为字符串格式
String str = dateFormat.format(date);
System.out.println(str);
//将字符串转换成对应的日期类
Date date1 = dateFormat.parse("2010-10-10 20:20:20");
System.out.println(date1);
System.out.println("-----------------------------------------");
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
//设置指定时间的日历类
calendar.setTime(date1);
System.out.println(calendar);
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(calendar.get(Calendar.MINUTE));
System.out.println(calendar.get(Calendar.SECOND));
}
}
四、Math类
示例
package com.msbline.basic.common;
public class MathDemo {
public static void main(String[] args) {
System.out.println(Math.abs(-1));
System.out.println(Math.sqrt(2));
//向上取整
System.out.println(Math.ceil(3.14));
//向下取整
System.out.println(Math.floor(3.14));
//2的3次方
System.out.println(Math.pow(2,3));
}
}
五、枚举类 jdk1.5
示例
package com.msbline.basic.common;
public enum Event {
LAUNCH("launch"),PAGEVIEW("pageview"),EVENT("event");
private String name;
Event(String name){
this.name = name;
}
public void show(){
System.out.println(this.name);
Event[] ee = values();
for(int i = 0; i < ee.length; i++){
System.out.println(ee[i].name);
}
}
}
package com.msbline.basic.common;
public enum Gender {
男,女
}
package com.msbline.basic.common;
public class Test {
public static void main(String[] args) {
Gender gender = Gender.女;
Gender gender1 = Gender.男;
Event event = Event.LAUNCH;
System.out.println(event.name());
event.show();
}
}