常用类

本文深入讲解Java中的核心类,包括Object类的方法如getClass(), hashCode(), toString(), equals(), finalize()等的使用;探讨了包装类的特性及应用,例如Integer类的装箱与拆箱过程;并介绍了字符串操作技巧及StringBuffer和StringBuilder的区别。

Object

getClass()

public class Test {
    public static void main(String[] args) {
        Students s1 = new Students("韩",20);
        Students s2 = new Students("影", 15);
        //返回s1和s2的class文件
        Class c1 = s1.getClass();
        Class c2 = s2.getClass();
        //判断s1和s2是不是同一个类型
        if (c1==c2){
            System.out.println("是同一个类");
        }else{
            System.out.println("不是同一个类");
        }
    }
}

hashCode()

public class Test02 {
    public static void main(String[] args) {
        //hashCode方法
        Students s1 = new Students();
        Students s2 = new Students();
        Students s3 = s1;

        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println(s3.hashCode());
    }
}

toString()

public class Test03 {
    public static void main(String[] args) {
        Students s1 = new Students("s",20);
        Students s2 = new Students("a",50);

        //toString方法
        System.out.println(s1.toString());
        System.out.println(s2.toString());
    }
}

equals()

public class Test04 {
    public static void main(String[] args) {
        Students s1 = new Students("韩",20);
        Students s2 = new Students("影", 15);
        //equals() 判断两个对象是否相等
        Students s3 = new Students("影", 15);
        System.out.println(s1.equals(s2));
        System.out.println(s3.equals(s2));
    }
}

finalize()

public class Test05 {
    public static void main(String[] args) {
         new Students("韩",20);
         new Students("影", 15);
         new Students("傻", 15);

         System.gc();
        System.out.println("清理内存");
    }
}

包装类

Integer

public class Demo01 {
    public static void main(String[] args) {
        //类型转换:装箱,基本类型转换成引用类型的过程
        //基本类型
        int num1 = 18;
        //使用Integer类创建的对象
        Integer integer1=new Integer(num1);
        Integer integer2 = Integer.valueOf(num1);

        //类型转换:拆箱,引用类型转换成基本类型的过程
        int i = integer1.intValue();
        System.out.println(i);

        //JDK1.5之后提供了自动装箱和拆箱
        int age=30;
        //自动装箱
        Integer integer4=age;
        //自动拆箱
        int age2 = integer4;

        //基本类型和字符串之间的转换
        //1 基本类型转成字符串
        int n1 =100;
        //1.1 使用+号
        String s1 = "" + n1;
        //1.2 使用Integer中的toString()方法
        String s2 = Int eger.toString(n1);

        //2 字符串转换成基本类型
        String str="150";
        //使用Integer.parseXXX();
        int i1 = Integer.parseInt(str);
    }
}

整流缓冲区

  • Java预先创建了256个常用的整数包装类型对象
  • 在实际应用当中,对已创建的对象进行复用
  • valueOf取值范围在( -128~127)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L4cX0mcM-1603617199132)(G:\Java学习图片\包装类\valueof.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mSFk0mi8-1603617199134)(G:\Java学习图片\包装类\无标题.png)]

String

  • 字符串是常量,创建之后不能改变
  • 字符串字面值存储在字符串池中,可以共享

常用方法:

public class Demo02 {
    public static void main(String[] args) {
        //字符串方法的使用
        //1.lenght();返回字符串的长度
        //2.charAt(int index);返回某个位置的字符
        //3.contains(String str);判断是否包含某个字符串

        String content = "java是世界上最好的编程语言";
        System.out.println(content.length());//15
        System.out.println(content.charAt(0));//j
        System.out.println(content.charAt(content.length()-1));//言
        System.out.println(content.contains("java"));//true

        //4.toCharArray();返回字符串对应的数组
        //5.indexOf();返回字符串首次出现的位置
        //6.lastIndexOf();返回字符串最后一次出现的位置

        System.out.println(Arrays.toString(content.toCharArray()));
        //[j, a, v, a, 是, 世, 界, 上, 最, 好, 的, 编, 程, 语, 言]
        System.out.println(content.indexOf("好"));
        //9
        System.out.println(content.lastIndexOf("a"));
        //3


        //7.trim();去掉字符串前后的空格
        //8.toUpperCase();把小写转换成大写//toLowerCase();把大写转换成小写
        //9.endsWith(str);判断是否已str结尾;//startsWith(str),判断是否已str开头

        String content2="   hello   world   ";
        System.out.println(content2.trim());//hello   world
        System.out.println(content2.toUpperCase());// HELLO   WORLD
        System.out.println(content2.toLowerCase());// hello   world
        System.out.println(content2.endsWith("jdsd"));//false
        System.out.println(content2.endsWith("  "));//true
        System.out.println(content2.startsWith("asd"));//false
        System.out.println(content2.startsWith("   "));//true

        //10.replace(char old,char new);用新的字符或字符串替换旧的字符或字符串
        //11.split();对字符串进行拆分

        System.out.println(content.replace("java", "c++"));

        String say="java is the best programing language";
        String[] arr = say.split("[ ,]+");
        System.out.println(arr.length);
        for (String arrs : arr){
            System.out.println(arrs);
        }

        //equals、compareTo();比较大小
        //equalsIgnoreCase;忽视大小写比较
        String s1 = "hello";
        String s2 = "HELLO";
        System.out.println(s1.equals(s2));
        System.out.println(s1.equalsIgnoreCase(s2));

        String s3 = "abc";//97
        String s4 = "xyz";//120
        System.out.println(s3.compareTo(s4));//-23

        String s5 = "abc";
        String s6 = "abcxyz";
        System.out.println(s5.compareTo(s6));
    }
}

