JAVA包中的各种类汇总



1、java.lang包及该包中的类

java.lang包是java最基本的包,这个包中的所有类都由系统自动引入,所以不用import语句就可以访问该包中的类。

1.1Object类:

Object类是所有类的父类,包括自定义的类(无需用extends显式声明继承自Object)

Object类的方法:

(1)public boolean equals(Object obj)

Object类的equals方法比较的是两个对象的地址,相当于两个对象做如下运算obj1==obj2,即比较两个对象的地址是否相等。若要按自己的意愿比较两个对象,需要对该方法进行覆盖,同时覆盖了该方法的类最好也覆盖Object类的hashCode()方法,同时equals比较的两个对象返回为true时,hashCode()对这两个对象返回的hash值最好一样。

(2)public int hashCode()

返回一个哈希值,不同对象有不同的哈希值。

(3)public String toString()

返回一个反映当前对象信息的字符串。

有一个Employee类如下:

public class Employee{

       privateint id;

       privateString name;

       privateint age;

       publicEmployee(int id, String name, int age){

              this.id= id;

              this.name= name;

              this.age= age;

       }

       publicEmployee(){

       }

       publicString getName(){

              returnthis.name;

       }

       publicvoid setName(String name){

              this.name= name;

       }

       publicint getId(){

              returnthis.id;

       }

       publicvoid setId(int id){

              this.id= id;

       }

       publicint getAge(){

              returnthis.age;

       }

       publicvoid setAge(int age){

              this.age= age;

       }

       publicboolean equals(Object obj){

              if(this== obj){

                     returntrue;

              }

              if(this.getClass()== obj.getClass()){//getClass方法获得对象原来的对象类型

                     Employeeemp = (Employee)obj;

                     returnthis.getId() == emp.getId() && this.getAge() == emp.getAge()

                                   &&this.getName().equals(emp.getName());

              }

              returnfalse;

       }

       publicString toString(){

              returnthis.getId()  + " "+this.getName() + " " + this.getAge();

       }

public int hashCode(){

returnthis.getId()*5+this.getAge()*2+this.getName().length()+100;

}

}

test类调用如上的employee类:

public class test{

public static void main(String args[]){

Employee e1=new Employee(1,"张三",21);

Employee e2=new Employee(1,"张三",21);

System.out.println(e1.toString());//打印e1对象的一些信息

System.out.println(e1==e2);//打印结果为false

System.out.println(e1.equals(e2));//打印结果为true

}

}

1.2Class类:

Class类非常特殊,当一个类X被编译后,都会有一个特殊的Class对象产生,它隐藏在X.class文件中,Class对象由编译系统自动生成。

Class类的方法:

(1)public static Class forName(String className) throwsClassNotFoundException

这个方法是静态方法,可以用Class直接调用,如:Class.forName(“Gum”);方法的返回值是形参指示的类的Class对象。该方法的调用forName(“X”)会导致类X的初始化。

(2)public String getName()

该方法返回Class对象代表的实体名(实体可以是类、接口、数组、基本数据类型等)。例如,(newObject()).getClass().getName的值是java.lang.Object,其中getClass()获得当前对象的Class对象,同一个类的对象有相同的Class对象。

1.3Math类:

Math类是一个最终类,类头定义是:public final class Math extends Object,该类的方法都是静态方法,可以用类名直接调用。

1.4StringStringBuffer

String类用于处理那些不会发生改变的字符串,而StringBuffer类用于处理那些可能发生变化的字符串。String可以通过String s1=”hello”String s1=new String(“hello”)创建,而StringBuffer只能用构造函数创建,即:StringBuffer s1=newStringbuffer(“hello”)

String s1=”hello”String s1=new String(“hello”)的区别:

public class test{

public static void main(String args[]){

String s1="hello";

String s2="hello";//s1s2指向相同的地址

System.out.println(s1==s2);//打印的是true

s1+="ss";//s1作字符串的连接操作,s1将会重新生成

System.out.println(s1+"  "+s2);//s1s2不一样了

String s3=new String("hello");

String s4=new String("hello");//s3s4指向不同的地址

System.out.println(s3==s4);//打印的是false

}

}

String类的方法:

(1)public int length()

该方法返回字符串的长度。

(2)public char charAt(int index)

返回index位置的字符,index0length()-1

(3)public Boolean equals(Object obj)

Object类的方法的覆盖,可以用来比较两个String类中的字符串是否一样。

(4)public int indexOf(int ch)

返回字符串中第一次出现形参所指示的字符的位置,若没该字符,则返回-1。形参是字符的Unicode值。

(5)public boolean equals(Object anObject)

Object类的equals方法的重载,若当前String对象的字符串和形参anObject指向的String对象的字符串匹配时才返回为true

(6)public static String format(String format, Object args)

将形参format字符串格式化后返回格式化的字符串。例如:

public class ExceptionTest{

public static void main(String[] args)throws Exception{

String s1="aaa %s bbb";

String s2=s1.format(s1,"ccc");//s2”aaa ccc bbb”

System.out.println(s2);

}

}

(7)public String[] split(String regex)

通过String regex正则表达式的匹配,将调用该方法的字符串拆分成各个字符串。例如:

public class test{

public static void main(String[] args){

String s1="aaa:bbb:ccc";

String[] strarr=s1.split(":");

//s1字符串按分号(:)拆分成aaabbbccc,分别保存于strarr字符串数组中

for(int i=0;i<strarr.length;i++)

System.out.println(strarr[i]);//打印拆分的字符串

}

}

