Java是一种真正的面向对象的语言,即使是开发简单的程序,也必须设计对象。Java自身也为我们提供了许多已设计好的类,要想灵活使用Java进行编程,熟悉Java的这些主要类将是必不可少的前提条件之一。

1     String类

 String是串的意思,这个类是字符串常量的类。Java中的字符串和C语言中的字符串是有区别的。

在C语言中,并没有真正意义上的字符串,C语言中的字符串就是字符数组,在Java中,字符串常量是一个String类,它和字符数组是不同的

1.1 String类的构造函数:

(1)public String() 创建一个空的字符串常量。

如: String test=new String();          或: String test;             test=new String();

(2)public String(String value )用一个已存在的字符串常量作参数创建一个新的字符串常量

另外Java会为每个用双引号”……”括起来的字符串常量创建一个String类的对象 如: String k=”Hi.”; Java会为”Hi.”创建一个String类的对象,然后把这个对象赋值给k。等同于: String temp=new String(“Hi.”); String k=temp;

(3)public String( char value[] )用一个字符数组作为参数来创建一个新的字符串常量。

 char z[]={‘h’,’e’,’l’,’l’,’o’}; String test=new String(z); 注:此时test中的内容为”hello”

(4)public String( char value[], int offset, int count ) 用字符数组value,从第offset个字符起取count个字符来创建一个String类的对象

char z[]={‘h’,’e’,’l’,’l’,’o’}; String test=new String(z,1,3); 如果 起始点offset 或 截取数量count 越界,将会产生异常 StringIndexOutOfBoundsException

(5)public String( StringBuffer buffer ) 用一个StringBuffer类的对象作为参数来创建一个新的字符串常量。

String类是字符串常量,而StringBuffer类是字符串变量,是不同的

1.2  String类的方法有:

 (1)public char charAt( int index ) 获取字符串常量中的一个字符。参数index指定从字符串中返回第几个字符, String s=”hello”; char k=s.charAt(0); (注:此时k的值为’h’)

(2)public int compareTo( String anotherString ) 比较字符串常量的大小,参数anotherString为另一个字符串常量。若两个字符串常量一样,返回值为0。若当前字符串常量大,则返回值大于0。若另一个字符串常量大,则返回值小于0。用法如: String s1=”abc”; String s2=”abd”; int result=s2.compareTo(s1);(注:result的值大于0,因为d在ascII码中排在c的后面,则s2>s1)

(3)public String concat( String str ) 这个方法将把参数字符串常量str接在当前字符串常量的后面,生成一个新的字符串常量,并返回。 String s1=”How do “; String s2=”you do?”; String ss=s1.concat(s2); (注:ss的值为”How do you do?”)

(4)public boolean startsWith( String prefix ) 判断当前字符串常量是不是以参数prefix字符串常量开头的。是,返回true。否,返回false。 String s1=”abcdefg”; String s2=”bc”; boolean result=s1.startsWith(s2); (注:result的值为false)

(5)public boolean startsWith ( String prefix, int toffset ) 这个重载方法新增的参数toffset指定 进行查找的起始点。

 (6)public boolean endsWith( String suffix ) 判断当前字符串常量是不是以参数suffix字符串常量结尾的。是,返回true。否,返回false。 String s1=”abcdefg”; String s2=”fg”; boolean result=s1.endsWith(s2); (注:result的值为true)

(7)public void getChars ( int srcBegin, int srcEnd, char dst[], int dstBegin ) 这个方法用来从字符串常量中截取一段字符串并转换为字符数组。参数srcBegin为截取的起始点,srcEnd为截取的结束点,dst为目标字符数组,dstBegin指定将截取的字符串放在字符数组的什么位置。实际上,srcEnd为截取的结束点加1,srcEnd-srcBegin为要截取的字符数,用法如: String s=”abcdefg”; char z[]=new char[10]; s.getChars(2,4,z,0); (注:z[0]的值为’c’,z[1]的值为’d’, 截取了两个字符 4-2=2)

(8)public int indexOf( int ch ) 返回值为字符ch在字符串常量中从左到右第一次出现的位置。若字符串常量中没有该字符,则返回-1。用法如: String s=”abcdefg”; int r1=s.indexOf(‘c’); int r2=s.indexOf(‘x’);(注:r1的值为2,r2的值为-1)

 (9)public int indexOf ( int ch, int fromIndex ) 对上一个方法的重载,新增的参数fromIndex为查找的起始点。用法如: String s=”abcdaefg”; int r=s.indexOf(‘a’,1) (注:r的值为4)

(10)public int indexOf( String str ) 返回字符串常量str在当前字符串常量中从左到右第一次出现的位置,若当前字符串常量中不包含字符串常量str,则返回-1。用法如: String s=”abcdefg”; int r1=s.indexOf(“cd”); int r2=s.indexOf(“ca”);(注:r1的值为2,r2的值为-1)

 (11)public int indexOf ( String str, int fromIndex ) 这个重载方法新增的参数fromIndex为查找的起始点。以下四个方法与上面的四个方法用法类似,只是在字符串常量中从右向左进行查找。

