自动类型转换(隐式转换)的逆过程,将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符(()),但可能造成精度降低或溢出,格外要注意。
通常,字符串不能直接转换为基本类型,但通过基本类型对应的包装类则可以实现把字符串转换成基本类型。如: String a = “43”; int i = Integer.parseInt(a);
boolean类型不可以转换为其它的数据类型。
System.out.println("a == b = " + (a == b) );//a == b = false
System.out.println("a != b = " + (a != b) );//a != b = public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
System.out.println("a > b = " + (a > b) );//a > b = false
System.out.println("a < b = " + (a < b) );//a < b = public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
System.out.println("b >= a = " + (b >= a) );//b >= a = public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
System.out.println("b <= a = " + (b <= a) );//b <= a = false
/*
* 区分好==和=的区别:
* */
boolean b1 = false;
if(b1 == public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}){ //false == public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
} 结果比较为false
System.out.println("结果为真");
}else{
System.out.println("结果为假");//输出
}
boolean b2 = false;
if(b2 = public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}){ //把public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}赋值给b2
System.out.println("结果为真");
}else{
System.out.println("结果为假");//输出
}
}
}
复制代码
执行上面示例代码,得到以下结果:
a = 0011 1100
b = 0000 1101
-----------------
// 1.参加运算的两个数据,按照二进制进行按位与的运算。
// 运算规则:0&0=0; 0&1=0; 1&0=0; 1&1=1;
// 即:两位同时为“1”,结果才为“1”,否则为0。
a&b = 0000 1100
// 2.参加运算的两个对象,按二进制位进行“或”运算。
// 运算规则:0|0=0; 0|1=1; 1|0=1; 1|1=1;
// 即 :参加运算的两个对象只要有一个为1,其值为1。
a|b = 0011 1101
// 3.参加运算的两个数据,按二进制位进行“异或”运算。
// 运算规则:0^0=0; 0^1=1; 1^0=1; 1^1=0;
// 即:参加运算的两个对象,如果两个相应位为“异”(值不同),则该位结果为1,否则为 0。
a^b = 0011 0001
~a = 1100 0011
复制代码
如果是对负数移位,则是对补码移位
复制代码
7.4 逻辑运算符
下表列出了逻辑运算符 - 假设布尔变量A的值为:public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
},变量B 的值为:false,则 - <img> 以下简单的示例程序演示了逻辑运算符。 将以下Java程序复制并粘贴到Test.java文件中,然后编译并运行该程序 -