String,StringBuffer ,基本數(shù)據(jù)類型的對象包裝類
e.g1:編寫一個程序來簡單了解String類的使用.程序一行一行地讀取從鍵盤上不停輸入的字符串,并打印顯示.直到輸入一行"bye"為止.
?class ReadLine{
???????? public static void main(String args[]){
?????????????????? byte[] buf? = new byte[1024];
?????????????????? int ch = 0;
?????? int pos = 0;
?????? String strInfo = null;
??????????????????? while(true){
?????????????????????????? try{
?????????????????????????????????? ch = System.in.read();
?????????????????????????? }catch(Exception e){
??????????????????????????????????? System.out.println(e.getMessage());
?????????????????????????? }
?????? switch(ch){
??????? case '\r':
???????? break;
??????? case '\n':
???????? strInfo = new String(buf,0,pos);//在buf中提取0至pos元素.
??????? if(strInfo.equals("bye"))
???????? return;
??????? else{
???????? System.out.println(strInfo);
???????? pos = 0;
???????? break;
??????? }
??????? default:
???????? buf[pos++] = (byte)ch;
?????? }
??
?????}
??????? }
?}
e.g2:如,要將字符串轉(zhuǎn)換成基本數(shù)據(jù)類型,幾乎都使用"Xxx包裝類.parseXxx"方式實現(xiàn)(一個例外是對于Boolean類,用的是getBoolean方法):要將包裝類轉(zhuǎn)換成基本數(shù)據(jù),幾乎都是"Xxx包裝類對象.xxxVaue"方式.
class TestInteger{
?public static void main(String[] args){
??int w = Integer.parseInt(args[0]);
??int h = new Integer(args[1]).intValue();
??for(int i = 0; i < w;i++){
???StringBuffer str = new StringBuffer();
???for(int j = 0 ; j < h;j++){
????str.append("*");
???}
???System.out.println(str.toString());
??}
?}
}