Integer Number too large in java int for the value 08 ,但是当我只传递 8 或 11,12,13 时就没问题了
Integer Number too large in java int for the value 08 , but when I pass just 8 or 11,12,13 it is just fine
Employee("Helen",16000,1965,08,03),编译器抛出整数对于值太大的错误:08,但编译器对值 11 或 8 没有问题。
import java.util.*;
class Employee{
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s,int year, int month, int day){
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);
hireDay = calendar.getTime();
}
public String getName(){
return name;
}
public double getSalary(){
return salary;
}
public Date getHireDay(){
return hireDay;
}
public void raiseSalary(double byPercent){
double raise = salary * byPercent / 100;
salary += raise;
}
}
public class EmployeeTest{
public static void main(String[] args){
Employee[] staff = new Employee[3];
staff[0] = new Employee("Jack",15000,1992,03,07);
staff[1] = new Employee("James",18000,1987,05,02);
>> staff[2] = new Employee("Helen",16000,1965,08,03);
// 这是 problem.When 我传递 08 当我传递 08 或它找到的某个两位数时它抛出错误
}
Output :
[shadow@localhost java]$ javac EmployeeTest.java
EmployeeTest.java:37: error: integer number too large: 08
staff[2] = new Employee("Helen",16000,1965,08,03);
^
1 error
[shadow@localhost java]$
这是因为,在 Java 中,以 0
开头的数字(如您的 08
)被视为八进制(基数 8)。而且八进制没有8。
(根据定义,八进制只使用数字 0-7)
作为实验,您可以尝试 07
或 011
并查看它们是否有效,或者尝试 08
或 09
并查看它们是否无效.
Employee("Helen",16000,1965,08,03),编译器抛出整数对于值太大的错误:08,但编译器对值 11 或 8 没有问题。
import java.util.*;
class Employee{
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s,int year, int month, int day){
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);
hireDay = calendar.getTime();
}
public String getName(){
return name;
}
public double getSalary(){
return salary;
}
public Date getHireDay(){
return hireDay;
}
public void raiseSalary(double byPercent){
double raise = salary * byPercent / 100;
salary += raise;
}
}
public class EmployeeTest{
public static void main(String[] args){
Employee[] staff = new Employee[3];
staff[0] = new Employee("Jack",15000,1992,03,07);
staff[1] = new Employee("James",18000,1987,05,02);
>> staff[2] = new Employee("Helen",16000,1965,08,03);
// 这是 problem.When 我传递 08 当我传递 08 或它找到的某个两位数时它抛出错误 }
Output :
[shadow@localhost java]$ javac EmployeeTest.java
EmployeeTest.java:37: error: integer number too large: 08
staff[2] = new Employee("Helen",16000,1965,08,03);
^
1 error
[shadow@localhost java]$
这是因为,在 Java 中,以 0
开头的数字(如您的 08
)被视为八进制(基数 8)。而且八进制没有8。
(根据定义,八进制只使用数字 0-7)
作为实验,您可以尝试 07
或 011
并查看它们是否有效,或者尝试 08
或 09
并查看它们是否无效.