javase--3常用类(java.lang)

常用类(java.lang)

一、String

1.不可变的字符序列

2.判断字符串是否相等:常量储存在data sagment,值相同的变量存储一份,new出的东西存在堆内存中,值相同的变量也分开存储

public class TestString {
public static void main(String args[]) {
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2); //true
String s3 = new String("hello");
String s4 = new String("hello");
System.out.println(s3 == s4); //false
System.out.println(s3.equals(s4));//true
char[] c = {'s','u','n','  ','j','a','v','a'};
String s5 = new String(c);
String s6 = new String(c,4,4);
System.out.println(s5);//sun java
System.out.println(s6);//java
}
}

3.常用方法

public class TestString2 {
public static void main(String args[]) {
String s1 = "sun java";
String s2 = "Sun Java";
System.out.println(s1.charAt(1)); //u
System.out.println(s2.length());//8
System.out.println(s1.indexOf("java"));//4
System.out.println(s1.indexOf("Java"));//-1
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true

String s = "我是程序员,我在学java";
String sd = s.replace('我','你');
System.out.println(sd);//你是程序员,你在学java
}
}

public class TestString3 {
public static void main(String args[]) {
String s1 = "Welcome to java world";
String s2 = "     sun  java    ";
System.out.println(s1.startsWith("welcome"));//false
System.out.println(s1.startsWith("Welcome"));//true
System.out.println(s2.endsWith("world")); //true
System.out.println(s1.toLowerCase());//welcome to java world
System.out.println(s1.toUpperCase());//WELCOME TO JAVA WORLD
System.out.println(s2.trim());//sun  java
}
}

public class TestString4 {
public static void main(String args[]) {
int num = 12345;
String snum = String.valueOf(num);
System.out.println(snum+"是"+snum.length()+"位数");//12345是5位数
String woman = "Mary,F,2010";
String w[] = woman.split(",");
for(int i=0; i<w.length; i++) {
System.out.print(w[i]+“”);//Mary    F    2010
}
}
}

二、StringBuffer

1.可变的字符序列

当对字符进行增加、截取的时候,String类型要重新在堆中开辟内存,而StringBuffer直接在原变量的内存中进行更改

2.常用方法

public class TestString7 {
public static void main(String args[]) {
String s1 = "Microsoft";
char[] a = {'a','b','c'};
StringBuffer sb = new StringBuffer(s1);
sb.append("/").append("IBM").append("/").append("sun");
System.out.println(sb);// Microsoft/IBM/sun
StringBuffer sb2 = new StringBuffer("数字");
for(int i=0; i<=9; i++) {
sb2.append(i);
}
System.out.println(sb2);//数字0123456789
sb2.delete(8,sb.length()).insert(0,a);
System.out.println(sb2);//abc数字012345
System.out.println(sb2.reverse());//543210字数cba
}
}

三、基础数据类型的包装类

基础数据类型存放在栈内存中,其包装类存在与堆内存中

1.基本方法

以Integer为例:

valueOf( ):将String类型转换为Integer

parseInt( ):将String类型转换为int

intValue( ):将Integer转换为int

new Integer(int)或valueOf( ):将int转换为Integer

toString( ):将Integer转换为String

toString(int):将int转换为String

例:

public class TestObject1 {
public static void main(String args[]) {
Integer i = new Integer(100);
Double d = new Double("123.456");
int j = i.intValue() + d.intValue();
float f = i.floatValue() + d.floatValue();
System.out.println(j);
System.out.println(f);
double pi = Double.parseDouble("3.1415926");
double r = Double.valueOf("2.0").doubleValue();
double s = pi*r*r;
System.out.println(s);
try {
int k = Integer.parseInt("1.25");
} catch(NumberFormatException ex) {
System.out.println("数据格式不对!");
}
System.out.println(Integer.toBinaryString(123)+"B");
System.out.println(Integer.toHexString(123)+"H");
System.out.println(Integer.toOctalString(123)+"O");
}
}

四、Math

基本方法

public class TestMath {
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)));//e的15次方,在求以10为底的导数
double d = 60.0,r = Math.PI / 4;
System.out.println(Math.toRadians(d)); //将角度转换为弧度
System.out.println(Math.toDegrees(r));//将弧度转换为角度
}
}

五、File

windows下为反斜杠\,linux下为正斜杠/,通用正斜杠

1.基本方法

import java.io.*;


public class TestFile {
public static void main(String args[]) {
String separator = File.separator;
String filename = "myfile.txt";
String directory = "mydire1" + separator + "mydire2";
File file = new File(directory,filename);
if(file.exists()) {
System.out.println("文件名:" + file.getAbsoluteFile());
System.out.println("文件大小:" + file.length());

} else {
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
}

注意:在求路径时,最佳为上述方法,也可用正斜杠。但在用反斜杠时,要用\\,因\为转义字符的标识。

    生成的文件存在与class文件的同一级,但当class存在于包中时,存在于与包同一级。

六、枚举类(java.lang.Enum类)

public class TestEnum {
public enum MyColor { red, yellow, green};
public static void main(String args[]) {
MyColor m = MyColor.red;
switch(m) {
case red:
System.out.println("red");
break;
case yellow:
System.out.println("yellow");
break;
case green:
System.out.println("green");
break;
default:
System.out.println("default");
}
System.out.println(m);
}
}

用enum定义的是类型,再用类型定义变量。注意:C中枚举变量可用数值代表,但java不可用数值代表,只能用类型.变量代表。相当于类的静态变量。

猜你喜欢

转载自blog.csdn.net/hx_uestc/article/details/7214843