错题整理
测试知识及错题整理
boolean类型的两个值
真:true
假:false
直接量
整型的直接量是:int类型
将一个int类型的直接量转换为long类型的直接量需要在后面加大写L[小写l]
char a1=1;//int类型直接量
byte a2=1;//int类型直接量
short a3=1;//int类型直接量
int a4=1;//int类型直接量
long a5=1;//int类型直接量
//均可,无编译错误
//int类型的直接量转换为long类型的直接量
long a6=1L;//1L为long类型直接量
一个int类型的变量,无法直接赋给比int范围小的数据类型[char、byte、short]
int d=4;
char d1=d; [1]
byte d2=d; [2]
short d3=d; [3]
[1]-[3]均存在编译错误,由于大类型赋值给小类型需要强制转换
浮点型的直接量是:double类型
将一个double类型的直接量转换为float类型的直接量需要在后面加大写F[小写f]
float b2=1.23; 存在编译错误
1.23属于double类型的直接量,float的范围比double小,无法直接定义。
double b1=1.23;
//float b2=1.23; //编译错误
float b2=1.23F; //在double类型直接量后面加加大写F[小写f],将其变为float类型的直接量。
错题
以下有误是:
double z=3.145;
float z1=1.2345;
boolean truth=true;
int z2=128;
三目运算 数据类型以大范围为准
boolean表达式?值1:值2
其中若值1与值2的数据类型不同,以数据类型范围大的为准
例如:5>4? 12 : 90.3;
此时值1为int类型,值2为double类型,则以double类型为准
5>4为true;选择值1,则输出结果为 double类型的12.0 ,而不是int类型的12
错题
以下输出结果为:
public static void main(String[] args){
int x=4;
System.out.println(x>4?99.9:9);
}
mian方法体的定义
main方法固定写法:
public static void main(String[] agrs){};
若不是String[] 则不是main方法,但是编译无误,只是运行是找不到main方法入口
错题
找出以下错误点
public static void main(String s){
System.out.println(“hello”);
}
for循环体
for(初始变量 ; boolean类型表达式 ; 变量变化)
boolean类型表达式可以直接是boolean类型的值true或false
错题
判断下列代码执行次数
public static void main(String[] agrs){
for(int i=0 ; true ; i++);
}
JAVA1995年发布,发布公司为Sun
变量与作用范围域
变量在哪个大括号{}中创建的,则作用范围即为哪个大括号{}中
当大括号结束,则该变量作用范围也消散
在大括号中同样的变量不能被声明两次
public static void mian(String[] args){
int num=100; [1]
for(int i=1;i>=0;i–){ [2]
int a=10; [3]
int num=10; [4]
}
System.out.println(num);
System.out.println(a); [5]
}
在外层大括号中最先声明了一个[1]num变量,在外层大括号中运行了一个for循环,for循环大括号中声明了三个变量[2] i;[3]a;[4]num;
[2] 与[3]a随着for循环的结束而消散; [4]与最先声明的重复,存在编译错误
[5]输出a变量,但a变量已随着for循环结束而消散,因此这里存在编译错误,由于a是一个为声明以及初始化的变量
变量需先声明,再初始化,才能使用
int a=30,b,c; 声明3个变量且a初始化值为30
b=a+10; a变量的值+10=40,赋给b
c++; //c未初始化,不能使用 编译错误
System,out,println©; //c未初始化,不能使用 编译错误
错题
以下运行情况为
public static void main(String[] agrs){
for(i=0;i<=10;i++)
if(i>7)
break;
System.out.println(i);
}
Boolean.valueOf()
Boolean.valueOf(String s);
在不区字符串分大小写的情况下,值若为"true",则返回boolean类型的true;否则返会boolean类型的false
String b1="true";
System.out.println(Boolean.valueOf(b1));//true
String b2="True";
System.out.println(Boolean.valueOf(b2));//true
String b3="True";
System.out.println(Boolean.valueOf(b3));//true
String b4="tRuE";
System.out.println(Boolean.valueOf(b4));//true
String b5="false";
System.out.println(Boolean.valueOf(b5));//false
String b6="1234";
System.out.println(Boolean.valueOf(b6));//false
Boolean.valueOf(boolean);
当boolean类型的值为true,则返回true;
当boolean类型的值为false,则返回false
boolean c1=true;
System.out.println(Boolean.valueOf(c1));//true
boolean c2=false;
System.out.println(Boolean.valueOf(c2));//false
System.out.println(Boolean.valueOf(5>4));//true
System.out.println(Boolean.valueOf(5<4));//false
错题
调用textIfA(),判断结果
public static void textIfA(){
if (testIfB(“True”)) { System.out.println(“True”); }
else{ System.out.println(“Not true”); }
}
public static boolean textIfB(String s){
return Boolean.valueOf(s);
}
数据类型转换
小范围数据类型转成大范围数据类型 系统自动转换
double a=4; //4为int类型的直接量,系统自动转换为double类型
int a1=5; double b=a1;
大范围数据类型转成小范围数据类型 手动强制转换
int b=(int)1.23; //1.23为double类型的直接量,(int)手动强制转换
long b2=89; int b3=(int)b2;
大范围数据类型转成小范围数据类型 可能存在溢出或精度损失
int c=(int)1.23; //1.23为double类型的直接量,(int)手动强制转换 结果 1 精度损失
int c1=1024 ; byte c2=(byte)c1 // 1024比byte数据类型范围要大,存在溢出 结果为0
数据溢出没有编译错误,但会存在强制转换后数据不准
数组声明后、未初始化的默认值
boolean[] a4=new boolean[9]; //默认值false
char[] a2=new char[9]; //默认值" "空格
int[] a1=new int[9]; //默认值0
byte[] a3=new byte[9]; //默认值0
short[] a4=new short[9]; //默认值0
long[] a5=new long[9]; //默认值0
double[] a3=new double[9]; //默认值0.0
float[] e3=new float[9]; //默认值0.0
String[] a5=new String[9]; //默认值null