JAVA--核心类

来源:互联网 发布:python 图像特征提取 编辑:程序博客网 时间:2024/06/06 16:47

object 类

** object类是所有类的父类,任何一个类都继承了object 类 ** 

object 类常用方法:

toString();  返回一个字符串用于描述该对象。equals(object obj); 用于比较两个对象的内存地址,判断是否为同一对象hashCode(); 返回该类的hash值。注意:重写一个类的equals方法后,我们都会重写hashCode方法。

样例代码:

package cn.itast.object; class Person{    int id;    String name;    public Person(int id,String name)    {        this.id = id;        this.name = name;    }    public Person()    {       }    public String toString()    {       return "编号" + this.id +"姓名" + this.name;    }    public boolean equals(Object obj)    {        Person p = (Person)obj;        return this.id == p.id;    }    public int hashCode()    {        return this.id;    }}public class Demo1 {    public static void main(String[] args) {         Person p1 = new Person(100,"狗娃");         System.out.println(p1);             Person p2 = new Person(100,"狗娃");         System.out.println(p1.equals(p2));         System.out.println(p1.hashCode());         System.out.println(p2.hashCode());    }}

String 类

String 的构造方法:

1.String()   创建一个空字符串对象2.String(byte[] bytes)  使用一个字节数组来创建一个字符串的对象。3.String(byte[] bytes,int offset, int length)  解码数组中指定部分4.String(char[] value)  使用一个字符数组创建一个字符串对象。5.String(char[] value,int offset, int length)   解码数组中指定部分6.String(int[] codePoints,int offset,int length)  解码数组中指定部分7.String(String original)

样例代码:

public class Demo2 {    public static void main(String[] args) {        /*byte[] buf= {97,98,99};        String hh = new String(buf);        String dd = new String(buf,1,2);        System.out.println( hh );        System.out.println( dd );*/        /*char[] arr ={'a','b','c'};        String ss = new String(arr);        String bb = new String(arr,2,1);        System.out.println(ss);        System.out.println(bb);*/        /*int[] arr={97,98,99};        String aa = new String(arr,1,1);        System.out.println(aa);*/        /*String gg = new String("sssss");        System.out.println(gg);*/    }}

StringBuffer

定义:

由于String的内容是不可变的,一旦修改马上会创建一个对象,所以为了避免导致String对象泛滥,在频繁改变字符串对象的应用中,需要使用可变的字符串缓冲区类。

实质:

StringBuffer底层是一个字符串数组,默认缓冲区的容量是16。如果字符串的长度不够,内存将自动 ** 增长一倍 **,线程安全的所有的缓冲区操作方法都是同步的。效率很低。

方法:

 1.reverse( )   把字符串反序输出 2.delete(int star,int end)   3.deleteCharAt(int index)  4.replace(int star,int end,String str)  5.toString() 6.indexOf(String str) 7.substring(int star)    8.append() 9.insert(int cur,String str) 10.lastIndexOf(String str) 

System

作用:

获取系统属性。

常用的方法:

1.arraycopy(Object src,int srcPro,Object dest,int destPro,int length)  (源数组,源数组中的起始位置,目标数组,目标数组的起始位置,要复制数组的长度)2.currentTimesMillis() 获取系统时间3.exit(int status) 0:正常退出jvm  1:异常退出4.gc()  建议jvm启动垃圾回收器回收垃圾5.getenv(String name)  根据环境变量名获取环境变量6.String getProperty(String name) 获取某个系统属性

样例代码:

public class Demo2 {     public static void main(String[] args)     {         int[] arr = {13,13,14,15,15};         int[] arr1 = new int[4];         System.arraycopy(arr,0,arr1,0,4);         System.out.println(Arrays.toString(arr1));         //System.exit(1);         System.out.println(System.getenv("path"));         System.out.println(System.currentTimeMillis());         Properties dd = System.getProperties();         //dd.list(System.out);         String ss = System.getProperty("os.name");         System.out.println(ss);     }}

Runtime

作用:

该类描述当前应用程序的运行环境,一个应用程序只有一个运行环境

方法:

1.getRuntime() 返回当前应用程序的运行环境对象。2.exec(String command) 根据指定的路径执行对应的执行文件。3.freeMemory()  返回jvm中的空闲内存。(以字节为单位)4.maxMemory()   返回jvm中试图管理的最大内存5.totalMemory()  返回jvm中内存总量

样例代码:

import java.io.IOException;import java.io.PrintStream;import java.util.Arrays;import java.util.Properties;public class Demo2 {     public static void main(String[] args) throws IOException, InterruptedException     {        Runtime runtime = Runtime.getRuntime();        Process process = runtime.exec("C:\\Windows\\notepad.exe");        //Thread.sleep(3000);        process.destroy();        System.out.println(runtime.freeMemory());        System.out.println(runtime.maxMemory());        System.out.println(runtime.totalMemory());     }}

Date

作用:

Date 类封装的是系统的当前时间。但是Date已经过时了,sun推荐使用Calendar类。Calendar: 该类是一个日历的类,封装了年月日时分秒时区。日期格式化类:SimpleDateFormat

样例代码:

 public static void main(String[] args) throws ParseException  {         /*Date date = new Date();         System.out.println(date);*/         /*Calendar  calendar = Calendar.getInstance();         System.out.println(calendar.get(Calendar.YEAR));         System.out.println(calendar.get(Calendar.MONTH)+1);         System.out.println(calendar.get(Calendar.DATE));         System.out.println(calendar.get(Calendar.HOUR_OF_DAY));         System.out.println(calendar.get(Calendar.MINUTE));         System.out.println(calendar.get(Calendar.SECOND));*/         Date date = new Date();         SimpleDateFormat dateformat = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");  //使用了默认的格式         String time = dateformat.format(date);         System.out.println(time);           String bir = "1999年01月02日  12:13:65";        Date date1= dateformat.parse(bir);         System.out.println(date1);     }

Math

方法:

1.abs(double num)        取绝对值2.ceil(double num)       向上取整 3.floor(double num)      向下取整4.round(double num)      四舍五入5.random()   产生大于等于0.0 小于1.0的随机数  Random 类可产生各种随机数

样例代码:

// 产生验证码。。。public class Demo1{         public static void main(String[] args) {             Random random = new Random();              StringBuilder str = new StringBuilder();             char arr[] = {'z','g','f','c','b'};             for(int i=0; i<4; i++){                 int index = random.nextInt(arr.length);                 str.append(arr[index]);             }             System.out.println(str.toString());         }    }
0 0