"java programming ideas - chapter 2 (everything is an object)"

everything is an object

2.1 Manipulating objects with references

Although everything is seen as an object, a single-manipulated identifier is actually a reference to an object. It's like a remote (reference) manipulating a TV (object). But the remote control and TV can exist independently.

You have a reference, but you don't necessarily need to have an object associated with it. For example, create a String reference: String s;, what is created here is just a reference, not an object. Using s at this point will generate a runtime exception because s is not associated with any object. It's safe to do this: create a referenced trial and initialize it.

2.2 You must create all objects

Once you create a new reference, you want it to be associated with a new object. Usually the new operator is used for this purpose.
Strings can be initialized with quoted text.

new的意思是给我一个新对象。
String s = new String("asdf");

Where to store:

  1. Register: Fastest. inside the cpu.
  2. Stack: located in general-purpose RAM.
  3. Heap: A general-purpose memory pool (also located in the RAM area) for storing java objects
  4. Constant Storage: Constant values ​​are usually stored directly inside the program code.
  5. Non-RAM Storage: Data that lives outside the program. Such as stream objects or persistent objects.

Basic data types do not need to create variables with new, but directly store "values" and use them on the stack, which is more efficient.

2.3 Never destroy objects

Scope: Scope determines the visibility and lifetime of variable names defined within it.
Object scope: The object created by new will remain as long as you need it. Unused objects are reclaimed by the GC.

2.4 Creating a new data type: class

If everything searches for objects, what determines the appearance and behavior of a certain class of objects?
class Name is used to describe the object.
There are two kinds of elements in a class: fields and methods.

If a member of a class is a primitive data type, even if it is not initialized, java will ensure that it gets a default value.
write picture description here

2.5 Methods, parameters and return values

The basic composition of a method: name, parameters, return value, method body.
The return value describes the value returned from the method after the method is called. No return value can be void identified.
The parameter list gives the type and name of the information passed to the method.
The method name and parameter list uniquely identify a method (also known as the method signature).

The method's parameter list is passed a reference to the object, and the reference must be of the correct type.

2.6 Building a Java program

Names can use reversed domain names to avoid duplicate name conflicts.
import is used to locate the class file location. The wildcard "*" can import all at once.

static keyword:
when applied to fields: only one storage space is allocated, and all objects share one.
When acting on a method: A method can be called directly without creating an object of the class.

2.7 Your first Java program

package com.thinkinjava.two;

import java.util.Date;

public class Test {

    public static void main(String[] args) {
        System.out.println("hello");
        System.out.println(new Date());
    }
}

2.8 Comments and embedded documentation

Multi-line comments:

/**
* Multi-line comments
* @author Administrator
*
*/

single line comment: // single line comment.
Annotated documentation is generated by javadoc.

2.9 Coding style

Camel case: AbCd

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325473662&siteId=291194637
Recommended