Java常用类

Object类

  1. 超类、基类,所有类的直接或间接父类,位于继承树的最顶层。

  2. 任何类,如没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承。

  3. 0bject 类中所定义的方法,是所有对象都具备的方法。 0bject 类型可以存储任何对象。 作为参数,可接受任何对象。作为返回值,可返回任何对象。

     

     

    getClass()方法

    作用:返回两个对像的class类型。

package Object;
​
public class Application {
    public static void main(String[] args) {
        GetClass getClass1 = new GetClass("lili",12);
        GetClass getClass2 = new GetClass("meimei",18);
        Class aClass = getClass1.getClass();
        Class bClass = getClass1.getClass();
        if (aClass==bClass){
            System.out.println("getClass1与getClass1是同一个类");
        }else{
            System.out.println("getClass1与getClass1不是是同一个类");
        }
    }
}

hashCode()方法

  • public int hashCode(){};

  • 返回该对象的哈希码值

  • 哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值。

  • 一般情况下,相同对象返回相同的哈希码。

package Object;
​
public class Application {
    public static void main(String[] args) {
        GetClass getClass1 = new GetClass("lili",12);
        GetClass getClass2 = new GetClass("meimei",18);
        Class aClass = getClass1.getClass();
        Class bClass = getClass1.getClass();
        if (aClass==bClass){
            System.out.println("getClass1与getClass1是同一个类");
        }else{
            System.out.println("getClass1与getClass1不是是同一个类");
        }
        System.out.println(getClass1.hashCode());
        System.out.println(getClass2.hashCode());
    }
}
输出:
getClass1与getClass1是同一个类
460141958
1163157884

toString()方法

public String toString()

返回对象的字符串表示形式。一般来说, toString方法返回一个“textually代表”这个对象的字符串。结果应该是一个简明扼要的表达,容易让人阅读。建议所有子类覆盖此方法。

