2.Everything is an Object

You manipulate objects with references

这里有一个疑惑,见注解【10

可以在创建对象之前创建引用,比如 string s; 表示一个string类对象的引用,它并没有“指向”一个实际对象。

 

You must create all the objects

 

Where storage lives

There are six different places to store data:

Registers. 寄存器在处理器中,数量有限,速度最快,程序员无法控制,由编译器决定分配,对程序员是透明的。

The stack. ram中,通过stack pointer与处理器联系,该指针向下移动分配新的空间,向上则释放。所以编译器需要准确了解栈中数据的大小和生存期。Java的一些变量,如对象引用存放在堆栈中,对象本身则不在。

The heap.同样在ram中,尺寸很大,java的对象存放在此 ,分配速度要彼stack中慢。

Static storage. ram中的固定地址,用来存放程序运行期间一直存在的数据,可以用static关键字来声明对象中的一个成员为静态的,但是对象不存放在static storage.

Constant storage.常量直接存放在代码中,而代码是不改变的。在嵌入系统中,常量也可分离出来,存放在rom

Non-RAM storage. 流对象和持久对象,持久对象的意义在与:将对象转化并存储在其他媒介上,在需要的时候可以恢复为基于ram的对象。

 

Special case: primitive types

基本类型不是在堆中创建的,而是在堆栈中生成的,直接保存值,而不使用引用。

基本类型所占空间大小在所有平台上都是不变的,这也是java的可移植性的一个原因。

!!!所有数值类的基本类型都是signed有符号的,与c等不同

The size of the boolean type is not explicitly specified; it is only defined to be able to take the literal values true or false.

每种基本类型都对应着该类型的包装类

The “wrapper” classes for the primitive data types allow you to make a nonprimitive object on the heap to represent that primitive type. For example:

char c = 'x';

Character C = new Character(c);

or you could also use:

Character C = new Character('x');

 

 

High-precision numbers

Java includes two classes for performing high-precision arithmetic: BigInteger and BigDecimal. Although these approximately fit into the same category as the “wrapper” classes, neither one has a primitive analogue.

 

 

Arrays in Java

A Java array is guaranteed to be initialized and cannot be accessed outside of its range. The range checking comes at the price of having a small amount of memory overhead on each array as well as verifying the index at run time, but the assumption is that the safety and increased productivity is worth the expense.

When you create an array of objects, you are really creating an array of references, and each of those references is automatically initialized to a special value with its own keyword: null.

Java中引用的缺省值是null

 

 

You never need to destroy an object

 

Scoping

In C, C++, and Java, scope is determined by the placement of curly braces {}.

A variable defined within a scope is available only to the end of that scope.

 

下面这种做法在C 中是合法的,但是在java中确不是,

{

  int x = 12;

  {

    int x = 96; // Illegal

  }

}

其实这种方式并不会给程序带来什么好处,它只不过利用了编译器的“不作为”,完全可以不使用。

 

 

Scope of objects

Java的对象在作用域之外依然存在,占据内存,但是引用则与基本变量类似,作用域外就实效。 //??那么到底什么叫依然存在,如果程序一个对象引用超过了作用域,它的引用被清除,那么该引用原来指向的那段内存区域没有恢复自由身么??

 

 

Creating new data types: class

 

Fields and methods

All you do in Java is define classes, make objects of those classes, and send messages to those objects

A field is an object of any type that you can communicate with via its reference. It can also be one of the primitive types (which isn’t a reference).

当一个类的成员变量没有被初始化的时候,它都包含默认值,该值是一段为0 的内存空间在成员变量的类型上的表示。

如果基本数据类型不是类的成员变量而是“local”变量,它不会被编译器初始化,如果你不人为对它进行初始化,那么java编译器会给出编译错误,而在c++中,编译器只是简单警告。

 

Methods, arguments, and return values

In many languages (like C and C++), the term function is used to describe a named subroutine. The term that is more commonly used in Java is method, as in “a way to do something.”

The fundamental parts of a method are the name, the arguments, the return type, and the body. Here is the basic form是否还应包含访问控制符?

The method name and argument list together uniquely identify the method。注意,返回值类型并不能标识一个方法! 这么说在子类中可以覆盖父类的方法并改变其返回值???可能是编译错误。

 

Methods in Java can be created only as part of a class. A method can be called only for an object类的静态方法是个例外,它可以通过类调用。

Calling a method is commonly referred to as sending a message to an object

Object-oriented programming is often summarized as simply “sending messages to objects.”

 

 

The argument list

Return关键字做了两件事:离开方法体,返回得到的值。可以使用return从方法的任意处返回。

方法在结尾的时候会返回,所以一个返回值为void的方法不需显示提供return语句。

 

 

Building a Java program

Name visibility

Java通过一种崭新的方法来避免其他语言(如c++)中的名字冲突问题----包。

一个普遍同意的约定:使用倒写的域名来定义命名空间。

 

 

Using other components

Import语句提供了编译器查找源文件中出现类的途径。通常将一个类库放入一个包中。

 

 

The static keyword

创造一个类实质上只是在 描述(!!)该类所有对象的共性(包括状态和行为),实际上只有在你使用new关键字创建一个类的实例(对象)后,你才会得到一些实质的东西。

但是有一个例外:static 。如果你打算无论程序中生成多少对象,你都只需要某些数据的一份存储,或者你需要一个不需要与任何实例对象关联的方法,这个时候你可以使用static关键字。    一旦一个变量或者方法被声明为static,该变量或方法将不与任何特定对象实例相关联。

两种refer static 变量和方法的方法:通过该类的实例对象来访问,如 obj.staticvarname,或使用类名来指定:classname.staticvarname,后一种更常用也更make sense

 

//**有一种假设需要验证:在生成一个类的实例对象时,成员变量在每个实例中有一份存储,成员方法却没有这个必要,因为成员方法是不可修改的,所以所有特定类的对象完全可以使用同一份拷贝的方法,这样可以很大程度上降低空间,java的实际情况是否是这样?

 

 

Your first Java program

import语句是针对源文件的,从此更能看出 import 是给编译器用的。Import语句也是java语句,必须用’;’结束。

 

Compiling and running

 

Comments and embedded documentation

两种注释,一个来自c,一个来自c++,还有很多注释是建立在这两种注释的基础上的,可以使用自己的风格。

 

 

Comment documentation

 

Syntax

/** ……….*/

跳过!!!

 

 

Coding style

 

类名的第一个字母大写,其余字母小写。如果类名包含多个单词,则每个单词首字母大写,单词间直接相连。

方法,变量,引用的书写也与上述类似,只是首字母小写。

 

 

Summary

。。。。。。。。。。。。。。。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值