public class test018
{
public static void main(String[] args)
{
int x = 50;
float y = x; //隐式转换,int 精度小于 float ,由系统自动完成
// 顺序参照 byte < short < int < long < float < double
System.out.println(y);
byte myByte = 127;
int myInt = 150;
float myFloat = 452.12f;
char myChar = 10;
double myDouble = 45.46546;
System.out.println("byte + float = " + (myByte + myFloat)); //float
System.out.println("byte + int = " + (myByte + myInt)); // int
System.out.println("byte + char = " + (myByte + myChar)); //int
System.out.println("char + double=" + (myChar + myDouble)); //double
//下面是强制转换
double a = 45.23;
int b = (int)a; //丢失精度
double c = 456.6;
long d = (long)c; //丢失精度
char e = 'a';
int f = (int)e; //
System.out.println(b);
System.out.println(d);
System.out.println(f);
}
}
test019.java
public class test019
{
public static void main(String[] args)
{
int x = 20;
{
int y = 40; // 花括号表示符合语句,在花括号内部声明的变量,仅在其内部有用,即限定了作用域
System.out.println("y=" + y);
int z = 245;
boolean b;
{
b = y > z;
System.out.println("b = " + b);
System.out.println("x= " + x);
}
}
String word = "hello world";
System.out.println(word);
}
}
test020.java
public class test020
{
public static void main(String[] args)
{
//if else 条件语句
//语句序列中只有一条语句时,写法如下,但是不建议这样写,没有可读性
int a = 100;
if(a== 100)
System.out.println("a = " + a);
//语句序列省略时,可以末尾加分号,代替花括号,依然不建议这样写,可读性差
boolean b = false;
if(b);
boolean c = false;
if(c) {}
//
int x = 45;
int y = 12;
if(x > y)
{
System.out.println("变量x大于变量y");
}
else
{
System.out.println("变量x小于变量y");
}
//
int math = 95;
int english = 56;
if(math >= 60)
{
System.out.println("数学及格了");
}
else
{
System.out.println("数学不及格");
}
if(english > 59)
{
System.out.println("英语及格了");
}
else
{
System.out.println("英语不及格");
}
//三元运算符替代法
int a1 = 5;
int b1 = 0;
if(a1 > 0)
{
b1 = a1;
}
else
{
b1 = -a1;
}
int a2 = 5;
int b2 = 0;
b2 = a2 > 0 ? a2:-a2;
System.out.println("b1 = " + b1);
System.out.println("b2 = " + b2);
}
}