在Java中,可以通過以下方式定義字符串變量并賦值:
String str = "Hello, World!";
new
關鍵字實例化一個字符串對象:String str = new String("Hello, World!");
StringBuilder
或StringBuffer
類創(chuàng)建可變的字符串對象:StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(", ");
sb.append("World!");
String str = sb.toString();