为什么我需要指定数据类型?

huangapple go评论68阅读模式
英文:

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作为第二个参数。您的定义最多接受shortshortint的严格子集)。然后,编译器会拒绝未解析的调用。

当您编写(short)20时,您命令编译器将32位的int缩减为16位的short值。这样做太危险了(某些int值可以表示为short),不能隐式地进行转换,因此您需要自己编写代码并强制进行转换(前提是您知道自己在做什么)。

英文:

Java number literals are at ints, 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 (shorts are strict subset of ints). 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);
}

huangapple
  • 本文由 发表于 2020年9月3日 17:57:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63721219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定