Java programming ideas - everything is an object Chapter 2

2.1 with the manipulation object reference

  1. Each programming language has its own way of operating memory elements.
  2. References can exist independently, that is, you have a reference does not necessarily need to have an object associated with it.
  3. A Java language features: strings can be initialized with quoted text. Typically, you must use a more general object initialization method.

2.2 You must create all the objects

  1. Where to store
    a register: internal to the processor, Java can not directly operate.
    . Stack B: in the general RAM, by the stack pointer , fast, Java's object references stored here.
    . heap C: also located in RAM, for storing all Java objects. The compiler does not need to know the data stored on the heap of survival. Flexible operation, but the storage allocated when you need more time.
    d constants are stored: generally constant value is directly stored in the internal code. It can be stored in ROM.
    Non e RAM memory: If the data is fully viable outside the program, it can not any control program, may also be present in the program is not running. Stream object : Object converted into a byte stream, typically is sent to another machine; persistent object : the object is stored on disk.
  2. Exception: basic types
    . A direct storage type base "value", and placed in the stack, and therefore more efficient.
    b. Java to be determined for each basic type size occupied storage space. But they will not change the size of random hardware architecture changes.
    c. All numeric types are signed, so do not look for unsigned numeric type.
    d. Java automatic packaging functions will automatically convert basic data types for the packaging type.
    . e precision digital : Java provides two classes for high precision calculations: a BigInteger and the BigDecimal . These two classes of methods, similar to the basic operation of the operation can perform the type provided.
  3. Java arrays in
    a. One of the main goals of Java is safety.
    b. Java will ensure that the array is initialized and can not be accessed outside of its range. This range checking, based on each array subscript checking a small amount of memory overhead and run-time expense. But the resulting return was a safety and efficiency, and therefore the price paid is worth it (and Java sometimes can optimize these operations).
    c. When the object is to create an array, the array when creating the application, automatically initialized to null , the basic type are initialized to 0 .

2.3 Never destroy the object

  1. Scope
    a. Determine the scope of its internal variable names defined in the visibility and lifecycle .
    b.Java allowed to hide in the larger scope of variables, the compiler will report variables have been defined.
  2. The object scope
    a. Java objects and primitives do not have the same life cycle. When a new When you create a Java object, it can survive outside the scope.
    b. Java has a garbage collector, to monitor all the objects created with new, and which objects are no longer referenced identified. Then release the memory space of these objects, so that other new objects to use.

2.4 Creating new data types: class

  1. class is used to create the type, which determines the appearance and behavior of a class object.
  2. : Two types of elements disposed in a class field (data members) and methods (member functions).
    Basic member defaults, but it does not apply to " local " variables. Variable must be initialized before use, otherwise the Java compiler will report errors.

2.5 methods, parameters and return values

  1. Java method determines what an object is capable of receiving messages. The basic components of the method comprising: the name, arguments, return values, and methods thereof.
  2. Methods in Java can only be created as part of a class. The method can only be invoked through the object, and the object must be able to call this method of execution.
  3. List of parameters: the parameter list passed the object is referenced , except for basic data types.
  4. return usage, there is no need to leave until the end of the method can return anywhere.

2.6 Building a Java program

  1. Name visibility
    a. In order to generate a library name will not be confused with other names, Java programmers to turn the designer wants to use your own Internet domain name. After the reverse domain name, it is used to represent the period divided subdirectories.
  2. Other components using
    a. To solve this problem class name conflicts, we must eliminate all possible confusion. To achieve this purpose, you can use the import keyword to tell the compiler exactly what you want class yes.
  3. static keyword
    a. In one case, just for a specific domain assigned a single storage space, without regard to how much you want to create an object, do not even create any object. Another scenario is desirable that a method does not contain any of its associated object class together. In other words, even if no object is created, you can call this method.
    b. By static meet the needs of both keywords. When you declare a thing static, they think with this field or method will not be associated with a class of it that any object instances together.
    fields or methods c. static created, stored only once, all objects share a storage space.

2.7 Your first Java program

import java.util.*;
public class HelloDate{
    public static void main(String[] args){  //String[] args用来存储命令行参数
        System.out.println("Hello "+new Date());
    }
}

System of a number of other methods

public class ShowProperties {
    //main()的第一行将显示从运行程序的系统中获取的所有“属性”,因此它可以向你提供环境信息
    public static void main(String[] args) {
        System.getProperties().list(System.out);
        System.out.println(System.getProperty("user.name"));
        System.out.println(
                System.getProperty("java.library.path")
        );
    }
}
  1. Compile and run
javac HelloDate.java   //编译
java HelloDate  //运行

2.8 Notes and embedded documents

Two types of comments

/*我是注释*/
//我是注释
  1. Annotate documents
    a. Use javadoc tool to produce documents
  2. Syntax
    a. All javadoc command can only be in the "/ **" comments appear, as usual, also the end of the comment "* /."
    b. javadoc documentation comments to only public and protected members.
/** 我是注释*/
  1. Embedded HTML
    A All types of comment documentation - class, fields and methods - both support embedded HTML
  2. Some examples of labels

2.9 Coding Style

  1. The first letter of the class name uppercase
  2. If the class name consists of several words, and then put them together, in which the first letter of each word in uppercase.
  3. Hump ​​style
  4. Methods, fields, and object reference names, as recognized style and class style. Only the first character of an identifier in lowercase.

2.10 summary

  In this chapter, we have been in contact with quite a bit of Java programming knowledge on how to write a simple program. In addition, the Java language and its basic idea has also been a general awareness.

Published 44 original articles · won praise 20 · views 5308

Guess you like

Origin blog.csdn.net/qq_42396168/article/details/104952152