public int lastIndexOf( int ch )

public int lastIndexOf( int ch, int fromIndex )

public int lastIndexOf( String str )

public int lastIndexOf( String str, int fromIndex )

 public int length() 这个方法返回字符串常量的长度,这是最常用的一个方法。用法如: String s=”abc”;int result=s.length();(注:result的值为3)

public char[] toCharArray()将当前字符串常量转换为字符数组,并返回。用法如: String s=”Who are you?” char z[]=s.toCharArray();

public static String valueOf( boolean b )

public static String valueOf( char c )

public static String valueOf( int i )

 public static String valueOf( long l )

 public static String valueOf( float f )

public static String valueOf( double d ) 以上6个方法可将boolean、char、int、long、float和double 6种类型的变量转换为String类的对象。

用法如: String r1=String.valueOf(true);(注:r1的值为”true”)

 String r2=String.valueOf(‘c’);(注:r2的值为”c”) float ff=3.1415926;

String r3=String.valueOf(ff); (注:r3的值为”3.1415926″)

2  StringBuffer 类

String类是字符串常量,是不可更改的常量。而StringBuffer是字符串变量,它的对象是可以扩充和修改的。 StringBuffer类的构造函数 l public StringBuffer() 创建一个空的StringBuffer类的对象。

 public StringBuffer( int length ) 创建一个length 的StringBuffer类的对象。 注意:如果参数length小于0,将触发NegativeArraySizeException异常。

public StringBuffer( String str ) 用一个已存在的字符串常量创建StringBuffer类的对象。

2.1   StringBuffer类的方法有:

 (1)public String toString() 转换为String类对象并返回。由于大多数类中关于显示的方法的参数多为String类的对象,所以经常要将StringBuffer类的对象转换为String类的对象,再将它的值显示出来。用法如: StringBuffer sb=new StringBuffer(“How are you?”); Label l1=new Label(sb.toString()); (注:声明一个标签对象l1,l1上的内容为How are you?)

(2)

 public StringBuffer append( boolean b )

 public StringBuffer append( char c )

public StringBuffer append( int i)

public StringBuffer append( long l )

public StringBuffer append( float f )

public StringBuffer append( double d )

以上6个方法可将boolean、char、int、long、float和double 6种类型的变量追加到StringBuffer类的对象的后面。

用法如:

double d=123.4567;

StringBuffer sb=new StringBuffer();

sb.append(true);

sb.append(‘c’).append(d).append(99);(注:sb的值为truec123.456799)

 (3)public StringBuffer append( String str ) 将字符串常量str追加到StringBuffer类的对象的后面。

 (4)public StringBuffer append( char str[] )将字符数组str追加到StringBuffer类的对象的后面。

(5)public StringBuffer append( char str[], int offset, int len )将字符数组str,从第offset个开始取len个字符,追加到StringBuffer类的对象的后面。

 (6)

public StringBuffer insert( int offset, boolean b )

 public StringBuffer insert( int offset, char c )

public StringBuffer insert( int offset, int i )

public StringBuffer insert( int offset, long l )

 public StringBuffer insert( int offset, float f )

 public StringBuffer insert( int offset, double d )

 public StringBuffer insert( int offset, String str )

public StringBuffer insert( int offset, char str[] ) 将boolean、char、int、long、float、double类型的变量、String类的对象或字符数组插入到StringBuffer类的对象中的第offset个位置。

用法如: StringBuffer sb=new StringBuffer(“abfg”); sb.insert(2,”cde”);(注:sb的值为abcdefg)

 (7)public int length()返回字符串变量的长度,用法与String类的length方法类似。

3  Math 类

 数学类包含了许多数学函数,如sin、cos、exp、abs等。

Math类是一个工具类,它在解决与数学有关的一些问题是有着非常重要的作用。

 这个类有两个静态属性:E和PI。E代表数学中的e 2.7182818,而PI代表派pi 3.1415926。

引用时,用法如:Math.E 和 Math.Pi。

这个类的方法有: abs方法用来求绝对值。

public static int abs( int a )

public static long abs( long a )

public static float abs( float a )

public static double abs( double a )

acos求反余弦函数。

public static native double acos( double a )

asin求反正弦函数。 public static native double asin( double a )

atan求反正切函数。public static native double atan( double a )

ceil返回 最小的 大于a的整数。

public static native double ceil( double a )

cos求余弦函数。

public static native double cos( double a )

exp求e的a次幂。public static native double exp( double a )

 floor返回 最大的 小于a的整数。public static native double floor( double a )

log返回lna。public static native double log( double a )

 pow求a的b次幂。public static native double pow( double a, double b )

sin求正弦函数。public static native double sin( double a )

sqrt求a的开平方。public static native double sqrt( double a )

tan求正切函数。 public static native double tan( double a )

public static synchronized double random() 返回0到1之间的随机数。

使用这些方法时,用法为Math.***** (*****为方法名)。

用法如: int a=Math.abs(124); int b=Math.floor(-5.2); double s=Math.sqrt(7);


版权声明:本文为brave1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/brave1/article/details/521055