toString类方法Object返回一个由其中的对象是一个实例,该符号字符的类的名称的字符串@` ”和对象的哈希码的无符号的十六进制表示。 换句话说,这个方法返回一个等于下列值的字符串:

getClass().getName() + '@' + Integer.toHexString(hashCode())

  • 结果

    对象的字符串表示形式。

public class Application {
    public static void main(String[] args) {
        GetClass getClass1 = new GetClass("lili",12);
        GetClass getClass2 = new GetClass("meimei",18);
        Class aClass = getClass1.getClass();
        Class bClass = getClass1.getClass();
        //1.getClass
        System.out.println("===========1.getClass==============");
        if (aClass==bClass){
            System.out.println("getClass1与getClass1是同一个类");
        }else{
            System.out.println("getClass1与getClass1不是是同一个类");
        }
        //2.hashCode
        System.out.println("============2.hashCode=============");
​
        System.out.println(getClass1.hashCode());
        System.out.println(getClass2.hashCode());
        //3.toString
        System.out.println("==============3.toString=============");
        System.out.println(getClass1.toString());
        System.out.println(getClass2.toString());
    }
}
===========1.getClass==============
getClass1与getClass1是同一个类
============2.hashCode=============
460141958
1163157884
==============3.toString=============
Object.GetClass@1b6d3586
Object.GetClass@4554617c

equals

 

public boolean equals(Object obj)

指示一些其他对象是否等于此。

equals方法在非空对象引用上实现等价关系:

  • 自反性 :对于任何非空的参考值xx.equals(x)应该返回true

  • 它是对称的 :对于任何非空引用值xyx.equals(y)应该返回true当且仅当y.equals(x)回报true

  • 传递性 :对于任何非空引用值xyz ,如果x.equals(y)回报truey.equals(z)回报true ,然后x.equals(z)应该返回true

  • 它是一致的 :对于任何非空引用值xy ,多次调用x.equals(y)始终返回true或始终返回false ,没有设置中使用的信息equals比较上的对象被修改。

  • 对于任何非空的参考值xx.equals(null)应该返回false

equals类方法Object实现对象上差别可能性最大的相等关系; 也就是说,对于任何非空的参考值xy ,当且仅当xy引用相同的对象( x == y具有值true )时,该方法返回true

请注意,无论何时覆盖该方法,通常需要覆盖hashCode方法,以便维护hashCode方法的通用合同,该方法规定相等的对象必须具有相等的哈希码。

  • 参数

    obj - 与之比较的参考对象。

  • 结果

    true如果此对象与obj参数相同; false否则。

//4.equals
System.out.println("==============4.equals=============");
System.out.println(getClass1.equals(getClass2));
​
false

finalize()方法

  • 当对象被判定为垃圾对象是,由JVM自动调用此方法,用与标记垃圾对象,进入回收队列。

  • 垃圾对象:没有有效引用指向此对象是,即为垃圾对象。

  • 垃圾回收:有GC销毁垃圾对象,释放数据存储空间。

  • 自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象。

  • 手动回收机制:使用System.gc();通知JVM执行垃圾回收。

包装类

基本数据类型所对应的包装类。

Object可以统一所有数据,包装类的默认值是null。

 

基本数据类型包装类型
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter

类型转换与装箱、拆箱

把基本类型转换为引用类型叫装箱;

把引用类型转换为基本类型叫拆箱;

基本类型一般放在栈里面,二引用类型放在堆里面。

public class AutoBoxing {
    public static void main(String[] args) {
        int age = 18;
        Integer integer1 = new Integer(age);
        Integer integer2 = Integer.valueOf(age);
        System.out.println("==============手动装箱================");
        System.out.println(integer1);
        System.out.println(integer2);
        Integer integer3 = new Integer(100);
        int num = integer3.intValue();
        System.out.println("================手动拆箱==============");
        System.out.println(num);
        //JDK1.5之后提供了自动装箱和拆箱的功能
        int money=1000;
        Integer integer4 = money;
        System.out.println("==============自动装箱=================");
        System.out.println(integer4);
        int money2;
        money2 = integer4;
        System.out.println("===============自动拆箱================");
        System.out.println(money2);
​
    }
}

==============手动装箱================ 18 18 ================手动拆箱============== 100 ==============自动装箱================= 1000 ===============自动拆箱================ 1000

 

String类

 

  • public int length():返回字符串的长度。

  • public char charAt(int index):根据下标获取字符。

  • public boolean contains(String str):判断当前字符串中是否包含str。

  • public char[] toCharArray():将字符串转换为数组。

  • public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1。

  • public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引。

  • public String trim():去掉字符串前后的空格。

  • public String toUpperCase():将小写转成大写。

  • public boolean endWith(String str):判断字符串是否以str结尾。

  • public String replace(char oldChar, char newChar);将旧字符串替换成新字符串 。

  • public String[] split(String str):根据str做拆分。

import java.util.Arrays;
​
public class StringFunction {
    public static void main(String[] args) {
        //public int length():返回字符串的长度。
        //public char charAt(int index):根据下标获取字符。
        //public boolean contains(String str):判断当前字符串中是否包含str。
        System.out.println("===============1===============");
        String str1 = "This is China,my hometown!";
        System.out.println(str1.length());
        System.out.println(str1.charAt(10));
        System.out.println(str1.contains("China"));
        /**
         * public char[] toCharArray():将字符串转换为数组。
         * public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1。
         * public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引。
         */
        System.out.println("===============2===============");
        String str2 = "I am proud of myself,not for my did,but for i am doing!";
        System.out.println( str2.toCharArray());
        System.out.println(Arrays.toString(str2.toCharArray()));
        System.out.println(str2.indexOf("am"));
        System.out.println(str2.lastIndexOf("am"));
        System.out.println("===============3===============");
        /*
             public String trim():去掉字符串前后的空格。
             public String toUpperCase():将小写转成大写。
             public boolean endWith(String str):判断字符串是否以str结尾。startWith(String str):判断字符串是否以str开头。
         */
        String str3 = "  hello WOrld  ";
        System.out.println(str3.trim());
        System.out.println(str3.toLowerCase());
        System.out.println(str3.toUpperCase());
        String str4 = "nihao.java";
        System.out.println(str4.endsWith(".java"));
        System.out.println(str4.startsWith("nihao"));
        System.out.println("===============4===============");
        /*
         public String replace(char oldChar, char newChar);将旧字符串替换成新字符串 。
         public String[] split(String str):根据str做拆分。
         */
        String str5 = "欢迎来到中国";
        System.out.println(str5.replace("中国","没国"));
        String[] s = str5.split("来");
        for (String s1 : s) {
            System.out.println(s1);
        }
    }
}
===============1===============
26
i
true
===============2===============
I am proud of myself,not for my did,but for i am doing!
[I,  , a, m,  , p, r, o, u, d,  , o, f,  , m, y, s, e, l, f, ,, n, o, t,  , f, o, r,  , m, y,  , d, i, d, ,, b, u, t,  , f, o, r,  , i,  , a, m,  , d, o, i, n, g, !]
2
46
===============3===============
hello WOrld
  hello world  
  HELLO WORLD  
true
true
===============4===============
欢迎来到没国
欢迎
到中国
​

可变字符串

StringBuffer:可变长字符串,JDK1. 0提供,运行效率慢、线程安全。 StringBuilder:可变长字符串,JDK5. 0提供,运行效率快、线程不安全。

常用方法:

append();追加

insert(index,“str“);添加

replace(indexstart,indexend,“str”);替换

delete(start,end);删除

public class StringBufferAndStringBuilder {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
       // String string = " ";
       // for (int i = 0; i < 99999; i++) {
       //     string+=i;
       // }
        //System.out.println(string);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 99999; i++) {
            stringBuffer.append(i);
        }
        //System.out.println(stringBuffer.toString());
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}

BigDecimal

位置: java. math包中。 作用:精确计算浮点数。 创建方式: BigDecimal bd=new BigDecimal( “1.0”); 方法:

  • BigDecimal add (Bi gDecimal bd) 加

  • BigDecimal subtract (BigDecimal bd) 减

  • BigDecimal multiply (BigDecimal bd) 乘

  • BigDecimal divide (Bi gDec imal bd) 除

 

 

进行除法运算时,如果不能 准确的计算出结果时需要指 定保留的位数和取舍方式。

 

除法: divide (BigDecimal bd, int scal,RoundingMode mode)

参数scal :指定精确到小数点后几位。

参数mode :

  • 指定小数部分的取舍模式,通常采用四舍五入的模式。

  • 取值为Bi gDecimal. ROUND_ HALF_ _UP。

Date

Date 表示特定的瞬间,精确到毫秒。Date类中的大部分方法都已经被Calendar类中的方法所取代。

  • 时间单位:

  • 1秒=1000毫秒

  • 1毫秒=1000微秒

  • 1微秒=1000纳秒

import java.util.Date;
​
public class DateText {
    public static void main(String[] args) {
        //今天日期
        Date date1 = new Date();
        System.out.println(date1.toString());
        System.out.println(date1);
        System.out.println(date1.toLocaleString());//过时方法
        /*
           Sat Jan 23 16:44:12 CST 2021
           Sat Jan 23 16:44:12 CST 2021
           2021-1-23 16:44:12
          
         */
        Date date2 = new Date(date1.getTime()-(60*60*24*1000));//昨天日期
        //getTime()获取当前的毫秒数,相较于1970
        System.out.println(date2.toLocaleString());
        /*
           2021-1-22 16:44:12
         */
        //方法 after before
        System.out.println(date1.after(date2));
        System.out.println(date1.before(date2));
        /*
        true
        false
         */
        //比较 comparTo()
        int d = date1.compareTo(date2);
        int f = date2.compareTo(date1);
        System.out.println(d);
        System.out.println(f);
        /*
        1
        -1
         */
        //比较是否相等equals()
        System.out.println(date1.equals(date2));
        /*
        false
         */
    }
}

Calendar

  • Calendar提供了获取或设置各种日历字段的方法。

  • 构造方法:protected Calendar();由于修饰符是protected,所以无法直接创造该对象。

  • 其他方法:

    方法名说明
    static Calendar getInstance()使用默认时区和区域获取日历
    void set(int year,int month,int date,int hourofday,int minute,int second)设置日历的年、月、日、时、分、秒。
    int get(int field)返回给定日历字段的值。字段比如年、月、日等
    void setTime(Date date)用给定的Date设置此日历的时间。Date-Calendar
    Date getTime()用给定的Date设置此日历的时间。Date-Calendar
    void add(int field,int amount)用给定的Date设置此日历的时间。Date-Calendar
    long getTimeInMillies()以毫秒为单位返回该日历的时间值。
    public class CalenderTest {
        public static void main(String[] args) {
            Calendar calendar1 = Calendar.getInstance();//创造Calendar对象
            System.out.println("目前时间:"+calendar1.getTime().toInstant());
            //获取时间信息
            int year = calendar1.get(Calendar.YEAR);
            int month = calendar1.get(Calendar.MONTH);//月份从0开始的
            int day = calendar1.get(Calendar.DAY_OF_MONTH);
            int hour = calendar1.get(Calendar.HOUR);
            int min = calendar1.get(Calendar.MINUTE);
            int second = calendar1.get(Calendar.SECOND);
            System.out.println(year+"年"+(month+1)+"月"+day+"日,"+hour+":"+min+":"+second);
            //修改时间
            Calendar calendar2 = Calendar.getInstance();
            calendar2.set(Calendar.DAY_OF_MONTH,5);
            System.out.println(calendar2.getTime().toLocaleString());
            //add方法修改时间
            Calendar calendar3 = Calendar.getInstance();
            calendar3.add(Calendar.HOUR,-1);
            System.out.println(calendar3.getTime().toLocaleString());
            int max = calendar3.getActualMaximum(Calendar.DAY_OF_MONTH);//获取当前月份最大值
            System.out.println(max);
        }
    }
    输出:
    目前时间:2021-01-23T13:49:47.095Z
    2021年1月23日,9:49:47
    2021-1-5 21:49:47
    2021-1-23 20:49:47
    31

SimpleDateFormate

格式化时期

public class SimpleDateFormateTest  {
    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd,HH-mm-ss");
        //创建Date日期
        Date date1 = new Date();
        //格式化date(把日期转化为字符串)
        String string = sdf.format(date1);
        System.out.println(string);
        //解析,把字符串转化为日期
        Date date2 = sdf.parse("1998-03-03,21-04-56");
        System.out.println(date2.toLocaleString());
    }
}
2021-01-23,22-02-15
1998-3-3 21:04:56

System

System系统类,主要用于获取系统的属性数据和其他操作,构造方法是私有的。

方法名说明
static voidarraycopy(...);复制数组
static lang currentTimeMillis();获取当前系统时间,返回的是毫秒值
static void gc();建议JVM赶快启动垃圾回收其回收垃圾
static void exit(int status);退出jvm,如果参数是0,表示正常退出jvm,非0表示异常退出jvm.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值