String s;
String是一个类,String的变量是对象的管理者而非所有者
String s=new String("a string");
用+连接两个字符串
“hello”+“world”->“helloworld”
+一边是字符串而另一边不是时,会将琳娜过一遍表达为字符串然后连接
"I’m "+18->“I’m 18”
输入字符串
in.next();读入一个单词,单词的标志是空格(空格包括空格、tab和换行)
in.nextLine();读入一整行
if(input=="bye"){...} //比较是否同一个
if(input.equals("bye")){...} //比较内容是否相同
s1.compareTo(s2);
比较大小,s1比s2大输出,s2比s1大输出-1
获得str1的长度str1.length();
访问String里面的字符str1.charAt(index)
返回在index上的单个字符
index的范围为0到length()-1
得到子串
s.substring(n)
得到从n号位置到末尾的全部内容
s.substring(b,e)
得到从b号位置到e号位置之前的内容,注意是左闭右开
所有字符串都是不可变的,所有操作都产生新字符串
switch-case中也可以使用字符串
Math类
Math.abs求绝对值
Math.round四舍五入取整
Math.random取0-1随机数
Math.pow求幂