如何打印出Java数据类型的类型

如何打印出Java数据类型的类型

本文介绍了如何打印出Java数据类型的类型,大小和范围?尤其是那些修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我可以打印出a的字体大小和范围;使用bollean的原始Java数据类型,使用以下方法.

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

}

从论坛复制的代码.

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

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

请帮助.

推荐答案

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

Lots of things to clarify here...:

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

  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.

Java只有八种原始类型 :booleanbytechardoublefloatintlongshort

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

Java还具有八个对应的类,主要用于自动装箱,还可以提供有关原始类型的信息,如您的示例代码所示.

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.

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

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.

术语数据类型"也指对象类型. Java没有等效于C sizeof运算符,因为您不需要像C中那样分配内存.如果您 do 需要知道对象的内存占用量,请看一下此处.

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.

这篇关于如何打印出Java数据类型的类型,大小和范围?尤其是那些修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:53