Java——数据类型运算和强制转换
整数类型和浮点数之间的运算
public class learn{
public static void main(String[] args){
float number1=45.56f;
int number2=152;
System.out.println("number1和number2的和等于:"+(number1+number2));
System.out.println("number2减去number1等于:"+(number2-number1));
}
}
布尔类型
public class learn{
public static void main(String[] args){
int a=2;
int b=5;
boolean result1=((a>b)&&(a!=b));
boolean result2=((a>b)||(a!=b));
System.out.println(result1);
System.out.println(result2);
boolean c=20>50?true:false; //三元运算符
System.out.println(c);
}
}
运算
public class learn{
public static void main(String[] args){
byte mybyte = 127;
int myint = 150;
float myfloat = 452.12f;
char mychar=10;
double mydouble = 45.226;
System.out.println("mybyte+myint="+(mybyte+myint));
System.out.println("mybyte+myfloat="+(mybyte+myfloat));
System.out.println("mybyte*myint="+mybyte*myint);
System.out.println("mybyte/myint="+mybyte/mychar);
}
}
public class learn{
public static void main(String[] args){
int a=(int)45.22;
long y=(long)456.6f; //强制类型转换
int b=(int)'d';
System.out.println(a);
System.out.println(y);
System.out.println(b);
}
}
- 点赞
- 收藏
- 关注作者
评论(0)