字符類型char和浮點類型float和布爾類型boolean的使用
對字符類型賦值使用單引號' ',
public class Welcome {
public static void main(String[] args) {
char ch;
ch='a';
System.out.println(ch);
}
}
輸出為a
字符類型還可以直接使用整數來賦值,因為java中對字符類型使用的是uncode字符。
public class Welcome {
public static void main(String[] args) {
char ch;
ch=98;
System.out.println(ch);
}
}
輸出為b
在java中小數常量被默認為double類型,如1.3默認為double類型。如果要聲明為float類型,需要在之后加f,如1.3f
public class Welcome {
public static void main(String[] args) {
float f;
f=1.3f;
System.out.println(f);
}
}
輸出為1.3
在java中boolean 布爾類型只有2個取值是true和false,不能賦值為1之類的整數。
public class Welcome {
public static void main(String[] args) {
boolean bool;
bool=true;
System.out.println(bool);
}
}
輸出為true
在做條件判斷的時候,不能再像c語言那樣使用if(1)或where(1)這樣的語句。
只能寫為if(true)或where(true)這樣。