为什么与 C 不同,java 具有固定的数据类型大小 [英] Why java has fixed data type size unlike C

查看:39
本文介绍了为什么与 C 不同,java 具有固定的数据类型大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 中,我们知道数据类型(例如 int)的大小可能因编译器/硬件而异.

In C as we know the size of data types (ex. int) can vary depending on compiler / hardware.

但是为什么java语言中数据类型的大小是常量呢?为什么我们不能根据编译器在 java 中灵活地处理不同的数据类型大小?

But why the size of data types is constant in java language? Why don't we have the flexibility for different data type size in java depending on compiler?

推荐答案

JVM(Java 虚拟机)旨在独立于平台.如果跨平台的数据类型大小不同,那么就牺牲了跨平台的一致性.

The JVM (Java Virtual Machine) is designed to be platform independent. If data type sizes were different across platforms, then cross-platform consistency is sacrificed.

JVM 将程序与底层操作系统和平台隔离.这可能会使执行特定于系统的工作变得困难,但好处是您可以一次编写,随处运行(这在很大程度上是正确的,但有一些不幸的问题.一次编写,随处测试是一种更实用的方法).

The JVM isolates the program from the underlying OS and platform. This can make life difficult for performing system-specific work, but the benefits are that you can write-once, run-anywhere (this is largely true, with some unfortunate issues. Write-once, test-everywhere is a much more practical approach).

这篇关于为什么与 C 不同,java 具有固定的数据类型大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