字符串转化

byte Byte.parseByte()

short Short.parseShort()

int Integer.parseInteger()

float Float.parseFloat()

double Double.parseDouble()

boolean Boolean.parseBoolean()

char Character.parseCharacter()

样例代码

String s1="123";
String s2="456";
int a=Integer.parseInt(s1); //字符串转化为数字并相加
int b=Integer.parseInt(s2);
System.out.print(a+b);

输出结果:579

StringBuffer buf=new StringBuffer("Hello World!"); //字符串连接
buf.append("WeCome!");
System.out.print(buf.toString());

输出结果:Hello world! WeCome!

12-1

import java.util.StringTokenizer;
String S="Hello,class,Welcome to Java!Java is very important.";
String del=" ,.!";
StringTokenizer tok=new StringTokenizer(S,del); //统计字符串词数,并输出
System.out.println(tok.countTokens());
while(tok.hasMoreElements())
System.out.println(tok.nextToken());

输出结果:

9 Hello class Welcome to Java Java is very important

12-2

package classtoday;

public class classto1 {
public static void main(String[] args) {
Car ca=new Car();
Motorbike mo=new Motorbike();
ca.getCar();
mo.getCar();
}

}

abstract class Vehicle{
abstract void getCar(); //抽象方法
}

class Car extends Vehicle{
void getCar(){
System.out.println("四轮车"); // 实现抽象方法
}
}

class Motorbike extends Vehicle{
void getCar(){
System.out.println("双轮车"); // 实现抽象方法
}
}

输出结果:

四轮车 双轮车