6Object类String类.docx

上传人:b****5 文档编号:2794794 上传时间:2022-11-15 格式:DOCX 页数:8 大小:29.96KB
下载 相关 举报
6Object类String类.docx_第1页
第1页 / 共8页
6Object类String类.docx_第2页
第2页 / 共8页
6Object类String类.docx_第3页
第3页 / 共8页
6Object类String类.docx_第4页
第4页 / 共8页
6Object类String类.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

6Object类String类.docx

《6Object类String类.docx》由会员分享,可在线阅读,更多相关《6Object类String类.docx(8页珍藏版)》请在冰豆网上搜索。

6Object类String类.docx

6Object类String类

基础类_知识点

Object类

Object是java类层次的根,每个类都以Object类为基类。

所有的类,包括数组都实现了这个类的方法。

方法

booleanequals(Objectobj)

判定是否当前对象”等于”obj

equals方法满足:

1.自反性:

x.equals(x)返回true.

2.对称性:

对任何引用型变量x和y,x.equals(y)返回true当且仅当y.equals(x)返回true.

3.传递性:

对任何引用型变量x,y,z,x.equals(y)返回true并且y.equals(z)返回true,那么nx.equals(z)返回true.

4.一致性:

对任何访问变量x和y,只要不改变equals做比较时使用的信息,多次执行x.equals(y)都返回同样的结果。

5.如果x非空,那么x.equals(null)返回false.

Object的equals方法就是比较两个对象是否是同一个(即==)。

对任何类,只要需要比较它的两个对象是否相等,就需要重写这个方法。

ClassgetClass()

Java虚拟机运行时其内有一个名为Class的类,每个已经装入到虚拟机的类都有一个Class类的对象,该方法就是返回当前对象所属类的Class对象。

protectedObjectclone()

创建并返回当前对象的一个拷贝,拷贝的含义与当前对象所属的类有关。

一般要求,对任何对象x,

x.clone()!

=x.即x与其clone不是同一个对象。

x.clone().getClass==x.getClass。

即x与其clone是同一个类的对象。

应当是真的,但并不做绝对要求.典型地,应该有

x.clone().equals(x)

但是这个也不做硬性要求。

拷贝一个对象将确保创建这个类的一个新实例,而且也可能要求拷贝内部数据结构,但是拷贝不调用构造函数。

Object类的clone方法执行一个特殊的克隆操作:

如果当前对象所属的类没有实现Cloneable接口,那么将抛出一个CloneNotSupportedException。

否则Object的clone方法创建一个该对象所属类的一个新对象,用赋值的方式拷贝该对象的各个域到新对象的对应域(不调用构造函数),因此Object的clone方法执行浅拷贝。

Object类本身并没有实现Cloneable方法,因此Object类的对象调用clone方法导致异常。

Object类实现clone方法是为了给实现Cloneable接口的子类提供一个方便的工具,子类可能需要重写clone方法,这时可以调用基类的clone方法:

super.clone。

StringtoString()

返回当前对象的串表示。

inthashCode()

返回当前对象的hash码。

voidnotify()和wait方法与多线程有关,以后介绍。

Math类

Math类包括两个常量pi和e。

还含有三角函数,反三角函数,指数函数,对数函数等常用函数,它们都是Math类的静态方法。

包装类

8种基本类型都不是类类型。

很多类和方法只处理Object类或其子类,例如,所有泛型类的类型参数必须是类,不能是基本类型。

为此java语言为每个基本类型都提供一个相应的类,称为这个基本类型的包装类型。

包装类型的名称,类层次如下:

包装类的构造函数

1.使用基本数据构造对应的包装类对象,例如Integer(9),Character(’c’)。

2.用数的十进制字符串表示,构造对应的包装类对象,例如newFloat(“6.02e3”);Character类没有这样的构造函数。

包装类的对象都是常量,一经构造之后内容就不再改变。

以下是Integer类的主要方法,其它包装类的方法类似。

比较

intcompareTo(IntegeranotherInteger)

ComparestwoIntegersnumerically.

