fenr9

导航

Java学习--基本数据类型的定义和运算

例1:

public class DataDemo05{

public static void main(String args[]){
char ch1 = '\"' ; // 表示的是一个"
char ch2 = '\\' ; // 表示的是一个\
System.out.println("ch1 = " + ch1) ;
System.out.println("ch2 = " + ch2) ;
System.out.println("\"Hello World!\"") ;
}
};

 

运行结果:

ch1 = "
ch2 = \
"Hello World!"

 

例2:注意:3.0后的"f“不能少

public class DataDemo06{
public static void main(String args[]){
float num = 3.0f ; // 定义一个浮点型变量
System.out.println("两个小数相乘:" + num * num) ;
}
};

运行结果:

两个小数相乘:9.0

 例3:定义布尔变量

public class DataDemo07{
public static void main(String args[]){
boolean flag = true ; // 定义布尔型变量
System.out.println("flag = " + flag) ; // 打印输出
}
};

运行结果:

flag = true

 

例4:

public class DataDemo08{
public static void main(String args[]){
int x = 30 ; // 定义整型变量
float y = 22.19f ; // 定义浮点型变量
System.out.println("x / y = " + (x / y)) ;
System.out.println("10 / 3.5 = " + (10 / 3.5)) ;
System.out.println("10 / 3 = " + (10 / 3)) ;
}
};

运行结果:

x / y = 1.3519603
10 / 3.5 = 2.857142857142857
10 / 3 = 3

 

例5

public class DataDemo09{
public static void main(String args[]){
String str = "lixinghua" ; // 定义字符串变量
int x = 30 ;
str = str + x ; // 修改str的内容并将内容重新给str变量
System.out.println("str = " + str) ;
}
};

运行结果:

str = lixinghua30

 

例5

public class DataDemo10{
public static void main(String args[]){
int i = 1 ; // 定义整型变量
int j = 2 ; // 定义整型变量
System.out.println("1 + 2 = " + 1 + 2) ;
System.out.println("1 + 2 = " + (1 + 2)) ;
}
};

运行结果:

1+2=12

1+2=3

 

例6:

public class DataDemo11{
public static void main(String args[]){
float f = 30.3f ; // 浮点型
int x = (int) f; // 强制类型转换
System.out.println("x = " + x) ;
System.out.println("10 / 3 = " + ((float)10 / 3)) ; // 执行强制转换

}
};

运行结果:

x = 30
10 / 3 = 3.3333333

 

posted on 2016-04-08 11:06  fenr9  阅读(160)  评论(0编辑  收藏  举报