Thinking in Java-----reading note (2)

Source: Internet
Author: User
Tags deprecated getmessage

# Thinking in Java 4th
# reading Note
# Victor
# 2016.02.16

Chapter 2 It's all about the object.

2.1 Manipulating objects with handles
Use a handle as an identifier to point to an object. But owning a handle does not mean that an object is connected to it.
For example, create a string handle: string S;
At this point, the handle is created, not the object. If you send a message to S, you get an error. Therefore, it is safer to create a handle that is initialized anyway.

2.2 Creating objects
You typically use the New keyword to connect a handle to an object when you create it. For example: string s = new string ("Hello world!");

2.2.1 Save location
1. Registers. Registers are generally assigned by the compiler, and we do not have direct control over them.
2. Stack. resides in the normal Ram area, but can be directly supported by the stack pointer. Pointer down and up creates and frees memory (push or pop), and the base address of the stack must be greater than or equal to the top of the stack. The Java object handle is stored in the stack, but the object is not placed in it.
3. Heap. A general-purpose memory pool (also in the Ram area) that holds Java objects. Unlike stacks, the compiler does not need to know how much storage is allocated from the heap or how long the data is stored. Storing data in heaps is more flexible.
4. Static storage. Static storage data will be in a fixed location.
5. Constant storage.
6. Non-RAM storage.

2.2.2 Basic Types
Instead of creating a variable with new, the base type creates an "automatic variable" that is not a handle and is stored on the stack.
Basic data Type wrapper class
BYTE byte
Boolean Boolean
Short Short
Char Character
int Integer
Long Long
float float
Double Double
The wrapper class facilitates the manipulation of the object and contains the relevant action methods.
Java's two classes: BigInteger and BigDecimal, are used for high-precision calculations, but they do not have a corresponding basic type, which are "wrapper classes".

2.2.3 an array of Java
When you create an array of objects, you are actually creating an array of handles. Each handle is automatically initialized to a special value with its own keyword: null. Before you use it, you must assign an object to each handle, or you will get an error. Java can also create an array of primitive types, and the compiler initializes it.

2.3 Elimination of variables
Java will help us do the work of variable cleanup without having to consider the time of the variable's existence.

2.3.1 Scope
{
int x = 12;
{
int y = 13;
}
}
The scope in Java is determined by the position of the curly braces.
Scoped variables that can be used only before the end of the scope.
It is worth mentioning that Java cannot write code like this:
{
int x = 12;
{
int x =123;/*illegal*/
}
}

Scope of the 2.3.2 object
In Java, objects created with new are preserved as long as they are willing. At the same time, Java has a "garbage collector" that finds all objects created with new and identifies which ones are no longer referenced, and then automatically frees the memory occupied by idle objects for the use of new objects.

Class 2.4
New class:
Class Atypename {/*class body is here */}
Instantiating an object:
Atypename a = new Atypename ();

2.4.1 fields and methods
When defining a class, you can set two elements in a class: data members (fields) and member functions (methods).
A field can be an object, or it can be a base type. If a base type belongs to a class member, they are not explicitly initialized or can get a default value, but this does not apply to local variables.

2.5 methods, arguments and return values
Structure of the method:
return type method Name (/* argument list */) {/* Method body */}

A generic method can only be called through an object, such as:
Example e = new Example ();
E.getdetails ();
Static methods can be called through the class:
Example.getmessage ();//GetMessage () is a static method

2.5.1 Independent variables
In addition to the base type, the arguments actually pass through the handle of the object.

2.6 Building Java Programs
Java classes are stored in their own namespaces (package). Each class of a file gets a unique identifier.

2.6.1 using other components
Import Java.util.vector;
# vector class under Import util

Import java.util.*;
# import all classes under Util

2.6.2 Static keyword
Once something is set to static, the data or method will not be associated with any object instances of that class. So although an object of that class has never been created, you can still invoke a static method, or access some static data.
Before that, for non-static data and methods, we had to create an object and use that object to access the data or method. This is because non-static data and methods must be aware of the specific objects they operate on.
It is worth mentioning, however, that the static method cannot invoke other object members until the object is created.

Examples of static variables:
Class statictest{
Static int i = 47;
}
Statictest st1 = new Statictest ();
Statictest st2 = new Statictest ();

Although we have created two objects here, STATICTEST.I still has only one, that is, the two objects share the same i.
Both st1.i and st2.i have the same value of 47, because they refer to the same memory area. So, if you execute STATICTEST.I + +, you can get st1.i = st2.i =48.

