Chapter 3 Introduction to Objects and Input/Output

与声明一个primitive variable不同,声明一个对象的时候,并不创建用来存储一个对象的内存空间,而是创建了一个存储该对象所在内存空间的地址。

在java里,new是一个操作符,它让系统分配一个特定类型的存储空间,并返回该内存空间的地址。

String str;

str = “Java Programing”;

String str = new String("Java Programing");

是有区别的,前者是把已经存在的“Java Programming”的存储地点赋值给str,而后在,需要先生存一个"Java Programing",然后再把它的地址赋值给str。


 In reality, when the statement in Line 4 executes, it first looks if the program
has already created the string "Java Programming". If this is the case, then the
String variable str will point to that memory location. However, when the statement
in Line 5 executes, the system will allocate a memory space, store the string "Java
Programming" into that memory space, and then store the address of that memory
space into str. This is a key difference and plays an important role when strings and
String variables are compared, which we will explain in Chapter 4.


In Java, variables such as str are called reference variables. More formally, reference
variables are variables that store the address of a memory space. In Java, any variable
declared using a class (such as the variable str) is a reference variable. Because str is a
reference variable declared using the class String, we say that str is a reference
variable of the String type.

Primitive type variables store data directly into their own memory spaces. Reference
variables store the address of the object containing the data. An object is an instance of
a class and the operator new is used to instantiate an object. In some languages, such as
C++, reference variables are called pointers.
Before discussing the class String, we first discuss how to use predefined methods in a
program.


There are two types of methods in a class: static and non-static. A static
method can be used, that is, called, using the name of the class containing the method.
static修饰的method,可以直接用类名来调用它,比如Math.pow(2,3)。

 

129页,输入输出

转载于:https://www.cnblogs.com/hare/p/4089313.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值