java学习系列4--常用类

1. 基本数据类型的包装类

//类型:Byte,Short,Integer,Character,Boolean,Long,Float,Double

//自动装箱,本质上,编译器编译时为我们添加了:Integer i = Integer.valueOf(100);
Integer i = 100;
//自动拆箱:本质上,编译器编译时为我们添加了:int a = new Integer(100).intValue();
int a = new Integer(100);

2. 字符串相关类

String str = "abca";
String str2 = "def";
//String类常用方法
char cha = str.charAt(0);//取第0个字符
boolean b1 = str.equals(str2);//比较内容是否相等
boolean b2 = str.equalsIgnoreCase(str2); //忽略大小写,比较是否内容相等
int i1 = str.indexOf("a"); //取str中字符串a的位置
int i2 = str.lastIndexOf("a"); //取str中字符串a最后一个出现的位置,没有返回-1
int len = str.length(); //返回字符串长度
String str3 = str.replace("a","ddd");//返回替换后的字符串
boolean b3 = str.startsWith("a");//返回是否字符串以a开头
boolean b4 = str.endsWith("a"); //返回字符串是否以a结尾
String str4 = str.substring(1); //截取字符串下标为1,到末尾
String str5 = str.substring(1,3); //截取字符串下标为1,2
str.toLowerCase();
str.toUpperCase(); //将字符串中的所有大小写进行转换
str.trim();//去掉字符串头部和尾部的空格

//String,StringBuilder,StringBuffer比较
String: 不可变字符序列
StringBuilder:可变字符串序列、效率高、线程不安全
StringBuffer: 可变字符串序列、效率低、线程安全

3. 时间处理相关类

//Date时间类:标准纪元1970.1.1 0点开始到某个时刻的毫秒数,类型是long。
Date  d = new Date(2000);

//DateFormat和SimpleDateFormat:完成字符串和时间对象的转化。

//把时间对象按照“格式字符串指定的格式”转成相应的字符串
DateFormat  df  = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
String str  =  df.format(new Date(4001100));

//把字符串按照“格式字符串指定的格式”转成相应的时间对象
DateFormat  df2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");

Date  date = df2.parse("1983年5月10日 10时45分59秒");

//测试其他的格式字符。比如:利用D,获得本时间对象是所处年份的第几天。
DateFormat  df3  = new SimpleDateFormat("D");
String str3  =  df3.format(new Date());

//Calendar日历类,GregorianCalendar公历类
//获得日期的相关元素
Calendar calendar = new GregorianCalendar(2999,10,9,22,10,50);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int  day = calendar.get(Calendar.DATE);        //也可以使用:DAY_OF_MONTH。
int weekday = calendar.get(Calendar.DAY_OF_WEEK);    //星期几。 1-7.  1:星期日,2星期一,。。。7是星期六。
//设置日期的相关元素
Calendar c2 = new GregorianCalendar();
c2.set(Calendar.YEAR, 8012);
//日期的计算
Calendar c3 = new GregorianCalendar();
c3.add(Calendar.YEAR, -100);
//日期对象和时间对象的转化
Date  d4 =  c3.getTime();
Calendar c4 = new GregorianCalendar();
c4.setTime(new Date());

4. 枚举类

只能够取特定值中的一个;

使用enum关键字;

所有的枚举类型隐形的继承自java.lang.Enum,每个枚举的成员默认都是public static final,可以直接通过枚举类型名直接使用他们;

强烈建议当你需要定义一组常量时,使用枚举类型;

尽量不要使用枚举的高级特性,事实上高级特性都可以使用普通类来实现,没有必要引入复杂性。

5. Math类和Random类

//取整相关操作
System.out.println(Math.ceil(3.2));
System.out.println(Math.floor(3.2));
System.out.println(Math.round(3.2));
System.out.println(Math.round(3.8));
//绝对值、开方、a的b次幂等操作
System.out.println(Math.abs(-45));
System.out.println(Math.sqrt(64));
System.out.println(Math.pow(5, 2));
System.out.println(Math.pow(2, 5));
//Math类中常用的常量
System.out.println(Math.PI);
System.out.println(Math.E);
//随机数
System.out.println(Math.random());// [0,1)

random() 生成[0,1)之间的随机浮点数
• 生成:0-10之间的任意整数:
  int a = (int)(10*Math.random());
• 生成:20-30之间的任意整数:
 int b = 20 + (int)(10*Math.random());

6. File类

文件和目录路径名的抽象表示形式。一个File对象可以代表一个文件或目录;

可以实现获取文件和目录属性等功能;

可以实现对文件和目录的创建、删除等功能;

 File不能访问文件内容;

File file = new File("d:\\test\\java.txt");
File file = new File("d:/test/java.txt");
File file = new File("java.txt");
路径可以是绝对路径和相对路径,分隔符采用\\或者/

通过File对象可以访问文件的属性;

public String getName() public String getPath()
public boolean isFile() public boolean isDirectory()
public boolean canRead() public boolean canWrite()
public boolean exists() public long length()
public boolean isHidden() public long lastModified()
public File [] listFiles();

通过File对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)

public boolean createNewFile()throws IOException
public boolean delete()
public boolean mkdir(), mkdirs() 注意两个的区别!!

猜你喜欢

转载自www.cnblogs.com/Emking/p/12149124.html
4--
今日推荐