类和类型之间的差异 [英] Difference between class and type

查看:98
本文介绍了类和类型之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Java的新手,我对类型的概念感到困惑。
例如,如果对象Hello World!属于类型 String class String ?或者两者都有?

解决方案

类是一种类型。接口是一种类型。原语是一种类型。数组是一个类型。



因此,每个类型也是一个类,一个接口,一个基本类型或一个数组。



有两种不同的类型:原始类型和引用类型:




  • 相同类型的原始值。这样的值只能通过对该变量的赋值操作更改。

  • 引用类型的变量始终保存对对象的引用的值。所有对象,包括数组,都支持 Object 类的方法。引用类型是类类型,接口类型和数组类型。



每个数据都有一个类型,它定义了它的结构,即它占用了多少内存,如何布局,



原始类型的示例:


  1. int

  2. float
  3. char
  4. boolean

    类类型示例:


    1. String

    2. 整数

    3. Boolean

    4. ArrayList

    5. StringBuilder

    接口类型示例:


    1. 集合

    2. 列表

    3. 映射

    4. Serializable

    数组类型示例:


    1. int []

    2. String []

    3. Integer [] [] []

    基本上,您可以引用为变量的任何内容都具有类型,类是一种类型。



    更多信息: http://docs.oracle.com/ javase / specs / jls / se8 / html / jls-4.html


    Being new to Java, I'm confused between the concepts of class and type. For example, should the object "Hello World!" belong to the type String or class String? Or maybe both?

    解决方案

    A class is a type. An interface is a type. A primitive is a type. An array is a type.

    Therefore, every type is also either a class, an interface, a primitive, or an array.

    There are two distinct categories of types: primitive types and reference types:

    • A variable of primitive type always holds a primitive value of that same type. Such a value can only be changed by assignment operations on that variable.
    • A variable of reference type always holds the value of a reference to an object. All objects, including arrays, support the methods of class Object. The reference types are class types, interface types, and array types.

    Every piece of data has a type which defines its structure, namely how much memory it takes up, how it is laid out, and more importantly, how you can interact with it.

    Examples of primitive types:

    1. int
    2. float
    3. char
    4. boolean

    Examples of class types:

    1. String
    2. Integer
    3. Boolean
    4. ArrayList
    5. StringBuilder

    Examples of interface types:

    1. Collection
    2. List
    3. Map
    4. Serializable

    Examples of array types:

    1. int[]
    2. String[]
    3. Integer[][][]

    Basically, anything that you can refer to as a variable has a type, and classes are a kind of a type.

    More info here: http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html

    这篇关于类和类型之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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