将每个单词首字母大写

//将每个单词首字母大写
String s = "this is a test";
String[] arr1 = s.split(" ");
for (int i =0;i < arr1.length;i++){
    char c = arr1[i].charAt(0);
    //将小写转换成大写
    char c1 = Character.toUpperCase(c);
    //获取大写字母和每个单词除第一个字母剩余部分
    String s7 = c1 + arr1[i].substring(1);
    System.out.println(s7);
}

StringBuffer和StringBuilder

/*
*   StringBuffer和StringBuilder的使用
*   区别:1.效率比String高
*        2.节省内存

*/
public class Demo03 {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        //1.append();追加
        sb.append("java是最好的语言");
        System.out.println(sb.toString());
        sb.append(",java真香");
        System.out.println(sb.toString());

        //2.insert();添加
        sb.insert(0,"我在最前面");
        System.out.println(sb.toString());

        //3.replace();替换
        sb.replace(0,5,"我是神");
        System.out.println(sb.toString());

        //4.delete();删除
        sb.delete(0,3);
        System.out.println(sb.toString());
    }
}

BigDecimal

public class Demo04 {
    public static void main(String[] args) {
        double a = 1.0;
        double b = 0.9;
        System.out.println(a-b);

        //面试题
        double result = (1.4-0.5)/0.9;
        System.out.println(result);
        //BigDecimal,大的浮点数精确计算
        BigDecimal bd1 = new BigDecimal("1.0");
        BigDecimal bd2 = new BigDecimal("0.9");
        //减法
        BigDecimal s1 = bd1.subtract(bd2);
        System.out.println(s1);

        //加法
        BigDecimal s2 = bd1.add(bd2);
        System.out.println(s2);

        //乘法
        BigDecimal s3 = bd1.multiply(bd2);
        System.out.println(s3);

        //除法
        BigDecimal s4 = new BigDecimal("1.4").subtract(new BigDecimal("0.5")).divide(new BigDecimal("0.9"));
        System.out.println(s4);

        BigDecimal s5 = new BigDecimal("10").divide(new BigDecimal("3"), 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(s5);
    }
}

Date

  • Date表示特定的瞬间,精确到毫秒。Date类中的大部分方法都已经被Calendar类中的方法所取代
  • 时间单位
    • 1秒=1000毫秒
    • 1毫秒=1000微秒
    • 1微妙=1000纳秒
public class Demo01 {
    public static void main(String[] args) {
        //1.创建Date对象
        //今天
        Date date1 = new Date();
        System.out.println(date1.toString());
        System.out.println(date1.toLocaleString());
        //昨天
        Date date2 = new Date(date1.getTime()-(60*60*24*1000));
        System.out.println(date2.toLocaleString());

        //2.方法 after before
        boolean b = date1.after(date2);
        System.out.println(b);
        boolean b2 = date1.before(date2);
        System.out.println(b2);


        //比较 compareTo();
        int i = date1.compareTo(date2);
        System.out.println(i);
        //比较是否相等
        date1.equals(date2);
    }
}

SimpleDateFormat

package com.simpleDateFormat;

import java.text.SimpleDateFormat;
import java.util.Date;

public class  Demo01 {
    public static void main(String[] args) throws Exception{
        //1创建SimpleDateFormat对象 y 年 M 月
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        //2创建Date
        Date date = new Date();
        //3.格式化时间(把日期转成字符串)
        String format = simpleDateFormat.format(date);
        System.out.println(format);
        //解析(把字符串转成时、时间)
        Date parse = simpleDateFormat.parse("1990年05月06日 02:52:45");
        System.out.println(parse);
    }
}

System

package com.system;

import org.w3c.dom.CDATASection;

public class Demo01 {
    public static void main(String[] args) {
        //1.arraycopy();数组的复制
        //src:源数组
        //srcPos:从哪个位置开始复制 0
        //dest:目标数组
        //destPos:目标数组的位置
        //length:复制到长度
        int [] arr = {12,54,87,62,54,89,38,45};
        int [] dest = new int[8];
        System.arraycopy(arr,4,dest,4,4);
        for (int dests : dest){
            System.out.println(dests);
        }
        //2.System.currentTimeMillis();获取当前系统时间,返回的是毫秒值
        System.out.println(System.currentTimeMillis());
        //3.System.gc();告诉垃圾回收器回收
        System.gc();
        //4.退出jvm
        System.exit(0);
        System.out.println("s  ");
    }
}

int [] dest = new int[8];
System.arraycopy(arr,4,dest,4,4);
for (int dests : dest){
System.out.println(dests);
}
//2.System.currentTimeMillis();获取当前系统时间,返回的是毫秒值
System.out.println(System.currentTimeMillis());
//3.System.gc();告诉垃圾回收器回收
System.gc();
//4.退出jvm
System.exit(0);
System.out.println("s ");
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值