intcompareTo(Objecto)

ComparesthisIntegertoanotherObject.

booleanequals(Objectobj)

Comparesthisobjecttothespecifiedobject.

把当前Integer对象的值转换成基本类型的值

bytebyteValue()

shortshortValue()

intintValue()

longlongValue()

floatfloatValue()

doubledoubleValue()

把串转换成int型数

staticintparseInt(Strings)

staticintparseInt(Strings,intradix)

把int型数转换成串

staticStringtoBinaryString(inti)

staticStringtoString(inti)

staticStringtoOctalString(inti)

staticStringtoHexString(inti)

staticStringtoString(inti,intradix)

把串转换成Integer

staticIntegervalueOf(Strings)

staticIntegervalueOf(Strings,intradix)

String类

String类的对象都是串常量,一经构造之后串的内容就不再改变。

构造函数

String()

初始化一个空串。

String(byte[]bytes)

使用平台特定的字符编码方案转换字节数组成一个String对象。

String(byte[]bytes,intoffset,intlength)

String(byte[]bytes,intoffset,intlength,Stringenc)

String(byte[]bytes,Stringenc)

String(char[]value)

String(char[]value,intoffset,intcount)

String(Stringoriginal)

String(StringBufferbuffer)

方法

Get方法

charcharAt(intindex)

byte[]getBytes()

byte[]getBytes(Stringenc)

voidgetChars(intsrcBegin,intsrcEnd,char[]dst,intdstBegin)

Stringsubstring(intbeginIndex)

Stringsubstring(intbeginIndex,intendIndex)

String比较方法

intcompareTo(Objecto)

intcompareTo(StringanotherString)

intcompareToIgnoreCase(Stringstr)

booleanequals(ObjectanObject)

booleanequalsIgnoreCase(StringanotherString)

booleanregionMatches

(booleanignoreCase,inttoffset,Stringother,intooffset,intlen)

booleanregionMatches

(inttoffset,Stringother,intooffset,intlen)

booleanstartsWith(Stringprefix)

booleanstartsWith(Stringprefix,inttoffset)

staticStringcopyValueOf(char[]data)

staticStringcopyValueOf(char[]data,intoffset,intcount)

String成数组

char[]toCharArray()

String检索

booleanendsWith(Stringsuffix)

测试当前串是否以串suffix结束.

intindexOf(intch)

返回字符ch在当前串的首次出现的下标。

intindexOf(intch,intfromIndex)

返回字符ch在当前串从fromIndex开始首次出现的下标。

intindexOf(Stringstr)

返回串str在当前串首次出现的下标。

intindexOf(Stringstr,intfromIndex)

返回串str在当前串从fromIndex开始首次出现的下标。

intlastIndexOf(intch)

intlastIndexOf(intch,intfromIndex)

intlastIndexOf(Stringstr)

intlastIndexOf(Stringstr,intfromIndex)

inthashCode()

intlength()

改变String

Stringreplace(charoldChar,charnewChar)

Stringconcat(Stringstr)

Stringtrim()

StringtoLowerCase()

StringtoLowerCase(Localelocale)

StringtoUpperCase()

StringtoUpperCase(Localelocale)

StringtoString()

基本类型成String

staticStringvalueOf(booleanb)

staticStringvalueOf(charc)

staticStringvalueOf(char[]data)

staticStringvalueOf(char[]data,intoffset,intcount)

staticStringvalueOf(inti)

staticStringvalueOf(longl)

staticStringvalueOf(floatf)

staticStringvalueOf(doubled)

staticStringvalueOf(Objectobj)

Stringbuffer类

StringBuffer类的对象是串变量。

构造函数

StringBuffer()

StringBuffer(intlength)

StringBuffer(Stringstr)

方法:

append方法

StringBufferappend(booleanb)

StringBufferappend(inti)

StringBufferappend(longl)

StringBufferappend(doubled)

StringBufferappend(floatf)

StringBufferappend(charc)

StringBufferappend(char[]str)

StringBufferappend(char

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1