StringBuffer类的方法:

(1)public StringBuffer append(String str)

把形参字符串加到当前字符串之后,形成一个新的可变的字符串。例如:

public class test{

public static void main(String[] args){

StringBuffer sb=newStringBuffer("hello");

sb.append("World");

System.out.println(sb);//sb字符串变为helloWorld

}

}

(2)public StringBuffer insert(int offset, String str)

在当前字符串中插入形参字符串str,形成一个新的可变字符串,形参offset是偏移量,它指示在何处插入,0<=offset<=length()

(3)public String toString()

把当前可变字符串变成String类对象,事实上在println打印StringBuffer时,自动调用了该方法。

1.5System类:

System类是一个final类,它的方法都是静态方法,没有人可以实例化一个System类。

System类的属性:

public static final InputStream in//标准输入,这个属性是InputStream的一个对象。

public static final PrintStream out//标准输出

public static final PrintStream err//标准错误输出

System类的方法:

(1)public static Properties getProperties(argument);

返回系统环境信息。

(2)public static String setProperty(String key, String value)

设置系统变量的值,key为键名,value为键值。

(3)public static long currentTimeMillis()

返回系统时间于197011日午夜间的时间差,单位是毫秒。

import java.util.*;

public class test{

public static void main(String[] args){

Date date=newDate(System.currentTimeMillis());

System.out.println(date);//显示当前系统日期和时间

System.out.println(date.getTime());//输出的就是System.currentTimeMillis()

}

}

(4)public static void exit(int status)

强制关闭Java虚拟机,并把状态信息status传递给操作系统,status非零时,表示非正常退出。

1.6Integer类:

Integer类的属性:

MAX_VALUEMIN_VALUE规定了int型的最大和最小值。

Integer的构造函数:

public Integer(int value)public Integer(String s)分别把数字和数字字符串转变成Integer类。

Integer类的方法:

(1)public int intValue()public long longValue()public double doubleValue()

这些方法将当前的Integer类转化成相应的简单数据类型。

(2)public static int parseInt(String s)

把形参s代表的字符串转化成int型。

(3)public static Integer valueOf(String s)

把字符串s转化成Integer类的对象。

2、 java.util包及包中的类:

Java的集合类是java.util包的主要内容,集合类只容纳Object类的实例,即指向Object类对象的指针,而数组可以容纳对象和简单数据类型。在集合类中对象必须是同类型的,否则运行时会抛出例外。

2.1calendar类:

calendar类是一个抽象类。

calendar类的方法:

(1)public static Calendar getInstance()

返回基于当前时间,使用了默认时区和默认语言环境的Calendar对象。

(2)public int get(int field)

返回日历某字段的值。

calendar类的例子:

import java.util.*;

public class test{

public static void main(String[] args){

Calendar cd=Calendar.getInstance();

System.out.println(cd.get(Calendar.YEAR));

System.out.println(cd.get(Calendar.MONTH));

System.out.println(cd.get(Calendar.DAY_OF_MONTH));

}

}

2.2Properties类:

Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。

Properties类的方法:

(1)public void load(InputStream inStream) throws IOException

从输入流中读取属性列表(键和元素对)。

(2)public String getProperty(String key, String defaultValue)

返回key键的值。

import java.util.*;

import java.io.*;

public class test{

public static void main(String[] args){

Properties pro=new Properties();//定义一个Properties的对象

try{

pro.load(newFileInputStream("123.properties"));//文件123.properties为输入流

}catch(FileNotFoundException e){

System.out.println("找不到文件");

}catch(IOException e){

System.out.println("文件加载异常");

}

Stringname=pro.getProperty("name");//多个name,将返回最后一个

Stringage=pro.getProperty("age");//多个age,将返回最后一个

System.out.println(name);

System.out.println(age);

}

}

(3)public Object setProperty(String key, String value)

设置Property对象的键和对应的键值。

(4)public void store(OutputStream out, String comments) throwsIOException

Properties对象的键和值输出到输出流。

例子:

import java.util.*;

import java.io.*;

public class test{

public static void main(String[] args){

Properties pro=new Properties();

pro.setProperty("name","zhang");//Properties对象pro设置键和值

pro.setProperty("age","21");

try{

pro.store(newFileOutputStream("123.properties",true),null);

//向文件123.properties末尾加入pro的键和值。

}catch(FileNotFoundException e){

System.out.println("找不到文件");

}catch(IOException e){

System.out.println("文件加载异常");

}

}

}

3java.text包及包中的类:

3.1SimpleDateFormat类:

SimpleDateFormat 是一个以与语言环境相关的方式来格式化和分析日期的具体类。它允许进行格式化(日期 -> 文本)、分析(文本 -> 日期)和规范化。

SimpleDateFormat类的方法:

(1)public StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition pos)

将形参中的date格式化为日期/时间字符串。

(2)public Date parse(String text, ParsePostion pos)

分析从pos给定的索引处开始的字符串text,将字符串转化成日期。

例子:

import java.util.*;

import java.text.*;

public class test{

public static void main(String[] args){

SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd");//注意大小写

System.out.println(sdf.format(new Date()));//将当前日期格式化为yyyy-MM-dd

try{

Date date=sdf.parse("2001-1-12");//将字符串转化为日期

System.out.println(sdf.format(date));//格式化日期并打印

}catch(ParseException e){

System.out.println("字符串解析错误!");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值