对象与原始对象

发布于 2024-12-23 06:02:38 字数 124 浏览 1 评论 0原文

最近有一位面试官要求定义对象和基元之间的区别。考虑到所有语言都以原语开头,这似乎是一个奇怪的问题。你会如何回答这个问题?

我还应该注意到,这次面试是针对前端开发职位的,所以他指的语言(我假设)是 JavaScript。

Recently had an interviewer ask to define the difference between objects and primitives. Seemed like an odd question considering that all languages begin with a primitive. How would you have answered this question?

I should also note that this interview was for a front-end development position so the language (I assume) he was referring to was JavaScript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆躺在深渊里 2024-12-30 06:02:38

原语是一种不由其他数据类型组成且不能进一步分解的数据类型。它就像编程场景中的原子。我说原子是因为原子是物质的基本单位,没有任何东西可以从它派生出来。

我的意思是,C 中的 int 不能分解为更小的数据类型。另一方面,对象可以被认为是由多个原始类型组成的分子。例如,string 是 C++ 标准库的一部分;但是,它是一个对象,内部由较小的数据类型组成并包含方法。

需要注意的是,并非所有面向对象的语言都是基于类的(例如 Javascript)。您无法在 Javascript 中定义类,因此这里的对象有很大不同。尽管 Javascript 中的所有内容都是对象(Ruby 也是),但 Number 对象实际上是内部原语的包装器。

A primitive is a data type that is composed of no other data types and can not be broken down any further. It is like the atoms in the programming scenario. I say atom because atom is a basic unit of matter and there is nothing that can be derived from it.

I mean, an int in C can not be broken down into smaller data type. An object, on the other hand can be thought of a molecule, consisting of more than one primitive type. For example, string comes as part of the C++ standard library; however, it is an object and it is composed of smaller data types internally and contains methods.

It is important to note that not all object-oriented languages are class based (eg. Javascript) You can not define a class in Javascript, so an object is quite different here. Even though everything in Javascript is an object (Ruby also), the Number object is really a wrapper for an internal primitive.

赏烟花じ飞满天 2024-12-30 06:02:38

Java的角度来看:

  1. 原语不是由其他数据类型组成的。作为对象的地方
    可以并且通常是。
  2. 基元按值传递,即基元本身的副本
    已通过。而对于对象,则传递引用的副本,
    不是物体本身。
  3. 原语是独立的数据类型,即不存在
    他们的层次结构/超类。而每个对象都是类的后代
    “目的”。
  4. 每个对象都有一些它自己继承的默认方法
    来自类 Object(例如 toString()、clone()、wait()、notify()、
    通知所有()等)。原语没有任何方法
    与他们自己有关。
  5. 基元是不可变的,它们可以直接使用而无需任何特殊
    关心。然而对于物体来说,需要特别小心,它们是
    默认情况下不是不可变的。
  6. 对于对象,有两种类型的副本:浅副本和深副本。那里
    是它们之间的显着差异。所以选择取决于
    我们打算如何使用它们。对于原语,没有这样的
    区别在于,默认情况下,所有内容都只是深复制。

From Java perspective:

  1. A primitive is not composed of other data types. Where as an object
    can be and generally is.
  2. Primitives are passed by value, i.e. a copy of the primitive itself
    is passed. Whereas for objects, the copy of the reference is passed,
    not the object itself.
  3. Primitives are independent data types, i.e. there does not exist a
    hierarchy/super class for them. Whereas every Object is descendent of class
    "Object".
  4. Every object has some default methods of itself, which it inherits
    from the class Object (e.g. toString(), clone(), wait(), notify(),
    notifyAll(), etc.). The primitives does not have any method
    associated with themselves.
  5. Primitives are immutable, they can be used as so without any special
    care. Whereas for objects, special care needs to be taken, they are
    not immutable by default.
  6. With objects, there are two types of copies, Shallow and Deep. There
    is a significant difference between them. So the choice depends on
    how do we intend to use them. With primitives, there is no such
    difference, everything is by default deep copy only.
铁轨上的流浪者 2024-12-30 06:02:38

让我们想象一下实际的差异。

基元

1) 基元数据类型使用少量内存来表示单个数据项。相同原始类型的所有数据都具有相同的大小。

例如:
原始类型 int 表示使用 32 位的整数。所有 int 类型的变量都使用 32 位。

2)Java中只有八种基本数据类型:byte、short、int、long、float、double、char和boolean。 Java 程序不能定义任何其他基本数据类型。

对象

对象是一大块内存,可能包含大量数据以及处理这些数据的方法(小程序)。 Java 有数千个标准对象类,程序员可以轻松创建其他类。 (虽然标准课程有上千个,但本课程你只需要熟悉十几个课程即可。)

Let us Visualize the actual differences.

Primitives

1) A primitive data type uses a small amount of memory to represent a single item of data. All data of the same primitive type are the same size.

For Example:
Primitive type int represents integers using 32 bits. All variables of type int use 32 bits.

2) There are only eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean. A Java program cannot define any other primitive data types.

Objects

An object is a large chunk of memory that can potentially contain a great deal of data along with methods (little programs) to process that data. There are thousands of object classes that come standard with Java, and a programmer can easily create additional classes. (Although there are thousands of standard classes, for this course you only need become familiar with a dozen or so classes.)

天煞孤星 2024-12-30 06:02:38

我认为primitive不能再像int、string那样进行划分(比如内置数据类型)

另一方面,对象可以分为数组、结构体等小块。

我只是一名学生,这是我的观点。
如果您认为我的回答有误,可以指正。
谢谢

I think primitive cannot divided any more like int, string (such as built-in data type)
.
On the other way, object can divide into small pieces like array, structure.

I am just a student and this is my opinion.
If you think my answer is wrong, you can correct me.
Thanks

原野 2024-12-30 06:02:38

在Java中,当您将k字段发送到测试方法时,首先它会转换为Integer类型。因为原始类型不是 Object 类型的子类型之一。

public static void main(String[] args) {
    int k=3;
    test(k);
    test2(k);
}

private static void test(Object k) {
    System.out.print(k.getClass().isPrimitive());//false
}

private static <T> void test2(T k) {
    System.out.print(k.getClass().isPrimitive());//false
}

In Java, when you send k field to test method, firstly it convert to Integer type. Because a primitive type is not one of sub types of Object type.

public static void main(String[] args) {
    int k=3;
    test(k);
    test2(k);
}

private static void test(Object k) {
    System.out.print(k.getClass().isPrimitive());//false
}

private static <T> void test2(T k) {
    System.out.print(k.getClass().isPrimitive());//false
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文