Thinkinginjava Learning Notes 02_ objects

Source: Internet
Author: User
Tags class definition

Object

1. The object is manipulated by a reference, but the object in Java is passed by value, which can basically be considered as the object itself in the operation, and still be remembered as a reference to the object entity in the internal structure, such as: String s = "ABCD"; In this statement, S is not a String object: "ABCD" itself, but instead represents a reference to a string object, which is a reference to the object s, but when you use S, you pass the object "ABCD", which is the value of the reference; Note that only double quotes are used;

2. Create a new object using new (in addition to the above mentioned string can be created directly with a string of quoted text), such as: New String ("ABCD"); and a reference to the object type is used to name the object (otherwise the object will have no persistence, because an object with a reference count of 0 is reclaimed by the garbage collection mechanism), such as: String s =?? New String ("ABCD");

When an object is created, the object reference is stored in the stack, and the entity of the object is stored in the heap, and the content in the heap has a flexible life cycle and storage allocation, but requires additional storage allocation and cleanup time;

3. In addition to object references, the basic types in Java are also stored in the stack (stack), when the base type is created, the variable ontology is created directly, rather than the reference creation, the variable exists in the stack, the use of high efficiency, such as: int a = 123; Create a shape variable the contents of the 123,a is 123; Each base type has a corresponding wrapper type, and when you use new to create a variable of the wrapper type, the same as the object creation, such as: int a = new Integer (123), and A is a reference to the shaping variable 123. 123 is stored in the heap, and a is stored in the stack as a 123 reference;

Special, string is not a basic type, because the length of the string is variable, and all the basic types in Java, the length is fixed, and does not change with the hardware and operating system;

The wrapper types in Java that support two high-precision operations: BigInteger and BigDecimal, which are used without length limits, automatically extended when used, and, accordingly, slower to operate (the high-precision calculations used by ACM students are probably the principle of implementation) The BigDecimal class uses a decimal floating-point number, and there is no problem of precision;

The basic types in Java are:

Shaping: Byte/short/int/long; The lengths were: 1/2/4/8 bytes;

The shaping default type is int, and when you use a long type, you need to add l at the end;

Floating-point type: float/double; length: 4/8 bytes;

Floating-point type is double by default, and when using float type, you need to add f at the end;

Logic: boolean; Length: 1/8 bytes, i.e. one bit;

Character type: char; Length: 2 bytes

The corresponding wrapper type name is the first letter capitalized word of the same name;

4. The scope of the object is defined by curly braces: {}, and Java does not support the practice of using a large scope to hide variables;

5. Create class using the Class keyword: class Atypename {/*pass*/};

? ? After defining a class, you can set two types of elements in the class: field (Data), method, field can be a basic type (unnecessary initialization, Java will ensure that there is a default value, will be in memory 0), or a reference to a specific object (must be a specific object reference) Each object has space to store the fields, and normal fields cannot be shared between objects. Access to the members of the object through the dot number;

In a local variable (not a field of a class), Java does not initialize the base type; Using an uninitialized variable is an error in Java;

In general naming, the first letter of each word in the class is capitalized;

6. Methods

There are no separate functions in Java, methods can only be created as part of a class, and can only be accessed through objects (except static methods, etc.), using the following form when defining a method:

ReturnType methodName (/* Argument list */) {
? ? /*method body*/
}

When invoking a method, it is necessary to pass the required arguments, specify what type of object the arguments require for the parameter list, specify the type of each object to be passed, and the name, the reference to the object that is passed when the method call is made, and the type of the reference must be correct; You can use an object to store the return value. The type of the object must be compatible with the return value type (not necessarily the same);

method is generally named when the first letter is lowercase;

Guess: The methods in Java should exist in the class space, that is, all objects share the same method entity;

7. Static Data and methods

Declare a static data or method through the static keyword, not only the instantiated object can be accessed, it can also be accessed directly using the class name, regardless of static data and methods, all objects share the same entity, when an object changes static data, other objects access the static data also changes;

8. Composition of Java programs

Each Java program must have a class and file name that is the same, and there is a main () method in the class:

public static main (string[] args) {

}

The Java program uses JAVAC to compile it into a bytecode (. Class) and then interprets the bytecode using the Java command, which is implemented in a first-compiled, post-interpretation manner;

The program is compiled into bytecode via the Javac command and then run through the Java command, before which you first need to configure the environment variables (for example, OSX):

Export Java_home=/system/library/frameworks/javavm.framework/versions/currentjdk/home
Export path= $JAVA _home/bin: $PATH
Export classpath= $PWD: $JAVA _home/lib/tools.jar: $JAVA _home/lib/dt.jar

9. Annotate and interpret documents

C language style annotations can be used in Java;

Java has a set of document maintenance tools: Javadoc;javadoc can find special comment tags in the program, not only to parse the information tagged by these tags, but also to extract the class name or method name of the comment. In addition to the annotated content, Java also uses the document label at the beginning of the @, Javadoc recognizes these annotations and tags, and automatically generates the program documentation;

Javadoc a total of three documents: class, Domain, method, all documents before the definition of the corresponding content, such as class comments just before the class definition, Javadoc only for public and protected document comments, and ignore private;

Javadoc generates a series of HTML files, which open a Web page named classname.html to see the resulting document;

A label that feels more important: @param; @return; @throws;

Thinkinginjava Learning Notes 02_ objects

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.