软件构造笔记——java中的数据类型

Data type in programming languages

1.1 Types and Variable

  • Types: A type is a set of values, along with operations that can be performed on those values.

  • Variables: Named location that stores a value of one particular type.

    – Form: TYPE NAME;

    – Example: String foo;

    – Java中的变量类型:

    • 类变量:独立于方法之外的变量,用static修饰。

    • 实例变量:独立于方法之外的变量,但没有static修饰。

    • 局部变量:类的方法中的变量。

    • 注:类变量与实例变量统称为成员变量。如图:

    • 软件构造笔记——java中的数据类型_第1张图片

      举例:

      public class Variable {
          static int x = 0;	// 类变量
          
          String str = "Hello World";	// 实例变量
          
          public void method() {
              
              int y = 0;	// 局部变量
                  
          }
      }
      

1.2 Types in Java

Primitive Types Object Reference Types
int , long, byte, short, char, float, double, boolean Classes, interfaces, arrays, enums, annotations
Immutable Some mutable, some not

关于java中变量在内存中的分配情况见转载的文章:java中成员变量和局部变量在内存中的分布

1.3 Boxed primitives

  • Immutable containers for primitive types

    – Boolean, Integer, Short, Long, Character, Float, Double

  • Language does auto-boxing and auto-unboxing(语言会自动进行转换,但会降低效率)

你可能感兴趣的:(Java,HIT软件构造)