JavaEE基础篇————常用类
String
**字符串内容-----不可以变
- java.lang包下,所有的包不需要导入
- String字符串存储原理
String s =“s”;
Sting s = new String(“ss”); - 值传递与引用传递
- 几种构造方法
- 常用的一些方法
StringBuffer与StringBuilde
**字符串内容-----可以变
- StringBuffer 可以在字符串后面追加,改变字符串
1 追加 apeend(Object)
2 插入 insert(位置,内容)
3 删除 delete(位置,内容)
2.两者的区别,StringBuffer是线程安全的
StringBuilde非线程安全
8种基本类型的包装类
3.1 基本数据类型: 无法.出属性,方法
3.2 对象类型
– int i=1;---->对象类型
– 获得整形int的最大值
byte--------Byte
short-------Short
int---------Integer
long--------Long
float-------Float
double------Double
boolean-----Boolean
char--------Character
3.3常用方法
/* 1,八种包箱类,六个父类时Number,两个是Object
2,其中Number中有这样些方法
3,意义方便toString()方法
byteValue();
shortValue();
intValue();
long.....
4,获取常量最大,最小值
MAX_VALUE;
MIN_VALUE;
5.有了自动拆箱后,Number里的方法就用不到了
基本数据类型----(自动转换)—----包装类型 自动装箱
包装类型----(自动转换)—----基本数据类型 自动拆箱
只有加减乘除会触发自动装拆箱机制
6,"整数型常量池" Integer[0~127],在该范围内不会创建对象,直接指向整数型常量池
7,ValueOf() int|String----->Integer
8总结,int--Sting--Integer之间转换
int————>string:数字+"",String.valueOf();
string————>int:Integer.parseInt("123")
int————>Integer:自动 装箱 Integer.valueOf(100);
Integer————>int:自动 拆箱 intValue();
Integer————>string:String.valueOf(Integer对象)
string————>Integer:Integer.valueOf("123")
//int,Integer,String转化
//String————>int
String s = "100";
int i1= Integer.parseInt(s);
System.out.println(i1+1); //101
//int ————> String
String s2 = String.valueOf(i1);
String s3 = i1+"";
System.out.println(s2+1);//1001
System.out.println(s3+1);//1001
//int————>Integer
int x =1000;
//Integer————>int
Integer y =x;
//String————>Integer
Integer.valueOf("123");
//Integer————>String
String.valueOf(y);
时间类----Date,Calender
1.Date
1.1 可以精确到毫秒
1.2 java.sql.Date / java.util.Date
1.3 构造器
//无参数构造器:Date date2=new Date()
//有参数构造器:Date date2=new Date(时间毫秒数);
//1970.1.1 00:00:00 周四
1.4 getTime() 获得从1970.1.1 00:00:00 到当前的时间毫秒数
1.5 setTime(long time) 设置时间对象的时间毫秒数
1.6 /**
* 年:y
* 月:M
* 日:d
* 时:HH
* 分:mm
* 秒:ss
*/
时间格式化——SimpleDataFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowdate = sdf.format(date);
字符串转时间:parse()
时间转字符串:format()
2,Calender
//格林威治时间
Calendar cal = Calendar.getInstance();
System.out.println(cal);
//1,获取当前时间毫秒数
System.out.println("时间毫秒数:"+cal.getTimeInMillis());
System.out.println("时间对象:"+cal.getTime());
//2.年
System.out.println("获得年:"+cal.get(Calendar.YEAR));
//3.月
System.out.println("获得月:"+cal.get(Calendar.MONTH));
//4.日
System.out.println("获得天:"+cal.get(Calendar.DAY_OF_MONTH));
System.out.println("获得天:"+cal.get(Calendar.DATE));
//5.周几:
// 周天(1) 周一(2) 周二(3)...
System.out.println("获得周几:"+cal.get(Calendar.DAY_OF_WEEK));
System.out.println("本周是本月的第几周:"+cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("今天是今年的第几天:"+cal.get(Calendar.DAY_OF_YEAR));
//设置:设置具体的日期
cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.MONTH, 8);//8+1月份
cal.set(Calendar.DAY_OF_WEEK, 3);//3-1=2
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(cal.getTime()));
//设置2:设置修改的日期
//回到前一年
cal.add(Calendar.YEAR, -1);
System.out.println(sdf.format(cal.getTime()));
4,数字类
4.1数字格式化
//数字格式,#任意数字,“,”千分位,“.“小数位
DecimalFormat df =new DecimalFormat("##.#");
String s = df.format(12.2321);
4.2随机数-----Random
Random r= new Random();
int num =2;
r.nextInt();//产生int范围内的数据
r.nextInt(num);//产生0~num-1范围内的数
5枚举类
5.1定义:①一枚一枚可以列举出来,才建议使用枚举类型
②枚举也是一种引用类型,枚举的每一个值都是常量
5.2语法
enum 枚举类型名{
枚举值1,枚举值2,…
}