第一阶段java第八章常用类库_第1页
第一阶段java第八章常用类库_第2页
第一阶段java第八章常用类库_第3页
第一阶段java第八章常用类库_第4页
第一阶段java第八章常用类库_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

本章概要String类StringBuffer类Math类Class类Object类Date类Calendar类Random类•••••••••兰赛普java课件String类3字符串字面量String

类对象未修改的原始字符串使用String类的方法可以更改字符串版本原始字符串保持不变String类的构造方法4构造方法说明String()它将创建一个空字符串String(String

value)它将新建一个字符串作为指定字符串的副本String

(char

[

]

value)它将根据字符数组构造一个新字符串String(byte

[

]

value)它将通过转换指定的字节数组新建一个字符串字符串长度字符串长度由length()方法确定语法public

intlength();返回字符串中的5字符数字符串长度字符串1Determined

bylength()

methodyntax

Returns

number

ofcharactersin

thestringPublic

int

length()长度String

name

=

"John

Smith";System.out.println

(name.length());S6字符串比较字符串1字符串2由equals()

方法确定字符串2检查组成字符串内容的字符同一个对象用==运算符检查检查字符串是否指向同一个或不同的对象字符串178字符串比较•

字符串比较运算符的用法•

使用String

类的方法,如equals()和==运算符public

class

Equality

