英文:
Why do I need to specify the type of data?
问题
以下是您要翻译的内容:
从编码方式的绝对开端问候,我在谈论不到一周的时间(准确地说,不到4天)。
所以,这是我的问题。我创建了一个名为User
的类,然后给它一些变量(如果我写的方式非常随意且不像编程)。
public class User
{
String name;
short age;
int height;
然后我创建了一些构造函数。这是其中之一:
public User(String name,short age,int height) {
this.name=name;
this.age=age;
this.height=height;
System.out.println(name);
System.out.println("Age: "+age);
System.out.println("Height: "+height);
System.out.println();
}
随后,在main
方法中,我创建了一个类User
的对象,并指定了变量的值:
public static void main(String[] args) {
User user1=new User("Dan",20,190);
}
但是当我运行程序时,我收到一个错误,指出没有任何(String,int,int)类型的构造函数。与此同时,如果写上(short)
,也就是明确指定数据类型,程序就能正常运行。为什么Java将与short
所需的类型匹配的数字视为int
呢?
英文:
Greatings there from the absolutely beginning of the coding way, I'm talking, less than a week (than 4 days, if to be correct).
So, here's my problem. I made a class User
and then gave it some variables (sorry if the way I'm writing is very casual and not programming-like).
public class User
{
String name;
short age;
int height;
Then I created some constructors. That's one of them:
public User(String name,short age,int height) {
this.name=name;
this.age=age;
this.height=height;
System.out.println(name);
System.out.println("Age: "+age);
System.out.println("Height: "+height);
System.out.println();
}
Afterwards, in the main
method I made a subject of the class User
with indicated aomounts of variables:
public static void main(String[] args) {
User user1=new User("Dan",20,190);
}
But when I run the programm, I get an error saying that there arent any constructor of (String,int,int) type. Meanwhile, if write (short)
,that is, specify the datatype, programm works. Why do Java see the the number matching the type of amount which short
requires as int
?
答案1
得分: 1
Java的数字字面量是int
类型,所以20被视为一个int
,除非在某些初始化过程中,它不会被隐式转换为short
。
编译器会查找一个方法,该方法至少以一个int
作为第二个参数。您的定义最多接受short
(short
是int
的严格子集)。然后,编译器会拒绝未解析的调用。
当您编写(short)20
时,您命令编译器将32位的int
缩减为16位的short
值。这样做太危险了(某些int
值可以表示为short
),不能隐式地进行转换,因此您需要自己编写代码并强制进行转换(前提是您知道自己在做什么)。
英文:
Java number literals are at int
s, so 20 is an int
which is not converted to short implicitely (except in some initialisations).
The compiler is then looking for a method that takes at least an int as second parameter. Your definition accept at most shorts (short
s are strict subset of int
s). Then compiler rejects the call as unresolved.
When you write (short)20
then you order the compiler to shorter down the 32 bits int
to a 16 bits short
value. It is too dangerous (some ints values can be represented as shorts) to make it implicitly thus you need to write it by yourself and enforce the conversion (provided that you know what you are doing).
答案2
得分: 0
编译器错误提示您,age 不是 short
类型,除非您告诉它,否则它会自动将其视为 int
类型。
尝试这样做,这样您告诉编译器您的年龄是 short
类型:
public static void main(String[] args)
{
String name = "Dan";
short age = 20;
int height = 190;
User user1 = new User(name, age, height);
}
英文:
The compiler error is telling you that age is not of type short
, (it automatically takes it as int
unless you tell him otherwise)
Try this, this way you're telling the compiler your age is type short
public static void main(String[] args)
{
String name = "Dan";
short age = 20;
int height = 190;
User user1=new User(name,age,height);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论