Java学习之路(五):常见的对象操作

Object对象

我们先来介绍一下API

API(Application Programming Interface):应用程序编程接口

Java API

  • 就是Java提供给我们使用的类,这些类将底层的实现封装起来
  • 我们不需要关心这些类是如何实现的,只需要学习这些类如何使用的就好

Ojbect是超级类,任何类都会继承与Object类,你不写,父类默认是Object

Object的概述

  • Object是类层次结构的根类
  • 所有类都直接或者间接的继承与Object类
  • Object类的构造方法: public Object()
  • 子类的构造方法默认访问的是父类的无参数构造方法

Object的hashCode方法

  • Object有一个方法public int hashCode()
  • 方法返回一个哈希码值,默认情况下该方法会根据对象的地址来计算
  • 不同对象的hashCode()一般来说不会相同,但是同一个对象的hashCode值是肯定相同的
  • 注意:hashCode不是对象的实际地址值,可以理解为逻辑地址值
public class test {

    public static void main(String[] args){
        StudentTest std1 = new StudentTest("null",11);
        StudentTest std2 = new StudentTest("null",12);
        StudentTest std3 = std1;
        System.out.println("std1:"+std1.hashCode());
        System.out.println("std2:"+std2.hashCode());
        System.out.println("std3:"+std3.hashCode());
    }
    
    
}
class StudentTest{
    String name;
    int age;
    public StudentTest(String name,int age){
        this.name = name;
        this.age = age;
    }
}

Object的getClass方法

 

  • 返回此object运行时类
  • 可以通过class类中的一个方法,获取对象的真实类的全名称
package day15_nullnull;

public class test {

    public static void main(String[] args){
        StudentTest std1 = new StudentTest("null",11);
        StudentTest std2 = new StudentTest("null",12);
        StudentTest std3 = std1;
        System.out.println(std1.getClass());
        System.out.println(std2.getClass());
        System.out.println(std3.getClass());
    }
    
    
}
class StudentTest{
    String name;
    int age;
    public StudentTest(String name,int age){
        this.name = name;
        this.age = age;
    }
}

Object类的toString方法

  •  toString方法返回此对象的字符串表示
  • 一般相当于:包名+@+Integer.toHexString(d.hashCode())
  • 但我们打印一个对象的时候,我们默认调用的就是toString方法,相当于Python中的__str__
  • 这个方法我们一个用于自定义字符串输出

建议重写toString方法

Object类的equals方法

  • 表示与其他对象是否相等
  • 默认情况下比较的是 对象的引用(地址)是否相同
  • 由于比较对象的引用没有意义,一般建议重写这个方法

补充:==与equals方法的区别

  •  ==是一个比较云算法,即可以比较基本数据类型,也可以比较引用数据类型
    • 基本数据类型比较的是值,引用数据类型比较的是地址值
  • equals方法是一个方法,只能比较数据类型
    • 默认的equals和==比较引用数据类型无任何区别
    • 但是通过自定制,我们可以做到我们想要的比较结果

Scanner类的一些介绍

 scanner是一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器

 scanner的构造方法:

  Scanner(InputStream source)

 System.in 介绍

  • System类下有一个静态的字段
  • public static final InputStream in    标准的输入流,对应着键盘录入

 Scanner的成员方法:

比较重要的:

  hasNext..  判断是否还有下一个输入项,其中...可以是Int,Double等,如果需要判断是否包含字符串,则可以省略...

  next....  获取下一个输入项

常用的:

  •  public int nextInt();   获取一个int类型的值
  • public String nextLine();  获取一个String类型的值,也就是字符串
  • 注意:如果要连用,最好使用一样的

String类的介绍

 string的构造方法:

  •  public String()
  • public String(byte[] bytes)   把字节数组转化为字符串
  • public String(byte[] bytes,int index,int length)  把字节数组的一部分转化为字符串
  • public String(char[] value,int index,int count)  把字符数组的一部分转化为字符串
  • public String(String original)  初始化一个新创建的String对象,使其表示一个与参数相同的字符序列;就是说,新创建的字符串是该参数字符串的一个副本(有没有想到深拷贝)
package day15_nullnull_01;

public class null_String {

    public static void main(String[] args){
        String str1 = new String();
        System.out.println(str1);
        
        byte[] bytes1 = {97,98,99};
        String str2 = new String(bytes1);
        System.out.println(str2);
        
        byte[] bytes2 = {97,98,99,100,101,102,103};
        String str3 = new String(bytes2,1,4);
        System.out.println(str3);
        
        char[] chr = {'a','b','c'};
        String str4 = new String(chr);
        System.out.println(str4);
        
    }
    
    
}

下面来看几道面试题

//1.判断定义为String类型的s1和s2是否相等
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
true
true
//一个常量在内存中只存有一份
答案
//2.下面这句话在内存中创建了几个对象?
String s1 = new String("abc");
//总共创建了两个对象
//常量池和堆区各有一个
答案
//3.判断定义为String类型的s1和s2是否相等
String s1 = new String("abc");
String s2 = "abc";