{/**

构造方法*/protected

Equality(){}/**它演示两个字符串的比较*

@param

args

传递至main

方法的参数*/public

static

void

main(String

[]

args)

{String

string1=new

String(“苹果是一种水果");String

string2=new

String(“玫瑰花是一种花");String

string3=new

String(“苹果是一种水果");System.out.println(“字符串1:"+string1);System.out.println(“字符串2:"+string2);System.out.println(“字符串3:"+string3);if

(string1

==

string2)

{System.out.println(“字符串1

和字符串2

相等");}else

{System.out.println(“字符串1

和字符串2

不等");}if(string1.equals(string3)){

System.out.println(“字符串1

和字符串3

相等");}else

{System.out.println("字符串1

和字符串2

不等");}System.out.println(“设置字符串1

等于字符串2");string2=string1;if(string1.equals(string2)){

System.out.println(“两个字符串相等");}else

{System.out.println(“两个字符串不等");}}}字符串比较9方法说明boolean

equalsIgnoreCase(String

value)此方法比较两个字符串,忽略大小写形式int

compareTo(String

value)按字母顺序比较两个字符串。如果两个字符串相等,则返回0;如果字符串在该值之前,则返回值小于0;如果字符串在该值之后,则返回值大于0boolean

startsWith(String

value)检查一个字符串是否以另一个字符串开始。boolean

endsWith(String

value)检查一个字符串是否以另一个字符串结束。10endsWith()字符串比较•

比较不同的字符串•

使用String

类的方法,如equalsIgnoreCase()、compareTo()、startsWith()和public

class

Stringdemo

{/**

构造方法*/protected

Stringdemo(){}/**

这是main

方法*

它演示String

类的比较方法*

@param

args

传递至main

方法的参数*/public

static

void

main(String

[]

args)

{String

string1,

string2,

string3;

string1

=

new

String("Answer");string2

=

new

String("ANSWER");string3

=

new

String("Question");System.out.println(“字符串A

是"+string1);System.out.println(“字符串B

是"+string2);System.out.println(“字符串C是"+string3);if

(string1

==

string2)

{System.out.println(“字符串A

和字符串B

指同一个对象");}else

{System.out.println(“字符串A

和字符串B

指不同的对象");}if

(string1.equals(string2))

{System.out.println(“字符串A

和字符串B

的内容相同");}else

{System.out.println(“字符串A

和字符串B

的内容不同");}if(string1.equalsIgnoreCase(string2)){

System.out.println(“忽略大小写,字符串A

和B

的内容相同");}else

if(string1.equalsIgnoreCase(string3)){

System.out.println(“字符串A

和B

的内容相同");}if(pareTo("Answer")==0){

System.out.println(“按字母,字符串A

与Answer

的内容相同");}if(string1.startsWith("A")){ System.out.println(“以A

开始");}}}搜索字符串11字符串1情形1:indexOf(character)方法找到第一个匹配索引0123情形2:如果没有找到匹配,则返回-1返回找到的第一个匹配的位置索引搜索字符串12•

搜索字符串内有无指定的字符或字符串•

使用String

类的方法,如indexOf()public

class

SearchString

{/**

构造方法*/protected

SearchString(){}/**

这是main

方法它演示在字符串内搜索@param

args

传递至main

方法的参数*/public

static

void

main(String[]

args)

{String

name

=

"JohnSmith@123.com";System.out.println(“Email

ID

是:"+name);System.out.println(“@

的索引是:"+name.indexOf('@'));System.out.println(“.的索引是:"+name.indexOf('.'));if(name.indexOf('.')>name.indexOf('@')){

System.out.println(“该电子邮件地址有效");}

else

{System.out.println(“该电子邮件地址无效");}}}提取字符串13方法说明public

char

charAt(int

index)此方法用于从指定位置提取单个字符,该位置由索引指定,索引中的值必须为非负public

String

substring(int

index)此方法用于提取从位置索引开始的字符串部分public

String

substring(int

beginindex,

intendindex)此方法用于提取beginindex

和endindex位置之间的字符串部分public

String

concat(String

str)此方法用于连接两个字符串,并新建一个包含调用字符串的字符串对象public

String

replace(char

old,

char

new)此方法用于将调用字符串中出现某个字符的所有位置都替换为另一个字符public

String

trim()此方法用于返回一个前后不含任何空格的调用字符串的副本提取字符串方法说明public

char

charAt(intindex)此方法用于从指定位置提取单个字符,该位置由索引指定,索引中的值必须为非负。public

String

substring(int

index)此方法用于提取从位置索引开始的字符串部分。pex

位置之间的p一个包含调用p字符的所有位ublic

String

substring(int

beginindex,int

endindex)

此方法用于提取beginindex

和endind字符串部分。ublic

String

concat(String

str)

此方法用于连接两个字符串,并新建字符串的String

对象。ublic

String

replace(char

old,char

new)

此方法用于将调用字符串中出现某个置都替换为另一个字符。public

String

trim()此方法用于返回一个前后不含任何空格的调用字符串的副本。...char

ch;ch

=

"orange".charAt(3);...它将从index(3)中提取单个字符串

“n”并将其存储在变量ch

中1415提取字符串•

如何使用字符串提取或字符提取•

使用String

类的方法,如substring()、concat()、replace()和trim()public

class

StringMethods

{/**

构造方法*/protected

StringMethods()

{}/**

这是main

方法*

@param

args

传递至main

方法的参数*/public

static

void

main(String

[]

args)

{String

s

=

"Java

is

a

"

+

"platform

independent

language";String

s1

=

"Hello

world";String

s2

=

"Hello";String

s3

=

"HELLO";System.out.println(s);System.out.println("index

of

t

=

"

+

s.indexOf('t'));System.out.println("last

index

of

t

=

"

+s.lastIndexOf('t'));System.out.println("index

of(t,

10)

=

"

+s.indexOf('t‘,

10));System.out.println(s1.substring(3,

8));System.out.println(s2.concat("World"));System.out.println(s2.replace('l',

'w'));System.out.println(s1.trim());}}更改字符串中字符的大小写Hello使用toUpperCase()方法HELLOHELLO使用toLowerCase()方法hello语法public

String

toUpperCase();语法Public

String

toLowerCase();16更改字符串中字符的大小写•

更改字符串中字符的大小写形式•

使用String

类的方法,如toUpperCase()和toLowerCase()public

classStringTest

{/**

构造方法*/protected

StringTest(){}/**

这是main

方法它演示字符串的length()

和UpperCase()

方法@param

args

传递至main

方法*/public

static

void

main(String

[]

args)

{String

name

=

new

String("George");System.out.println(“姓名是"+name);int

length=name.length();System.out.println(“姓名的长度为”+length+“个字符");System.out.println(“姓名用大写形式表示为:");String

nameUppercase

=

name.toUpperCase();System.out.println(nameUppercase);}}17StringBuffer18构造方法说明publicStringBuffer()保留16

个字符的空间public

StringBuffer

(int

length)设置缓存器大小publicStringBuffer(Stringvalue)接收字符串参数,用来设置初始内容,并在不重新分配的情况下保留16

个字符的空间StringBuffer

用于表示可以修改的字符串使用连接运算符(+)的字符串会自动创建字符串缓冲对象StringBuffer类19方法说明StringBuffer

insert(String

s)在指定位置插入布尔值的字符串表示int

length(

)确定StringBuffer

对象的长度void

setCharAt(int

pos,

charch)使用ch

指定的新值设置pos指定的位置上的字符String

toString(

)转换为字符串形式StringBuffer

reverse()保留StringBuffer

对象中的字符StringBuffer

delete(int

start,

int

end)此方法将删除调用对象中从start

位置开始直到end

指定的索引–1位置的字符序列StringBuffer

deleteCharAt(int

pos)此方法将删除pos

指定的索引处的字符StringBuffer

replace(int

start,

int

end,Strings)此方法使用一组字符替换另一组字符。将用替换字符串从start

指定的位置开始替换,直到end

指定的位置结束不变性String类创建后直接修改不变性的概念解决方法StringBuffer

类String

的对等类表示可增加和可编写字符的可变序列将字符插入到字符串中间或附加到字符串末尾20不变性• StringBuffer

类的用法•

使用StringBuffer

类的方法,如append()、insert()、replace()、setCharAt()和toString()public

class

StringBuf

{/**

构造方法*/protected

StringBuf(){}public

staticvoid

main(String

[]args)

{StringBuffer

buf

=

new

StringBuffer("Java");buf.append(“

Guide

Ver1/”);buf.append(3);intindex

=

5;buf.insert(index,

"Student");index

=

23;buf.setCharAt(index,

'.');intstart

=

24;int

end

=

25;buf.replace(start,

end,

"4");String

s=buf.toString();

//转换为字符串

System.out.println(s);}}21Math类Math

类数字运算的方法静态方法几何函数的方法子类最终类22Math类方法说明double

sin

(double

numvalue)计算角numvalue

的正弦值double

cos

(double

numvalue)计算角numvalue

的余弦值double

pow

(double

a,

double

b)计算a

的b

次方double

sqrt

(double

numvalue)计算给定值的平方根int

abs

(int

numvalue)计算int

类型值numvalue

的绝对值,也接收long、float和double

类型的参数double

ceil

(double

numvalue)返回大于等于numvalue

的最小整数值double

floor

(double

numvalue)返回小于等于numvalue

的最大整数值int

max(int

a,

int

b)返回int

型值a

和b

中的较大值,也接收long、float

和double

类型的参数int

min(int

a,

int

b)返回a

和b

中的较小值,也可接收long、float

和double

类型的参数

23Math类••Math

类的用法使用Math

类的方法,如ceil()、floor()和round()public

class

MathDemo

{/**

构造方法*/protected

MathDemo(){}/**

main

方法演示Math

类的不同方法*

@param

args

传递至main

方法的参数*/public

static

void

main(String[]

args)

{/**

此变量存储num

的值*/int

num=38;/**

该变量存储num1

的值*/float

num1=65.7f;System.out.println(Math.ceil(num));System.out.println(Math.ceil(num1));System.out.println(Math.floor(num));System.out.println(Math.floor(num1));System.out.println(Math.round(num));System.out.println(Math.round(num1));}24}Class类使用对象中的getClass()方法使用静态forName()方法创建的对象或25无需声明自动创建对象通过Class类• Class

类的用法•

使用Class

类的方法,如getClass()和getSuperClass()class

StoreString

{/**构造方法.*/protected

StoreString(){}private

String

name

=

"diana";}/**

这个类扩展StoreString

类.*/class

StoreInteger

extends

StoreString

{/**

构造方法.*/protected

StoreInteger(){}/**

该变量存储整数值.*/private

int

deptno;}public

class

ClassDemo

{/**构造方法*/protected

ClassDemo(){}/**

这个类演示Class

类的访问方法*

@param

args

传递至main

方法的参数*/public

static

void

main(String[]

args)

{StoreString

objString

=

new

StoreString();StoreInteger

objInteger

=

new

StoreInteger();Class

objClass;objClass

=

objString.getClass();System.out.println(“objString

对象的类型是:“+objClass.getName());objClass=objInteger.getClass();System.out.println(“objInteger

对象的类型是:"+objClass.getName());objClass=objClass.getSuperclass();System.out.println(“objInteger的父类是"+objClass.getName());}}26Object27方法说明booleanequals(Objectobj)将当前对象实例与给定的对象进行比较,检查它们是否相等void

finalize()

throwsThrowable当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。通常被子类重写String

toString()返回此对象的字符串表示void

wait()

throwsInterruptedException使当前线程进入等待状态所有类的父类默认情况下,用户定义的类扩展自Object

类Object类28• Object

类的用法•

使用Object

类的方法,如equals()演示:示例11/**

这个类演示Object

类.*

@version

1.0,

2005

年6

月13

日*

@author

Ben*/public

class

ObjectDemo

{/**

构造方法*/protected

ObjectDemo()

{}/**

这是main

方法它演示Object

类@param

args

传递至main

方法的参数*/public

static

void

main(String[]

args)

{if

(args[0].equals(“Java”))

{System.out.println(“是,Java

是一项非常好的技术!");}}}Date类Date类表示日期和时间提供操纵日期和时间各组成部分的方法Date类的最佳应用之一是获取系统当前时间29Date类构造方法构造方法

说明Date()

使用当天的日期创建DateDate(longdt)

使用自1970年1

月1

日以后的指定毫秒数创建Datevoid

display()

{String

strDate

,

strTime

=

"";System.out.println(“今天的日期是:"+objDate);long

time

=

objDate.getTime();System.out.println(“自1970

年1

月1

日起"+“以毫秒为单位的时间(GMT):"+time);strDate=objDate.toString();//提取GMT

时间strTime

=

strDate.substring(11

,

(strDate.length()

-

4));

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论