当我们有Wrapper Classes时,为什么Java中仍然存在原始数据类型? [英] Why are there still primitive data types in Java when we have Wrapper Classes?

查看:94
本文介绍了当我们有Wrapper Classes时,为什么Java中仍然存在原始数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java不是纯粹的OOP语言becoz Java仍然使用原始数据类型作为int,float等。

然后问题是我们在Java中有Wrapper Classes,所以现在为什么我们仍然使用原始数据类型???

为什么我们只使用Wrapper类作为我们的数据类型???

Java is not a pure OOP language becoz Java still uses primitive datatypes as int, float, etc.
But then the question arises that we have Wrapper Classes in Java, so now why are we still using primitive datatypes???
why not we use only Wrapper classes for our datatypes???

推荐答案

我很可能不是这个人回答这个问题。但无论如何我的评论。



语言是否是OOP并不是真的取决于使用原始数据类型。如果您认为是,那么您就不会明白OOP。



但我同意:

有一个基类为一切(比如在#c ... uups c#我的意思是对象)是一个非常好的和舒适的策略。
I'm most probably not the person to answer this. But anyway my comments.

Whether a language is OOP or not is not really depending on using "primitive" datatypes or not. If you think it is, then you do not get the point on OOP.

But I agree:
Having a base class for "everything" (like in #c...uups c# I mean object) is a very good and comfortable strategy.


作为纯粹的OO语言并不意味着你不能拥有原始类型。 C#仍然具有原始类型,但它是面向对象的,因为那些原始类型就像对象一样(例如,你可以对它们运行方法调用,它们有.Net类型,你可以将它们传递给采用 object的方法,在泛型类型参数等中使用它们。)



继续使用原始类型的原因是它们非常有效,因为它上面的数据和操作直接映射在许多情况下处理器操作码。 Java可能是VM上的字节码语言,但是类可以将JIT编译为本机代码,而且在本机(原始)数据类型上更容易。



Java的原因不像C#那样做事情是因为它们没有将引用和值类型区分为语言的面向对象部分的干净部分。在C# int 中意味着System.Int32,一个完全成熟的.Net类型,但它不是一个类:它是一个 struct ,一个值类型。在Java中,唯一的值类型是原始类型,它们存在于面向对象的系统之外,该系统仅包含类(即Java),即引用类型。 C#/ .Net从其他几种语言中吸取了教训,其中一种就是这种语言,要将原始类型带入OO帐篷,你需要将值类型作为环境的一个组成部分。
Being a pure OO language doesn't mean you can't have primitive types. C# still has primitive types, but it is object oriented, because those primitive types act like objects (e.g. you can run method calls on them, they have a .Net type, you can pass them to methods taking object, use them in generic type parameters etc).

The reason to keep using primitive types is that they are very efficient, because the data and operations on it map directly to processor opcodes in many cases. Java may be a bytecode-on-VM language but classes get JIT-compiled to native code and that's far easier on native (primitive) data types.

The reason Java doesn't do things the same way as C# is because they don't make the distinction between reference and value types as a clean part of the object oriented part of the language. In C# int means System.Int32, a fully fledged .Net type, but it is not a class: it is a struct, a value type. In Java the only value types are the primitive types and they exist outside the object oriented system, which consists (in Java) of only classes, i.e. reference types. C#/.Net learnt lessons from several other languages and one of them is this one, that to bring primitive types inside the OO tent you need to have value types as an integral part of your environment.


这篇关于当我们有Wrapper Classes时,为什么Java中仍然存在原始数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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