Java基础知识回顾-实用类

Java.lang包下:

1.枚举类

枚举类可以用于Switch语句,可以用于限制特殊域,防止对象脱离实际值

使用步骤:

创建枚举类

public enum Season{Spring,Summer,Automn,Winter}

创建方法调用:

public class SeasonDemo {
 public void operate(Season season)
 {
  switch(season){
  case Spring:
   System.out.println("春暖花开!");
   break;
  case Summer:
   System.out.println("天气炎热!");
   break;
  case Automn:
   System.out.println("丰收的季节!");
   break;
  case Winter:
   System.out.println("北京的冬天!");
   break;
  }
 }
}

---

创建测试方法:

public class tst {
 public static void main(String[] args) {
  SeasonDemo sd=new SeasonDemo();
  sd.operate(Season.Automn);
  }
 }

 

2.包装类

常用方法:

使用字符串类型作为参数构建基本类型,字符类型除外,因为字符类型不能接收字符串,参数值必须符合要转换的值的标准

int num=new Integer("21");

静态重载的ValueOf方法:可以接收基本类型数据和字符串作为参数并返回包装类的对象,参数值必须符合要转换的值的标准

int num=Integer.valueOf("3");

int num=Integer.valueOf('3');

类型转换

1.包装类-->基本类型

public type typeValue()

Integer integerID=new Integer(25);//包装类

int intID=integerID.intValue();//基本类型

2.字符串-->基本类型//参数类型必须为基本类型 的表现方式

public static type parseType(String type)

int num=Integer.parseInt("36");//字符串"36"转换为int

3.基本类型-->字符串

public static String toString (type value);

String sex=Character.toString('男');

通常情况下转换为字符串格式使用

String sex='男'+"";

 

3.Math类

常用方法:

max方法:返回参数中最大的参数

public static type max(type a,type b)

int num=Math.max(20,25);

求幂运算:返回a的b次幂的值

public static double pow(double a,double b)

double num=Math.pow(3,2);

求随机数:返回0到1之间的随机数

public static double random()

int random=(int)(Math.random()*10+1)//返回0-10的随机整数

 

4.String 类

多次修改字符串时候不建议使用String 类

常用方法:

public boolean equals()

public String trim()

public boolean equalsIgnoreCase(String anotherString)

public String substring(int beginindex,int endindex)//包含起始在末尾之前的元素,下标从0开始

public int indexof(String str)//获取参数值所对应的下标,如果找不到参数中的字符串的下标则返回-1,可以用于判断

 

5.StringBuffer类

public StringBuffer append(Type value)//追加

public StringBuffer reverse();//反转 "万千一" 转后" 一千万"

public String toString()

public StringBuffer(int startindex,int endindex,String str);

 

6.时间类

Date:

Date date=new Date()//获取当前时间

Calendar:是抽象类,将时间转换为单独的 年月日时分秒

Calendar calendar = calendar.getInstance();//获取日历对象

int year=calendar.get(Calendar.YEAR);

int Month=calendar.get(Calendar.Month)+1;

int day=calendar.get(Calendar.DAY);

int hour=calendar.get(Calendar.HOUR);

int minute=calendar.get(Calendar.MINUTE);

int second=calendar.get(Calendar.SECOND);

calendar.set(Calendar.MONTH,8);//设置月份为9月

calendar.add(Calendar.DAY_OF_MONTH,10);//日期加10天

GrogorianCalendar类:Calendar的子类,判断闰年

GrogorianCalendar grogorianCalendar=new GrogorianCalendar();

boolean flag=grogorianCalendar.isLeapYear(grogorianCalendar.get(grogorianCalendar.YEAR));//今年

常用方法:

calendar.set(Calendar.MONTH,8);//设置当前月份

calendar.add(Calendar.DAY_OF_MONTH,10);//当前日期加10天

通过Calendar.get(Calenadr.YEAR);获取年

DateFormat类:

提供了多种格式化和解释时间的方法

格式化:将时间转换为文本

解析:将文本转换成时间

常用类:SimpleDateFormat类

构造方法:SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

常用方法:

public final String format(Date date)

public Date parse (String source)

例子:

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

//获取当前时间

Date date=new Date();

//时间格式化为指定的模式的字符串

String datesdf=sdf.format(date);

System.out.println(datesdf);

//字符串格式化为时间对象格式

String newStr="2011-01-01 14:06:05"

Date newdate=sdf.parse(newStr)

 

Random类:伪随机数

构造方法:

Random random=new Random();//使用当前时间为种子值(初始值)

Random random=new Random(Long seed);//通过LONG类型的数字作为种子

获取伪随机数:

public int nextInt();//返回下一个伪随机数,返回类型为整型

public int nextInt(int n)//伪随机数取值在0到指定的数值N之间

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2013-03-19 22:24  鱼翅盖饭  阅读(156)  评论(0编辑  收藏  举报