tips.Print的問題,tips.Print問題

來源:互聯網
上載者:User

tips.Print的問題,tips.Print問題

《編程導論(Java)》中是tips.Print;設計模式中是tool..Print。

package tips;import java.io.PrintStream;/** * 到處都是System.out.println().如果一個常式中使用它們較多,請使用本類。 * 1.2.1類體結構,練習要求閱讀本類。 * @see java.io.PrintStream * @author yqj2065 * @version 0.1 */public class Print{    public static void pln(Object x){        System.out.println(x);    }     public static void pln(){        System.out.println();    }    public static void p(Object x){        System.out.print(x);    }    /**     * 使用指定格式字串和參數,列印格式化的字串。     */    public static PrintStream pf(String format, Object... args){        return System.out.printf(format,args);            }    public static void pfln(String format, Object... args){        System.out.printf(format,args). println();           }    }
一直用得好好的,但是沒有重載System.out.println(char[]),於是出了問題。

今天看一個貼子,上面有Java Puzzlers的第12個謎題

    public static void main(String[] args) {        String letters = "ABC";        char[] numbers = {'1', '2', '3'};        pln("1)" + letters + " easy as " + numbers);        p("2)" + letters + " easy as ");        System.out.println(numbers); // println(char[])        System.out.println(new char[]{'1', '2', '3'});        pln(new char[]{'1', '2', '3'});    }
平時沒有用過println(char[]),所以輸出

1)ABC easy as [C@53b931e1
2)ABC easy as 123

覺得有點神奇。

    public void println(Object x) {          String s = String.valueOf(x);          synchronized (this) {              print(s);              newLine();          }      }
這個比較清楚——我叫輸出為引用的“大概模樣”

 print(char[] s)  Prints an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

所以

        char[] cs = {'1', '2', '3'};
        System.out.println(cs);

列印:123

關鍵是,我在tips.Print中沒有重載System.out.println(char[])!看不見上面的輸出。

唯一的好處是:

        char[] cs = null;
        pln(cs);
        System.out.println(cs);

pln(Object)不怕null,而System.out.print(char[] s) 會拋出NullPointerException(java.io.Writer.write())。


記錄一下。懶得改tips.Print了。


VB問題:對於print方法,空print語句的作用是什?

[對象.]print "dsdsdsdsdf"就是在對象上輸出字串“dsdsdsdsdf”,對象省略時一般是當前表單,也就是你的代碼所在表單。
空的print語句是print""這樣的吧,意思就是輸出Null 字元串,這個字串佔用一行,當你寫下一個print語句時,輸出的內容就在剛才Null 字元串的下面一行顯示,所以一個空的print語句可以理解為換行,相當於chr(13) & chr(10)的作用
 
一個簡單c問題:print與printf的不同

print--是函數,可以返回一個值,只能有一個參數。用起來最簡單

printf--函數,把文字格式化以後輸出,直接調用系統調用進行IO的,他是非緩衝的。
如:
$name="hunte";
$age=25;
printf("my name is %s, age %d", $name, $age);

sprintf--跟printf相似,但不列印,而是返回格式化後的文字,其他的與printf一樣。
如:
char sql[256];
sprintf(sql,"select * from table where no = '%s'",bankno);
它的功能只是把""裡面的語句賦給了變數sql。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.