StringBuffer类支持的方法大部分与String类似,因为StringBuffer类在开发中可以提升代码的性能,所以使用较多。Java为了保证用户操作的适应性,在StringBuffer类中定义的方法名称大部分都与String一样。
在Java中,Runtime类表示运行时的操作类,是一个封装了JVM进程的类,每一个JVM都对应着一个Runtime类的实例,此实例由JVM运行时为其实例化。
import java.io.IOException;
public class HelloWorld {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
System.out.println(run.availableProcessors());
System.out.println(run.freeMemory());
System.out.println(run.maxMemory());
Process p = null;
try {
p = run.exec("ls -d");
} catch (IOException e) {
e.printStackTrace();
}
p.destroy();
System.out.println(p.exitValue());
}
}
System类是一些与系统相关属性和方法的集合。在System类中所有属性都是静态的,要想引用这些属性和方法,直接使用System类调用即可。
Math类是数学操作类,其提供了一系列的数学操作方法,包括求绝对值、三角函数等。因为在Math类中提供的方法都是静态的,所以直接由类名称调用即可。
public static void main(String[ ] args) {
String str=null;
str.concat("abc");
str.concat("def");
System.out.println(str);
}
A. Null
B. Abcdef
C. 编译错误
D. 运行时出现NullPointerException异常
public class Test2 {
public static void main(String args[]) {
String s1 = new String("bjsxt");
String s2 = new String("bjsxt");
if (s1 == s2)
System.out.println("s1 == s2");
if (s1.equals(s2))
System.out.println("s1.equals(s2)");
}
}
A. s1==s2
B. s1.equals(s2)
C. s1==s2 s1.equals(s2)
D. 以上都不对
public class TestStringBuffer {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
mb_operate(a, b);
System.out.println(a + "." + b);
}
static void mb_operate(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
}
}
A. A.B
B. A.A
C. AB.AB
D. AB.B