一、字符串類型:String
String類型變量的使用
- String屬于引用數(shù)據(jù)類型,翻譯為字符串
- 聲明String類型變量時朱嘴,使用一對""
class StringTest{
public static void main(String[] args){
String s1 = "Hello World!";
System.out.println(s1);
String s2 = "a";
String s3 = ""; // " "內(nèi)放多少都字符都可以
char c = ‘ ’莫湘; //但定義char型變量,引號內(nèi)無東西不通過
}
}
- String可以和八中基本數(shù)據(jù)類型變量做運算尤蒿,且運算只能連接運算
- 運算結果仍然是String類型
class StringTest{
public static void main(String[] args){
int number = 1001;
String numberStr = "學號:";
String info = numberStr + number; // 連接運算
boolean b1 = true;
String info1 = info + b1;
System.out.println(info1); //info1仍然是String類型
//如何判段是相加還是連接運算(只要前面出現(xiàn)String后面的+都為連接)
char c = ' a';
int num = 10;
String str = "hello";
System.out.println(c + num + str); //107hello
System.out.println(c + str + num); //ahello10
System.out.println(c +( str + num)); //a10hello
System.out.println((c + num) + str); //107hello
System.out.println( str + num +c ); //hello10a
}
}
二、運算符
- 算術運算符
算術運算符
注:取余運算結果的符號與被余數(shù)相同
- 賦值運算符
賦值運算符
//開發(fā)中幅垮,實現(xiàn)變量+2的操作方法:
int num = 10 优质;
num = num + 2;//方法一
num += 2军洼; //方法二(推薦)
short s1 = 10;
s1 = s1 + 2; //編譯失敗
s1 += 2 巩螃;// 不會改變變量本身的數(shù)據(jù)類型
- 比較運算符(關系運算符)
比較運算符
比較還是賦值舉例
- 邏輯運算符
邏輯運算符
- 位運算符
位運算符
位運算符
- 三元運算符
if-else
- 條件表達式的結果為boolean類型
- 表達式一和表達式二統(tǒng)一會一個類型
class SanYunTest{
public static void main(String[] args){
//獲取兩個數(shù)中較大值
int m = 12;
int n = 5;
int max = (m > n)? m:n;
System.out.println( max );
}
}
- 如果既可以使用三元運算符又可以使用if-else語句,優(yōu)先使用三元運算符