Examples of static methods:
Class staticfun{
Static void incr{statictest.i++;}
}
A static method can be called either through an object or directly from its class.

Attention:
One of the important uses of Static is to help us invoke that method without having to create an object. As you'll see later, this is critical-especially when defining a program to run the Entry method main ().

2.7 Commenting and embedding documents
Java has two types of annotations.
The first is inherited from C + +, and when compiled, everything between/* and/* is ignored:
/* This is
* A section of notes
* It spans multiple lines
*/
The second type of comment is a single-line comment://This backwards is a comment (single line comment)

2.7.1 Notes Document
The tool for extracting annotations is called Javadoc. It uses some techniques from the Java compiler to find special comment tags for our placement program. It extracts not only the information indicated by these tokens, but also the class name or method name adjacent to the comment. This allows us to generate very professional program documentation with the lightest amount of work.
The Javadoc output is an HTML file that can be viewed in its own Web browser. This tool allows us to create and manage individual source files, and to generate useful documents vividly. Because of the jvadoc, we were able to create documents in a standard way. And because it is very convenient, we can easily get all the documentation for the Java library.

2.7.2 Specific syntax
All Javadoc commands can only appear in the "/**" comment. But as often as peace, annotations end in a "*/". There are two main ways of
To use Javadoc: embedded HTML, or use "Document Tag". where "Document tags" (doc tags) are some that begin with "@"
command, placed at the beginning of the comment line (but the leading "*" is ignored).
There are three types of annotation documents that correspond to the elements that follow the comment: class, variable, or method. That is, a class comment is just before a class definition; the variable comment is just before the variable definition, and a method definition is just before a method definition. As shown in the following simple example:

/** A class comment */
public class Doctest {
/** A variable comment */
public int i;
/** a method Comment */
public void F () {}
}

Note Javadoc can only handle commented documents for "public" and "protected" (protected) members. Comments for "private" and "friendly" (friendly) members are ignored, and we don't see any output (you can also include private members with the-private tag). This makes sense because only public and protected members can be used outside of the file. However, all class annotations will be included in the output results.

2.7.3 Embedding HTML
You can use HTML tags in document comments to format plain text to make it more aesthetically pleasing.

2.7.4 @see: Referencing other classes
All three types of comment documents can contain the @see tag, which allows us to reference documents in other classes. For this tag, Javadoc generates the appropriate HTML and links it directly to other documents. The format is as follows:

@see class Name
@see Full class name
@see Full class name # method name

2.7.5 Class Document tags
Along with embedding HTML and @see references, class documents can also include markup for version information and author names. The class document can also be used for "interface" purposes.

1. @version
The format is as follows:
@version Version Information
Where "version information" represents any information that is appropriate for use as a version description. If the "-version" tag is used on the Javadoc command line, the published information is extracted from the generated HTML document.

2. @author
The format is as follows:
@author Author Information
The "Author information" includes your name, e-mail address, or any other appropriate information. If the "-author" tag is used on the Javadoc command line, author information is extracted specifically from the generated HTML document.

2.7.6 Variable Document tags
The variable document can only include embedded HTML and @see references.

2.7.7 Method Document Tags
In addition to embedding HTML and @see references, the method also allows the use of document tags for parameters, return values, and violations.

1. @param
The format is as follows:
@param parameter Name Description
where "parameter name" refers to an identifier within a parameter list, and "description" represents some descriptive text that can be extended to subsequent lines. Once a new document tag is encountered, the previous description is considered to be complete. You can use any number of instructions, one for each parameter.

2. @return
The format is as follows:
@return Description
Where "description" refers to the meaning of the return value. It can be extended to a later line.

3. @exception
An "exception" (Exception) is a special object that can be "thrown out" if a method fails. When invoking a method, although only one exception object appears, some special methods may produce any number of different types of exceptions. All of these exceptions need to be explained. Therefore, the format of the exception token is as follows:
Exception Full class name description
Where the "full class name" explicitly specifies the name of an exception class, which is defined somewhere else. The "description" tells us why this particular type of violation occurs in a method call.

4. @deprecated
This flag is used to indicate that some of the old features have been superseded by the new features that have been improved. The purpose of this tag is to suggest that users no longer have to use a particular feature, as it may be discarded in future revisions. If you mark a method as @deprecated, you will receive a compiler warning when you use the method.

Thinking in Java-----reading note (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.