本文目錄一覽:
java中的輸出string字元串,是亂碼
同學,這個不是亂碼。
數組本身是沒有toString()方法的。
你這裡有個默認的調用 Object.toString()
Object中的toString()方法,是將傳入的參數的類型名和摘要(字元串的hashcode的十六進位編碼)返回。
也就是說,你直接對數組使用了toString()方法,就會得到 一個Ljava.lang.String;@175d6ab
其中,Ljava.lang.String 是指數據是String類型的,175d6ab是摘要了
你要想得到你要的類型,需要使用循環的方法來輸出數組中的每一個值。
下面是jdk中關於toString()方法的注釋:
/**
* Returns a string representation of the object. In general, the
* {@code toString} method returns a string that
* “textually represents” this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* p
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}’, and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* blockquote
* pre
* getClass().getName() + ‘@’ + Integer.toHexString(hashCode())
* /pre/blockquote
*
* @return a string representation of the object.
*/
Object中的toString()方法實現:
public String toString() {
return getClass().getName() + “@” + Integer.toHexString(hashCode());
}
java 字元串輸出
system.out.println(“輸出內容”) ; 輸出內容並換行
system.out.print(“輸出內容”) ; 輸出內容不換行
System 是一個類,out是一個static PrintStream 對象。由於它是「靜態」的,所以不需要我們創建任何東西,所以只需直接用它即可。
println()的意思是「把我給你的東西列印到控制台,並用一個新行結束」。所以在任何Java 程序中,一旦要把某些內容列印到控制台,就可條件反射地寫上System.out.println(“內容”)。
Java的常用輸入輸出語句?
常用的輸入語句是:
輸入字元串:new Scanner(System.in).next();
輸入整數:new Scanner(System.in).nextInt();
輸入小數:new Scanner(System.in).nextDouble();
常用的輸出語句:
換行輸出: System.out.println(變數或字元串);
非換行輸出: System.out.print(變數或字元串);
換行輸出錯誤提示(默認是紅字):System.err.println(變數或字元串);
不換行輸出錯誤提示(默認是紅字): System.err.print(變數或字元串));
JAVA按格式輸出字元串
在Java編程中格式化字元串,用String類的靜態方法String.format():
format(Locale l, String format, Object… args)
//使用指定的語言環境、格式字元串和參數返回一個格式化字元串。
format(String format, Object… args)
//使用指定的格式字元串和參數返回一個格式化字元串。
舉幾個這個方法實用的例子(注釋是輸出結果):
//案例1
long now = System.currentTimeMillis();
String s = String.format(“%tR”,now); //輸出當前時間的小時和分鐘
// 格式化輸出結果”09:22″
//案例2
Date d = new Date(now);
s = String.format(“%tD”,d); //輸出當前時間的month/day/year
// 格式化輸出結果”11/05/15″
java中jtextfield中如何輸出字元串操作
JTextField 是一個輕量級組件,它允許編輯單行文本。
輸出字元串,可以用它的父類方法:setText(String t) 來實現。只要把需顯示的字元串作實參調用這個方法就可以。比如:
String str=”你好”;
text.setText(str);
名為text的JTextField組件的內容就會顯示為「你好」。
另外,要取得其內容,則要用到getText()方法,該方法返回一個字元串,其內容為組件的內容。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/193896.html