2021-08-20

常用类

getclass()方法

获得了Person这个(类)Class,进而通过返回的Class对象获取Person的相关信息,比如:获取Person的构造方法,方法,属性有哪些等等信息。

public class JiCheng.Person1class Stu {
    public static void main(String[] args) {
        Person1 p = new Person1(1,"刘德华");
        System.out.println(p.getClass());
        System.out.println(p.getClass().getName());
    }
}
class Person1{
    int id;
    String name;
    public Person1(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
}
//结果
class JiCheng.Person1
JiCheng.Person1
hashCode()方法

在java中hashcode方法是Object类的native方法,返回值为int类型,根据一定的规则将与对象相关的信息(比如对象的存储地址,对象的字段等)映射成一个数值,这个数值称作为散列值。

public class JiCheng.Person1class Stu {
    public static void main(String[] args) {
        Person1 p = new Person1(1,"刘德华");
        System.out.println(p.hashCode());
    }
}
class Person1{
    int id;
    String name;
    public Person1(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
}
//结果
460141958
toString()方法

Object类中定义有toString方法,用于返回对象的字符串表示(一个可以表示该对象属性内容的字符串),返回的字符串形式为 “类名@(十进制)hashCode值”。下面看Object类中的源码:

但可以重写,返回你所需要的字符串格式

public class JiCheng.Person1class Stu {
    public static void main(String[] args) {
        Person1 p = new Person1(1,"刘德华");
        System.out.println(p.toString());
    }
}
class Person1{
    int id;
    String name;
    public Person1(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    /*//重写ToString
        public String toString(){
            return "id"+id +"name"+name;
        }*/
}
//结果
JiCheng.Person1@1b6d3586
//类似于id: 1 name: 刘德华

equals()方法

equals比较的才是字符串的值,==比较的是地址!

public class JiCheng.Person1class Stu {
    public static void main(String[] args) {
        String a1 = "123";
        String b1 = "123";
        String a2 = new String("123");
        String b2 = new String("123");
        //“ == ”比较的是地址
        System.out.println("b1 == a1 ?  " +             (b1==a1));//true
        System.out.println("b2 == a1 ?  " + (b2==a2));//false
         //而equals()比较的是内容
         System.out.println("b1 == a1 ?  " + b1.equals(a1));//true
            System.out.println("b2 == a1 ?  " + b2.equals(a2));//true
        
    }
}

class Person1{
    int id;
    String name;
    public Person1(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
}
//结果
b1 == a1 ?  true
b2 == a1 ?  false
b1 == a1 ?  true
b2 == a1 ?  true

finalize()方法

finalize()是Object里面的一个方法,当一个堆空间中的对象没有被栈空间变量指

向的时候,这个对象会等待被java回收:jdk里面是这样实现的:

垃圾回收机器(Garbage Collection),也叫GC,垃圾回收器主要有一下特点:

  1. 当对象不再被程序所使用的时候,垃圾回收器将会将其回收
  2. 垃圾回收是在后台运行的,我们无法命令垃圾回收器马上回收资源,但是我们可以告诉他可以尽快回收资源(System.gc()和Runtime.getRuntime().gc())
  3. 垃圾回收器在回收某个对象的时候,首先会调用该对象的finalize()方法
  4. GC主要针对堆内存
  5. 单例模式的缺点
    原文链接:https://blog.csdn.net/qq_37823003/article/details/107333386
public class Stu {
    public static void main(String[] args) {
        Person1 p = new Person1();
        //送点花
        for(int i = 0 ; i< 1000; i++){
            p = null;
        }
        System.gc();//增加垃圾回收器启动的概率
    }
}

class Person1
{

    protected void finalize() throws Throwable{

        System.out.println("我快死了!!!");
    }

}
//结果
我快死了!!!

包装类

基本数据类型 包装类型

byte Byte

short Short

int Integer

long Long

float Float

double Double

boolean Boolean

char Character

public class Stu {
    public static void main(String[] args) {
        //基本类型
        int k = 18;
        //使用Integer创建对象,装箱
        Integer n1 = new Integer(k);
        Integer n2 = Integer.valueOf(k);

        //类型转化:拆箱,引用类型转成为基本类型
        Integer n3 = new Integer(100);
        int number = n3.intValue();

        //jdk1.5之后
        int age = 30;
        //自动装箱
        Integer p1 = age;
        //自动拆箱
        int p2 = p1;

    }

}
int k = 10;
//把int转化为字符串
String n1 = k + " ";
System.out.println(n1);
利用toString进行转化为16进制
System.out.println(Integer.toString(k,16));
String str = "1290";
//包装类型.parsexxx(str)把字符串转化为int
Integer.parseInt(str);
System.out.println(Integer.parseInt(str));
//boolean字符串类型转化为基本类型,
//"true"------>true,非"true"---------->false

String概述

在这里插入图片描述

import java.util.Arrays;
public class Demo3{
    public static  void main(String[] args){
        String name = "Hello";//垃圾回收
        name = "zhangsan";
        String name2 = "zhangsan"
        String str1 = new String("java");
        String str2 = new String("java");
        System.out.println(str1 == str2);//false
        System.out.println(str1.equals(str2));//true
        String content = "java是世界上最好的语言";
        //返回content长度
        System.out.println(content.length());//13
        //返回字符串长度为0的位置
        System.out.println(content.charAt(0));//j
        //是否包含Java字符串
        System.out.println(content.contains("java"));//true
        String content1 = "java是真好,为了java,而为了java";
        //把字符串分割为数组
        System.out.println(Arrays.toString
                                 (content1.toCharArray()));
//[j, a, v, a, 是, 真, 好, ,, 为, 了, j, a, v, a, ,, 而, 为, 了, j, a, v, a]    
        //查询java位置(出现的第一个)
        System.out.println(content1.indexOf("java"));//0
        //从第7个数组位置开始查询java
        System.out.println(content1.indexOf("java",7));//10
        //从末尾查询Java位置,位置从前往后
       System.out.println(content1.lastIndexOf("java"));//18
    }
}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值