1.浮点数——默认数据类型为double型
public class TestDemo{
public static void main(String[] args) {
double num = 10.2; //10.2是一个小数所以属于double型
System.out.println(num);
System.out.println(num * 2);
}
}
10.2
20.4
2.浮点数使用float型
public class TestDemo{
public static void main(String[] args) {
float f1 = 10.2f; //小数都是double型,需要强制转换成float型
float f2 = (float)10.2;
System.out.println(f1 * f2);
}
}
104.03999
3.字符型
public class TestDemo{
public static void main(String[] args) {
// 整型和字符型的转化
char c = 'A';
int num = c;
System.out.println(c);
System.out.println(num);
}
}
A
65
4.布尔型:保存TRUE和FALSE两类数据
public class TestDemo{
public static void main(String[] args) {
boolean sign = false;
if(!sign)
{
System.out.println("Hello world!");
}
}
}
Hello world!
5.字符串类型----引用数据类型,属于一个类,在java中,类的每一个首字母都是大写
// 字符串的操作
public class TestDemo{
public static void main(String[] args) {
String str = "Hello world!";
System.out.println(str);
System.out.println("Hello world!");
}
}
Hello world!
Hello world!
// 字符串的连接
public class TestDemo{
public static void main(String[] args) {
String str = "Hello world!";
str = str + " Java is much easier to learn than C++";
str += "!!!";
System.out.println(str);
}
}
Hello world! Java is much easier to learn than C++!!!
注:“+”在数学运算和字符串连接中需要考虑优先级问题
// “+”在字符串连接和数学运算中的优先级关系
public class TestDemo{
public static void main(String[] args) {
int numA = 100;
double numB = 99.0;
String str1 = "字符串连接优先: " + numA + numB;
String str2 = "数学运算优先: " + (numA + numB);
System.out.println(str1);
System.out.println(str2);
}
}
字符串连接优先: 10099.0
数学运算优先: 199.0