java打印数据类型,如何打印出Java数据类型的类型,大小和范围?特别是那些改性剂?...

这篇博客讨论了如何在Java中获取原始数据类型(如byte, char, int等)的大小和取值范围。Java没有无符号整数类型,并且不支持C语言中的sizeof运算符。作者通过示例代码展示了如何打印出各种基本类型的大小和范围,但询问如何展示类似有符号长整型或有符号字符的范围。博客解释了Java中不存在无符号类型,并指出Java的八种原始类型及其对应的类主要用于自动装箱和提供类型信息。
摘要由CSDN通过智能技术生成

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.

解决方案

Lots of things to clarify here...:

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 only has eight primitive types: boolean, byte, char, double, float, int, long, short

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.

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值