热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

原始类型和引用类型对象-Primitivetypeandreferencetypeobjects

IhaveanexamquestionfromapastpaperthatImtryingtoanswer:我在过去的一篇论文中有一个考试题,我正在尝试回答:Disc

I have an exam question from a past paper that I'm trying to answer:

我在过去的一篇论文中有一个考试题,我正在尝试回答:

Discuss variables of type primitive, reference and static in the context of a programming language. Give suitable examples [8].

在编程语言的上下文中讨论基元,引用和静态类型的变量。举个合适的例子[8]。

The answer I have so far is:

我到目前为止的答案是:

A primitive type is an object which the language has given a predefined value. These types include int, bool and float. Reference type objects refer to these primitive types in a particular sequence when instantiated. Examples of these are strings and arrays. The static keyword, when assigned to a variable, means that there is only one instance of this variable and the value assigned applies to all references of the variable.

基本类型是语言已给出预定义值的对象。这些类型包括int,bool和float。引用类型对象在实例化时以特定顺序引用这些基元类型。这些的示例是字符串和数组。 static关键字在分配给变量时,意味着此变量只有一个实例,并且赋值的值适用于变量的所有引用。

I'm fairly new to programming so I don't know if this is exactly right, so if anyone could give me some tips on how to improve the mark I would get for this question I'd greatly appreciate it.

我对编程很新,所以我不知道这是否完全正确,所以如果有人能给我一些关于如何改进标记的提示我会得到这个问题我会非常感激。

3 个解决方案

#1


A primitive type is an object which the language has given a predefined value

基本类型是语言已给出预定义值的对象

Why? Even references can have predefined values as noted. For primitive (built in) types you may want to say these are types that a language provides built in support for. What your instructor might be glad to hear about is if you say that most primitive types are also value types in C# and you might want to discuss value types semantics (e.g., value type variable directly contains value - whereas a reference variable just contains an address to some object in memory).

为什么?即使引用也可以具有预定义值,如上所述对于原始(内置)类型,您可能希望说这些是语言提供内置支持的类型。您的教师可能很高兴听到的是,如果您说大多数原始类型也是C#中的值类型,您可能想要讨论值类型语义(例如,值类型变量直接包含值 - 而引用变量只包含一个地址到内存中的某个对象)。

About reference types again you may say that a reference variable doesn't contain the value or object directly - rather just a reference to it. Now again you may want to discuss reference semantics. For example if you have two reference variables pointing to same object - and you change the object from one reference change will be visible from another reference too - because both references point to same object. This is not the case with value types. If you assign same value type object to two different value type variables and change one variable - this change will not be visible in the second value type variable because each of them holds the value directly (e.g. each will have its own copy of the value type variable it was assigned to).

再次关于引用类型,您可能会说引用变量不直接包含值或对象 - 而只是对它的引用。现在再次,您可能想讨论引用语义。例如,如果有两个引用变量指向同一个对象 - 并且您从一个引用更改了对象,则从另一个引用中也可以看到更改 - 因为两个引用都指向同一个对象。值类型不是这种情况。如果将相同的值类型对象分配给两个不同的值类型变量并更改一个变量 - 此更改将在第二个值类型变量中不可见,因为它们中的每一个都直接保存值(例如,每个变量都有自己的值类型的副本变量它被分配给)。

Static types you have already described.

您已经描述过的静态类型。

#2


You are on the right track for sure, but you are missing some fundamental concepts about these. Also, the 3 are not mutually exclusive:

你肯定是在正确的轨道上,但是你缺少一些关于这些的基本概念。另外,3不是互斥的:

A primitive type is simply a syntax shortcut defined by the compiler for Framework Class Library or FCL types.

基本类型只是编译器为Framework Class Library或FCL类型定义的语法快捷方式。

A reference type is a pointer that represents an instance of a class. The objects they point to are allocated on the heap and the value of the variable is the memory address of that object rather than the class itself.

引用类型是表示类实例的指针。它们指向的对象在堆上分配,变量的值是该对象的内存地址而不是类本身。

Static is not a type at all, but really defines where and when fields, properties, methods, and classes can be used. A static variable lives on the class rather then an instance. A static constructor is called the first time you access the class. A static method can be called from the class definition. That explains the persistence you see on static variables as you create and destroy them.

Static根本不是一个类型,但实际上定义了可以使用字段,属性,方法和类的位置和时间。静态变量存在于类而不是实例上。第一次访问类时会调用静态构造函数。可以从类定义中调用静态方法。这解释了您在创建和销毁静态变量时看到的持久性。

#3


The answer to that question, in my opinion -- has not a thing to do with OOP and everything to do with the compiler and microprocessor.

在我看来,这个问题的答案与OOP没有任何关系,与编译器和微处理器有关。

The simplest and most accurate definition of the term that subsumes all of the qualities of a primitive type -- as I understand it -- is:

包含原始类型的所有特性的术语的最简单和最准确的定义 - 据我所知 - 是:

A primitive type must fit into the register used for operations on it -- IOW, in an X86 system -- the Accumulator.

原始类型必须适合用于操作的寄存器 - IOW,在X86系统中 - 累加器。

So, primitive types are limited to the size of the Accumulator and can be operated upon by native processor instructions. (Basic math and Boolean/bit-shifting operations). Yes, it fits into heap memory and on the stack, but those are still essentially 8-bit entities and the registers are not.

因此,原始类型仅限于累加器的大小,并且可以由本机处理器指令操作。 (基本数学和布尔/位移操作)。是的,它适合堆内存和堆栈,但那些仍然是8位实体,寄存器不是。

OOP languages do not use primitive types for their managed memory processes, they use structures that mimic primitive types. (Even in .NET, when you use the keyword int -- it uses System.Int32 to wrap that.)

OOP语言不使用原始类型作为其托管内存进程,它们使用模仿原始类型的结构。 (即使在.NET中,当您使用关键字int时 - 它使用System.Int32来包装它。)


推荐阅读
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • EPICS Archiver Appliance存储waveform记录的尝试及资源需求分析
    本文介绍了EPICS Archiver Appliance存储waveform记录的尝试过程,并分析了其所需的资源容量。通过解决错误提示和调整内存大小,成功存储了波形数据。然后,讨论了储存环逐束团信号的意义,以及通过记录多圈的束团信号进行参数分析的可能性。波形数据的存储需求巨大,每天需要近250G,一年需要90T。然而,储存环逐束团信号具有重要意义,可以揭示出每个束团的纵向振荡频率和模式。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 李逍遥寻找仙药的迷阵之旅
    本文讲述了少年李逍遥为了救治婶婶的病情,前往仙灵岛寻找仙药的故事。他需要穿越一个由M×N个方格组成的迷阵,有些方格内有怪物,有些方格是安全的。李逍遥需要避开有怪物的方格,并经过最少的方格,找到仙药。在寻找的过程中,他还会遇到神秘人物。本文提供了一个迷阵样例及李逍遥找到仙药的路线。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • 本文介绍了Codeforces Round #321 (Div. 2)比赛中的问题Kefa and Dishes,通过状压和spfa算法解决了这个问题。给定一个有向图,求在不超过m步的情况下,能获得的最大权值和。点不能重复走。文章详细介绍了问题的题意、解题思路和代码实现。 ... [详细]
author-avatar
张雍昊
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有