Object类,Scanner类,String类

Object类

一.Object类的基本概念

Object:是类层次结构的根类.每个类都使用 Object 作为超类(父类),就好像Object类是 eclipse软件的"说明书",Object类中有各种方法,并且所有类都默认继承于Object(但不属于多继承,不和其他继承冲突),通过继承可以理解为Object中的方法其他类都可以调用和使用(因为默认为其他类继承于Object)

二.Object中的方法

public int hashCode()返回该对象的哈希码值
public final Class getName()以 String 的形式返回此Class 对象所表示的实体的名称
public String toString()回该对象的字符串表示
public boolean equals(Object obj)指示其他某个对象是否与此对象“相等”
protected Object clone()创建并返回此对象的一个副本
  

方法注解

public int hashCode()返回该对象的哈希码值
1.public int hashCode()   返回该对象的哈希码值
public class abc {
}
public static void main(String[] args) {
 abc a = new abc() ;
System.out.println(a.hashCode()); //输出为:118352462

通过方法调用输出哈希码值(理解为地址值,都是唯一的)

2.

public boolean equals(Object obj)指示其他某个对象是否与此对象“相等”

equals是Object类中的一个方法, 默认比较的是地址值是否相同,但分两种情况

String类型中比较两个数据是否相等

类与类的比较中比较是否地址值相同,但可以通过方法重写来比较成员变量是否相同

3

protected Object clone()创建并返回此对象的一个副本

clone实现的前提是:使用clone方法的类必须先实现Cloneable接口

如果该类没有实现接口,则会抛出 CloneNotSupportedException的异常

三.Scanner类

public class abc{
import java.util.Scanner;  //导包	
	public static void main(String[] args) {		
Scanner sc = new Scanner(System.in) ;  //创建键盘录入对象
int a = sc.nextInt() ;   //接收数据
System.out.println("a:"+a);  //打印接受到的数据
 }	
}	
public boolean hasNextXXX()判断输入的数据是否符合xxx类型

录入结果异常报错:java.util.InputMismatchException:  录入的数据和接收的数据类型不匹配异常

△注意事项:如果第一个录入为int类型,第二次录入为String则String无法录入,因为第一次录入int类型后,自带空格,则String则接收到的是"空格"这个数据,则无显示,解决方案:从新建立键盘录入对象

四.String

       

String()
无参构造
String(byte[] bytes)
将字节数转换成字符串
public String(byte[] bytes, int index,int length)
将字节数组的一部分转换成字符串
public String(char[] value)
将字符数组转化成字符串
public String(char[] value, int index, int count)
将字符数组的一部分转换成字符串
public String(String original)
将一个字符串常量构造成一个字符串对象
String被赋值后,其值不会改变(理解为final String)

String类的常用的判断功能

boolean equals(Object obj)将此字符串与指定的对象比较
boolean equalsIgnoreCase(String str)将此 String 与另一个 String 比较,不考虑大小写
boolean contains(String str)判断当前大川中是否包含子字符串
boolean startsWith(String str)以当前str字符串开头
boolean endsWith(String str)以当前str字符串结尾
boolean isEmpty()判断字符串是否为空

String类的常用获取功能

public int length()    获取字符串的长度
public char charAt(int index)返回指定索引处的 字符
public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引
public int indexOf(int ch,int fromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
public int indexOf(String str,int fromIndex)回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索
public String substring(int beginIndex)从指定位置开始截取,默认截取到末尾,返回新的字符串
public String substring(int beginIndex, int endIndex)从指定位置开始到指定位置末尾结束(包前不包含)

String的常用转换功能

public byte[] getBytes()将字符串转换为字节数组
public char[] toCharArray()将字符串转换成字符数组
public static String valueOf(int i)将int类型的数据转换成字符串(可以将任何类型的数据转化成String类型)
public String toLowerCase()转成小写
public String toUpperCase()字符串中所有的字符变成大写

String类型的其他功能

public String replace(char oldChar,char newChar)将大字符串中的某个字符替换掉成新的字符
public String replace(String oldStr,String newStr)将大串中的某个子字符串替换掉
public String trim()去除字符串两端空格
public int compareTo(String anotherString)按字典顺序比较两个字符串

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值