Java Note - Everything is an object
2015-06-07 07:31
435 查看
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
4000
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.
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
The second comes from C++
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 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
4000
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 */
相关文章推荐
- [Object c]_[初级]_[NSString常用方法的总结]
- Objective-C 常用字符串操作
- Objective-C 文件操作
- [C++/object c]_[初级]_[std::map容器的使用总结和NSDictionary词典使用总结]
- 使用[NSKeyedUnarchiver unarchiveObjectWithData:data]程序crash
- 基于json-lib.jar包 创建JSONObject的四个常用方法
- ComFailException:can't co-create object.错误的猜测的一种解决方法
- WaitForMultipleObjects使用详解
- 十分钟让你明白Objective-C的语法(和Java、C++的对比)
- Different ways to create an object in Java
- 浏览器对象模型(Browser Object Model)
- ArcGIS操作——object name is not a regular identifier for underlaying DBMS[ArcGIS企业级数据导入错误]
- 轻松学习之一--Objective-C消息转发
- Object.prototype.toString.call() 区分对象类型
- Objective-C的入门(一)
- Object-c分类和协议
- Objective-c的内存管理MRC与ARC
- Delphi 中的 procedure of object .
- object-c面向对象1
- Objective-C 2.0属性(Property) retain,copy,assign,copy