黑马程序员——基础学习(八)API中Object、Scanner以及String的用法概述

本文详细介绍了Java中的重要概念,包括API的应用、Object类的功能及其方法、Scanner类的使用、String类的特点及构造方法等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

API

API:Application Programming Interface

Object

是所有类的根类,所有类都直接或者间接的继承自该类。

常用方法:

A:public int hashCode() 对象的哈希值。不是实际地址值。

例如:

Student s = new Student();

System.out.println(s.hashCode());

B:public final Class<?> getClass() 返回对象的字节码文件对象。

例如:

Student s = new Student();

Class c = s.getClass();

C:public String toString() 对象的字符串表示。要求重写,可以自动生成。

例如:

Student s = new Student();

System.out.println(s.toString()); 

D:public boolean equals(Object obj) 默认比较对象的地址值,可以根据需求重写

E:protected void finalize() 用于垃圾回收

F:protected Object clone() 复制对象数据

例如:

Student ss = (Student) s.clone();

 

面试题:

==号和equals()的区别?

==:

      基本类型:比较的是基本类型的值是否相同

      引用类型:比较的是引用类型的地址值是否相同

equals:

    默认情况下,比较的对象的地址值是否相同。

      如果我们想按照我们的需要把对象进行比较,那么,我们应该重写equals()法。

字符串直接赋值的做法:

  首先去字符串常量池里面找,看有没有该字符串存在。

  如果有,就直接返回该字符串的地址。

  如果没有,就在常量池里面创建一个字符串。

 

Scanner

使用:

Scanner sc = new Scanner(System.in);

 

int nextInt()

String nextLine()

int再String的注意问题及解决方案。

A:不同的对象就可以解决。

B:把所有的数据都当作字符串获取,然后要什么,就转换为什么类型。

我的想法

scanner是人机交互的一个重要类

String

由多个字符组成的一串数据。

构造方法:

A:public String()

例如:

String s1 = new String();

B:public String(byte[] bys)

例如:

byte[] bytes = { 97, 98, 99, 100, 101 };

String s2 = new String(bytes);

C:public String(byte[] bys,int index,int count)

例如:

String s3 = new String(bytes, 1, 2);

D:public String(char[] chs)

例如:

char[] chs = { 'a', 'b', 'c', 'd', 'e' };

String s4 = new String(chs);

E:public String(char[] chs,int index,int count)

例如:

String s5 = new String(chs, 2, 3);

F:public String(String str)

例如:

String s6 = new String("abcde");

G:String s = "hello";

例如:

String s7 = "abcde";

String类的特点及面试题

A:特点

字符串一旦被赋值,内容就不会在发生改变。

String s = new String("hello")和String s = "hello"的区别,前提:String类重写了equals()方法,用于比较字符串的内容是否相同


String s1 = new String("hello");

String s2 = new String("hello");

System.out.println(s1 == s2);//false

System.out.println(s1.equals(s2));//ture

 

String s3 = new String("hello");

String s4 = "hello";

System.out.println(s3 == s4);//false

System.out.println(s3.equals(s4));//true

 

String s5 = "hello";

String s6 = "hello";

System.out.println(s5 == s6);//true

System.out.println(s5.equals(s6));//true

4)String类的常用功能

A:判断功能

boolean equals(Object obj):判断括号里的字符串的内容是否和引用此方法的字符串内容完全一样

boolean equalsIgnoreCase(String str):忽略大小写,判断括号里的字符串的内容是否和引用此方法的字符串内容完全一样

boolean contains(String str):判断字符串是否以含义指定字符串

boolean startsWith(String str):判断字符串是否以指定字符串开头

boolean endsWith(String str):判断字符串是否以指定字符串结尾

B:获取功能

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):获取一个字符从某一索引位置开始的之后第一次出现的位置索引

String substring(int start):获取从指定位置开始截取的字符串

String substring(int start,int end):获取从指定位置开始到某一位置结束截取的字符串

C:转换功能

byte[] getBytes():将String类型的字符串变成byte类型的数组

char[] toCharArray():将String类型的字符串变成char类型的数组

static String valueOf(char[] chs):把char类型的数组转换为String类型的字符串

static String valueOf(int i):int类型的数据转换为String类型的字符串

String toLowerCase():将字符串的内容全部变成小写

String toUpperCase():将字符串的内容全部变成大写

String concat(String str):拼接字符串

D:其他功能

String replace(char old,char new):在字符串中用新的字符替换旧的字符

String replace(String old,String new):在字符串中用新的字符串替换旧的字符串

 

String trim():去掉字符串两端的空格

int compareTo(String str):按字典顺序比较两个字符串

int compareToIgnoreCase(String str):忽略大小写,按字典顺序比较两个字符串

我的代码

class StringDemo{
	public static void main(String args[]){
		//创建字符串对象
		String s = "HelloWorld";
		// int length():获取字符串的长度,其实就是字符串中字符的个数
		System.out.println(s.length());
		System.out.println("----------");
		// char charAt(int index):获取指定索引处的字符
		System.out.println(s.charAt(4));
		System.out.println("-------------");
		// int indexOf(int ch):获取指定的字符在字符串中第一次出现的索引位置
		System.out.println(s.indexOf('H'));
		System.out.println("----------");
		// int indexOf(int ch,int fromIndex):获取从指定位置开始的字符在字符串中第一次出现的索引位置
		System.out.println(s.indexOf('l',2));
		System.out.println(s.indexOf('o',4));
		System.out.println("-----------");
		// String substring(int start):从指定的位置开始截取一个字符串
		System.out.println(s.substring(6));
		System.out.println("-----------");
		// String substring(int start,int end):从指定位置开始到指定位置结束截取一个字符串
		// 需求:我要得到字符串 owor
		System.out.println(s.substring(4,8));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值