我怎样才能打印出java数据类型的类型、大小和范围?特别是那些带有修饰符的?

发布于 2024-11-19 02:40:00 字数 1190 浏览 0 评论 0 原文

目前我可以打印出 a;; 的字体大小和范围除 bollean 之外的原始 java 数据类型使用以下方法。

public class sizJ {

    public static void display(Class<?> type, int size, Number min, Number max) {
            System.out.printf("%-6s %-2s %-20s %s\n", type, size, min, max);
    }

    public static void main(String[] args) {
            System.out.printf("%s %-2s %-20s %s\n","type","size","min","max");
            display(Byte.TYPE, Byte.SIZE, Byte.MIN_VALUE, Byte.MAX_VALUE);
            display(Character.TYPE, Character.SIZE, (int) Character.MIN_VALUE, (int) Character.MAX_VALUE);
            display(Integer.TYPE, Integer.SIZE, Integer.MIN_VALUE, Integer.MAX_VALUE);
            display(Float.TYPE, Float.SIZE, Float.MIN_VALUE, Float.MAX_VALUE);
            display(Double.TYPE, Double.SIZE, Double.MIN_VALUE, Double.MAX_VALUE);
            display(Long.TYPE, Long.SIZE, Long.MIN_VALUE, Long.MAX_VALUE);
            display(Double.TYPE, Double.SIZE, Double.MIN_VALUE, Double.MAX_VALUE);

            display(SignedDouble.TYPE, Double.SIZE, Double.MIN_VALUE, Double.MAX_VALUE);
    }

从论坛复制的代码

问题是我如何打印相同的内容,例如signed long或signed char或unsigned int?

请帮忙。

Currently i can print out the type size and range of a;; primitive java data types except bollean using the following method.

public class sizJ {

    public static void display(Class<?> type, int size, Number min, Number max) {
            System.out.printf("%-6s %-2s %-20s %s\n", type, size, min, max);
    }

    public static void main(String[] args) {
            System.out.printf("%s %-2s %-20s %s\n","type","size","min","max");
            display(Byte.TYPE, Byte.SIZE, Byte.MIN_VALUE, Byte.MAX_VALUE);
            display(Character.TYPE, Character.SIZE, (int) Character.MIN_VALUE, (int) Character.MAX_VALUE);
            display(Integer.TYPE, Integer.SIZE, Integer.MIN_VALUE, Integer.MAX_VALUE);
            display(Float.TYPE, Float.SIZE, Float.MIN_VALUE, Float.MAX_VALUE);
            display(Double.TYPE, Double.SIZE, Double.MIN_VALUE, Double.MAX_VALUE);
            display(Long.TYPE, Long.SIZE, Long.MIN_VALUE, Long.MAX_VALUE);
            display(Double.TYPE, Double.SIZE, Double.MIN_VALUE, Double.MAX_VALUE);

            display(SignedDouble.TYPE, Double.SIZE, Double.MIN_VALUE, Double.MAX_VALUE);
    }

}

Code copied from a forum.

The question is how can i print the same for, say signed long or signed char or unsigned int ?

Kindly help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-11-26 02:40:00

这里有很多事情需要澄清...:

  1. Java 没有像 C 那样的无符号原语 - 顺便说一句,对于那些需要做大量 bit-twiddling,例如实现哈希函数。没有 unsigned 关键字,没有无符号类型,当然也没有对应的类型大小。

  2. Java 只有八种基本类型布尔字节字符双精度浮点int >,

  3. Java也有八个对应的类,主要用于自动装箱,还提供有关原语的信息类型,如您的示例中所示

  4. 修饰符(例如 publicfinalstatic)仅影响原语的可见性和访问语义 - 不影响 它的基本属性,例如它的大小或范围。

  5. 术语“数据类型”也指对象类型。 Java 没有 C sizeof 运算符的等效项,因为您不需要像 C 中那样用它来分配内存。如果您确实需要知道对象的内存占用量,请查看此处

Lots of things to clarify here...:

  1. Java does not have unsigned primitives like C - a significant issue, by the way, for those who need to do a lot of bit-twiddling, e.g. to implement a hash function. There is no unsigned keyword, no unsigned types and, of course, no corresposing type size.

  2. Java only has eight primitive types: boolean, byte, char, double, float, int, long, short

  3. Java also has eight corresponding classes, primarily used for autoboxing, but also to provide information on the primitive types, as seen in your sample code.

  4. Modifiers (e.g. public, final, static) only affect the visibility and access semantics of a primitive - not its basic properties, such as its size or range.

  5. The term "data type" also refers to object types. Java has no equivalent for the C sizeof operator, since you do not need it to allocate memory as in C. If you do need to know the memory footprint of an object have a look here.

ぶ宁プ宁ぶ 2024-11-26 02:40:00

Java 没有 sizeof 运算符来查找原始数据类型的大小,但除 Boolean 之外的所有 Java 原始包装器都提供一个以位为单位的 SIZE 常量,该常量可以除以 8 以获得以字节为单位的数据类型的大小。

class SizePrimitiveTypes
{
  public static void main (String[] args)
  {
    System.out.println("Size of byte: " + (Byte.SIZE/8) + " bytes.");
    System.out.println("Size of short: " + (Short.SIZE/8) + " bytes.");
    System.out.println("Size of int: " + (Integer.SIZE/8) + " bytes.");
    System.out.println("Size of long: " + (Long.SIZE/8) + " bytes.");
    System.out.println("Size of char: " + (Character.SIZE/8) + " bytes.");
    System.out.println("Size of float: " + (Float.SIZE/8) + " bytes.");
    System.out.println("Size of double: " + (Double.SIZE/8) + " bytes.");
  }
}

Java has no sizeof operator to find the size of primitive data types but all Java primitive wrappers except Boolean provide an SIZE constant in bits that could be divided by eight to get the size of a data type in bytes.

class SizePrimitiveTypes
{
  public static void main (String[] args)
  {
    System.out.println("Size of byte: " + (Byte.SIZE/8) + " bytes.");
    System.out.println("Size of short: " + (Short.SIZE/8) + " bytes.");
    System.out.println("Size of int: " + (Integer.SIZE/8) + " bytes.");
    System.out.println("Size of long: " + (Long.SIZE/8) + " bytes.");
    System.out.println("Size of char: " + (Character.SIZE/8) + " bytes.");
    System.out.println("Size of float: " + (Float.SIZE/8) + " bytes.");
    System.out.println("Size of double: " + (Double.SIZE/8) + " bytes.");
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文