六、常用类

1. String 类
java.lang.String 类代表不可变的字符序列。
“xxxxx” 为该类的一个对象。
String类的常见构造方法:
String(String original)
//创建一个String对象为original的拷贝。
String(char[] value)
//用一个字符数组创建一个String对象
String(char[] value,int offset,int count)
//用一个字符数组从offset项开始的count个字符序列创建一个String对象


常用方法:

public char charAt(int index)
//返回字符串中第index个字符。
public int length()
//返回字符串的长度。
public int indexOf(String str)
//返回字符串中出现str的第一个位置
public int indexOf(String str,int fromIndex)
//返回字符串中从fromIndex开始出现str的第一个位置
public boolean equalsIgnoreCase(String another)
//比较字符串与another是否一样(忽略大小写)
public String replace(char oldChar,char newChar)
//在字符串中用newChar字符替换oldChar字符
public boolean startsWith(String prefix)
//判断字符串是否以prefix字符串开头
public boolean endsWith(String suffix)
//判断字符串是否以prefix字符串结尾
public String toUpperCase()
//返回一个字符串为该字符串的大写形式
public String toLowerCase()
//返回一个字符串为该字符串的小写形式
public String substring(int beginIndex)
//返回该字符串从beginIndex开始到结尾的子字符串
public String substring(int beginIndex,int endIndex)
//返回该字符串从beginIndex开始到endIndex结尾的子字符串
public String trim()
//返回将该字符串去掉开头和结尾空格后的字符串
//静态重载方法
public static String valueOf(…)
//可以将基本类型数据转换为字符串;例如:
public static String valueOf(double d)
public static String valueOf(int i)
… … …
b + “”;
//方法 public String[] split(String regex)可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组。





2. StringBuffer与StringBuilder 类
StringBuffer(StringBuilder )和String类似,但StringBuffer可以对其字符串进行改变。
String, StringBuffer, StingBuilder 区别:
1.如果字符串内容需要频繁变化—》StringBuffer, StingBuilder
2.如果是单线程编程—》优先用StingBuilder
3.多线程编程,并且有多个线程操作同一个对象可能。--》StringBuffer

public class Test {
public static void main(String[] args) {
String s = "Mircosoft";
char[] a = {'a','b','c'};
StringBuffer sb1 = new StringBuffer(s);
sb1.append('/').append("IBM")
.append('/').append("Sun");
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("数字");
for(int i = 0;i<=9;i++){sb2.append(i);}
System.out.println(sb2);
sb2.delete(8,sb2.length()).insert(0,a);
System.out.println(sb2);
System.out.println(sb2.reverse());
}
}

3. 基本数据类型包装类(Byte / Short / Character / Integer / Long / Float / Double / Boolean)
包装类常见方法:
以下方法以java.lang.Integer为例
 public static final int MAX_VALUE //最大的int型数(231-1)
public static final int MIN_VALUE //最小的int型数(-231)
public long longValue() //返回封装数据的long型值
public double doubleValue() //返回封装数据的double型值
public int intValue() //返回封装数据的int型值
public static int parseInt(String s)
throws NumberFormatException
//将字符串解析成int型数据,返回该数据
public static Integer valueOf(String s)
throws NumberFormatException
//返回Integer对象,其中封装的整型数据为字符串s所表示。


4. Math类
java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型。
abs// 绝对值
acos,asin,atan,cos,sin,tan
sqrt //平方根
pow(double a, double b) //a的b次幂
log //自然对数
exp //e为底指数
max(double a, double b)
min(double a, double b)
random() //返回 0.0 到 1.0 的随机数
long round(double a) //double型的数据a转换为long型(四舍五入)
toDegrees(double angrad) //弧度->角度
toRadians(double angdeg) //角度->弧度

public class Test {
public static void main(String[] args) {
double a = Math.random();
double b = Math.random();
System.out.println(Math.sqrt(a*a+b*b));
System.out.println(Math.pow(a,8));
System.out.println(Math.round(b));
System.out.println(Math.log(Math.pow(Math.E,15)));
double d = 60.0, r = Math.PI/4;
System.out.println(Math.toRadians(d));
System.out.println(Math.toDegrees(r));
}
}

5. File 类
java.io.File类代表系统文件名(路径或文件名)。
File类的常见构造方法:
public File(String pathname)
以pathname为路径创建File对象,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
File的静态属性String separator存储了当前系统的路径分隔符。

通过File对象可以访问文件的属性。
public boolean canRead()
public boolean canWrite()
public boolean exists()
public boolean isDirectory()
public boolean isFile()
public boolean isHidden()
public long lastModified()
public long length()
public String getName()
public String getPath()

通过File对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)。
    public boolean createNewFile()throws IOException
public boolean delete()
public boolean mkdir()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值