简体   繁体   English

使用不同的数据类型

[英]Using Different Data types

public class UnIntentionalObjectCreation {

  private static Byte sumOfIntegerUptoN(Byte N) {
    Byte sum = 0;
    for (Byte i = 0; i < N; i++) {
        sum += i;
    }
    System.out.println(sum);
    return sum;
  }

  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    sumOfIntegerUptoN(10);
    long end = System.currentTimeMillis();
    System.out.println((end - start));

  }
}

Error in 6 line :- Inconvertible type found Error in 14 line:- UnIntentionalObjectCreation cannot be applined to (int) 6行错误:-找到不可转换的类型14行错误:-无法将UnIntentionalObjectCreation应用于(int)

So your method accepts a Byte , while you are passing an int . 因此,当您传递int ,您的方法接受Byte You can simply replace Byte variables/methods with int . 您可以简单地用int替换Byte变量/方法。

public class UnIntentionalObjectCreation {

  private static int sumOfIntegerUptoN(int N) {
    int sum = 0;
    for (int i = 0; i < N; i++) {
        sum += i;
    }
    System.out.println(sum);
    return sum;
  }

  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    sumOfIntegerUptoN(10);
    long end = System.currentTimeMillis();
    System.out.println((end - start));

  }
}

Should do the work. 应该做的工作。

Byte is an object but his behaviour is different form Integer , Long and Double , you cannot use it for basic arithmetic operations. Byte是一个对象,但其行为不同于IntegerLongDouble ,您不能将其用于基本算术运算。 And even if you can assign an integer number to a Byte object like this: 即使您可以像这样为Byte对象分配一个整数:

Byte a = 10;

Quite strangely, you cannot pass an integer number as parameter like you did calling sumOfIntegerUptoN method. 奇怪的是,您不能像调用sumOfIntegerUptoN方法那样将整数作为参数传递。

In your case, instead of a Byte I suggest to use a primitive data type like an int . 在您的情况下,我建议使用诸如int类的原始数据类型代替Byte

Byte object is immutable, so you cannot increment and decrement it. Byte对象是不可变的,因此您不能对其进行递增和递减。

The Byte class wraps a value of primitive type byte in an object. Byte类将原始类型byte的值包装在对象中。 An object of type Byte contains a single field whose type is byte. 字节类型的对象包含一个类型为字节的字段。

You should have a look at what is autoboxing and autounboxing in Java and which kind of primitive data type exists in java . 您应该了解一下Java中的自动装箱和自动装箱以及Java中 存在哪种原始数据类型

There is also another aspect I suggest to take care, a Byte is only 8 bit, so the range of number your can handle is quite small (-128/+127). 我还建议您注意另一个方面,一个字节只有8位,因此您可以处理的数字范围很小(-128 / + 127)。

There are few things I could notice - 我几乎没有注意到-

One

sumOfIntegerUptoN(10); // this is expected to be of Type

which can be modified as - 可以修改为-

sumOfIntegerUptoN(((Integer)10).byteValue()); // wraps the integer converted to byte into Byte

Two

sum += i; //where both these are Byte

the operation is not permitted according to the doc here . 根据此处文档,不允许进行该操作。 Since the values of the variables in the expression above are not known at the compile time. 由于上述表达式中变量的值在编译时未知。

As it happens in your case, do take a look at Why can not I add two bytes and get an int and I can add two final bytes get a byte? 碰巧在您的情况下,请看看为什么我不能添加两个字节并获取一个int值,而我可以添加两个最后字节获取一个字节?

Hence would recommend changing all the Byte occurrences in your code to int and perform operations accordingly. 因此,建议将代码中所有出现的Byte更改为int并相应地执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM