简体   繁体   English

如何打印出 java 数据类型的类型、大小和范围? 尤其是那些带有修饰符的?

[英]how can i print out the type, size and range of a java data type ? esp those wtih modifiers?

Currently i can print out the type size and range of a;;目前我可以打印出a的字体大小和范围;; primitive java data types except bollean using the following method.原始 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);
    }

} }

Code copied from a forum.从论坛复制的代码。

The question is how can i print the same for, say signed long or signed char or unsigned int?问题是我如何打印相同的内容,比如signed long 或signed char 或unsigned int?

Kindly help.请帮忙。

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 , eg to implement a hash function. 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 , eg to implement a hash function. There is no unsigned keyword, no unsigned types and, of course, no corresposing type size.没有unsigned关键字,没有无符号类型,当然也没有相应的类型大小。

  2. Java only has eight primitive types : boolean , byte , char , double , float , int , long , short Java 只有八种原始类型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. Java 也有八个相应的类,主要用于自动装箱,但也提供有关原始类型的信息,如您的示例代码所示。

  4. Modifiers (eg public , final , static ) only affect the visibility and access semantics of a primitive - not its basic properties, such as its size or range.修饰符(例如publicfinalstatic )仅影响原语的可见性和访问语义,而不影响其基本属性,例如其大小或范围。

  5. The term "data type" also refers to object types.术语“数据类型”也指 object 类型。 Java has no equivalent for the C sizeof operator, since you do not need it to allocate memory as in C. 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 .如果您确实需要了解 object 的 memory 封装,请查看此处

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. Java 没有 sizeof 运算符来查找原始数据类型的大小,但是除 Boolean 之外的所有 Java 原始包装器都提供了一个大小为字节的数据类型除以八的 SIZE 常量。

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.");
  }
}

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

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