Java Note - Everything is an object

Each programming language has its own means of manipulating elements in memory.

Java treat everything as an object, using a single consistent syntax.
The identifier you manipulate is actually a reference to the object.
When Java sees null, it recognizes that the reference in question is not pointing to an object.

When you create a reference, you want to connect it with a new object, you do so with the “new” operator.

Where storage lives

Stack - The Java reference live on the stack
stack has direct support from the processor via its stack pointer, the statck pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage.

Heap - The Java object live on the heap
whenever you need an object, you simply write the code to create it by using “new” and the storage is allocated on the heap when that code is executed.

Primitive types

One group of types which you will use quite often in your programming.

Java determines the size of each primitive type, these size do not chang from one machine architecture to another.

boolean (unfix)
byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)
float (4 bytes)
double (9 bytes)
char (2 bytes)

All numeric types are singed.

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

All primitive type has it own wrapper class.

Never need to destroy an object

The concept of scope determines both the visibility and lifetime of the variables. In C/C++/Java, scope is determined by the placement of curly braces {}.

Java objects have the different lifetimes.
When the reference vanishes at the end of the scope, the objects is still occupying memory.

Java has a garbage collector, which looks at all the objects that were created with “new” and figures out which ones are not being referenced anymore, then it releases the memory for those objects.

Creating new data types

The keyword class to mean “I am about to tell you what a men type of object looks like, another way, what establishes the type of an object.”

When you define a class, you can put two types of elements in it: fields and methods.

A field is an object of any type taht you can talk to via its reference, or a primitive type.
Each object keeps its own storage for its fields.
When a primitive date type is a field it is guaranteed to get a default value if you do not initialize it. This guarantee does not applied in method.

The term method is used to describe a named subroutine.
The fundamental parts of a method are the name, the arguments, the return type and the body.
The arguments list specifies what information you pass into the method, and the return type describes what comes back form the method.
What you must specify in the argument list are the types of the objects to pass in and the name to use for each one.
The method name and arguments list uniquely identify that method.
The act of calling a metnod is commonly referred to as “sending a message to an object”.

Name visibility

A problem in any programming language is the control of names, if you use a name in one module of the program, and another programmer uses the same name in another module, how do you distinguish one name from another?
Java introduce namespace to do with this situtation.
The Java creators want you to user your Internet domain name in reverse to specify you namespace.

Using other components

the keyword import tells the complier to bring in a package, which is a library of classes.

There is a certain library of classes that are automatically brought into every Java file: java.lang.

The static keyword

static means that particular field or method is not tied to any particular object instance of that class, so even if you have never created an object of that class you can call a static method or acess a static field.

There are two ways to refer to a static variable, through class name or object instance.

Comments

There are two types of comments in Java.
The first is the traditional C-style comment

/* mulitple lines comment */

The second comes from C++

// single line comment

JavaDoc

Possibly the biggest problem with documenting code has been maintaining that documentation.
If the documenttation and the code are separate, it becomes tedious to change the documentation every time you change the code.
The solution is simple: link the code to the documentation. The tool to extract the comments in Java is called Javadoc.

/** Java document */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值