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

编程入门 行业动态 更新时间:2024-10-12 01:23:31
本文介绍了如何打印出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...:

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

  • 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只有八种原始类型 :boolean,byte,char,double,float,int,long,short

    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.

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

    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数据类型的类型,大小和范围?尤其是那些修饰符?

    本文发布于:2023-06-06 10:52:11,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/539069.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:尤其是   数据类型   大小   类型   修饰符

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!