System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
不一样,一个在堆地址区,一个在常量区
答案
//4.判断定义为String类型的s1和s2是否相等
String s1 = "a" + "b" + "c";
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
Java的常量优化机制
其实在编辑的时候s1就已经是“abc”了
答案
//5.判断定义为String类型的s1和s2是否相等
String s1 = "ab";
String s2 = "abc";
Strign s3 = s1 + "c";
System.out.println(s3 == s2);
System.out.println(s3.equals(s2));
==  的是 false
equals 的是 true
答案

String类的判断功能

  • public boolean equals(Object anObject)     判断字符串是否一样
  • public boolean equalsIgnoreCase(String anotherString)  判断字符串是否一样,忽略大小写
  • public boolean contains(CharSequence s)  判断字符串是否包含那些字符
  • public boolean startsWith(String prefix)  判断字符串是否以什么开头
  • public boolean endsWith(String suffix) 判断字符串是否以什么结尾
  • public boolean isEmpty()  判断字符串是否为空字符串

String类的获取功能

  • int length():获取字符串的长度
  • char charAt(int index):获取指定索引位置的字符
  • int indexOf(int ch):获取指定字符在此字符串第一次出现处的索引
  • int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
  • int indexOf(int ch,int fromIndex):返回指定字符在此字符串中指定位置后第一次出现处的索引
  • int indexOf(String str,int fromIndex):返回指定字符串在此字符串中指定位置后第一次出现处的索引
  • lastIndexOf() 最后出现的位置
  • String substring(int start):从指定位置开始截取字符串,默认到未尾
  • String substring(int start,int end):从指定位置开始到指定位置结束截取字符图

String类的转化功能

  • byte[] getBytes()   把字符串转化为字节数组
  • char[] toCharArray()  把字符串转化为字符数组
  • static String valueOf(char[] chs)  把字符数组转化为字符串
  • static String valueOf(int i)  把int类型的数据转化为字符串
  • 补充:String的valueOf方法可以把任意类型的数据转换成字符串
  • String toLowerCase()  把字符串转成小写
  • String toUpperCase()  把字符串转成大写
  • String concat(String str)  把字符串拼接,且只能拼接字符串。

String类的其他的一些功能

  • public String replace(char oldChar,char newChar)   替换字符
  • public String replace(CharSequence target,CharSequence replacement)  替换字符串
  • Stringtrim(); 去除前后的空格
  • public int compareTo(String anotherString)   比较
  • public int compareToIgnoreCase(String str)   比较

StringBuffer类的概述

线程安全可变字符序列,一个类似与String的字符串缓冲区

StringBuffer内部实现是字符串数组

String和StringBuffer的区别:

  • String是一个不可变的字符序列
  • StringBuffer是一个可变的字符序列
  • StringBuffer线程安全,可以加锁

StringBuffer类的构造方法

  • public StringBuffer()    构造一个其中不带字符的字符串缓冲区,初始容量为16个字符
  • public StringBuffer(int capacity)  构造一个不带字符,但是具有指定初始容量的字符串缓冲区
  • public StringBuffer(CharSequence seq)  构造一个字符串缓冲区,它包含与指定的CharSequence相同的字符
  • public StringBuffer(String str)  构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容

StringBuffer的方法

  • public int capacity()   返回当前的容量。(理论值?)
  • public int length()    返回长度(字符数)  (实际值)

StringBuffer的添加功能

  • public StringBuffer append(String str)   可以把任意类型数据添加到字符缓冲区,并返回一个字符缓冲区本身
  • public StringBuffer insert(int offset,String str)  在指定位置把任意类型的数据插入到字符缓冲区中,并返回字符串缓冲区本身

StringBuffer的删除功能

  • public StringBuffer deleteCharAt(int index)    删除指定位置的字符,并返回本身
  • public StringBuffer delete(int start,int end)  删除从指定位置开始到指定位置结束的内容,并返回本身

StringBuffer的替换和翻转功能

  • public StringBuffer replace(int start,int end,String str)   从start开始到end用str进行替换
  • public StringBuffer reverse()    字符串的翻转

StringBuffer的截取

  • public String substring(int start)   从指定位置截取到末尾
  • public String substring(int start,int end)  截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
  • 注意:之前的返回值都是StringBuffer本事,这次返回的是String

String和StringBuffer之间的转换

String===>StringBuffer

1.通过构造方法,在创建StringBuffer时候,就将String当做参数传入

2.通过apped()方法

StringBuffer===>String

1.同过构造方法

2.通过StringBuffer的toString()方法

3.通过subString(start,end)切片的方法

补充:

 1.StringBuffer和StringBuilder的区别

  • StringBuffer是jdk1.0版本的,是线程安全的,也是效率低下的
  • StringBuilder是jdk1.5版本的,是线程不安全的,但也是效率高的

2.String和StringBuffer以及StringBuilder的区别

  • String是一个不可变的字符序列
  • StringBuffer和StringBuilder是一个可变的字符序列

猜你喜欢

转载自www.cnblogs.com/smiling-crying/p/9326933.html
今日推荐