Java programming thought notes 2 - Chapter 2: everything is an object

2. Everything is an object

Java is a more pure language than C + +.

C + + is too complex. Java is a pure object-oriented language. Everything in Java is an object.

2.1 manipulating objects by reference

Use pointers for manipulation in C language and C + +.

Java has no pointers, but because everything is an object, the identifier of the operation is actually a reference.

The author uses the TV remote control as an example. The remote control is a reference, and the TV is data. Even without the remote control, the TV still exists.

For example, String s = "asdf"

2.2 all objects must be created by you

Use the new keyword to achieve the purpose of reference

String s = new String("asdf")

2.2.1 where is it stored

  1. register
  2. stack
  3. heap
  4. constant storage
  5. Non RAM storage

2.2.2 special case: basic type

Not all Java objects are referenced

The following variables are not stored objects, but stored values.

Note that all basic data types are lowercase

  1. boolean, char, byte, short, int, long, float,double,void

The corresponding reference types are

  1. ,Boolean,Character,Byte,Short,Integer,Long,Float,Double,Void
char c = 'x' //Basic data type
Character ch = new Character('x') //Reference data type
//These two sentences are different

Java SE5 It can also be packaged automatically
Character ch = 'x'

BigInteger and BigDecimal are two classes of high-precision calculation, without corresponding basic types

2.2.3 arrays in Java

C and C + + have the problem of array memory leakage

One of the goals of Java is security, so it is more secure.

2.3 never destroy objects (garbage collection mechanism)

java has a garbage collection mechanism, which will automatically collect garbage. There is no need to manually delete memory like C or C + +.

2.3.1 scope

Scope refers to the scope of a variable, which is divided into local variables and global variables

{
    int x = 12
    {
        int q = 96 //Both x and q can be used
    }
    // q cannot be used, x can be used
}

be careful! The following code will report an error in java

{
    int x = 12
    {
        int x =96 //Will report an error
    }
}

2.3.2 scope of object

java objects do not have the same declaration cycle as basic types. When using new to create a java object, it can survive outside the scope.

{
    String s = new String("text file")
}
// Out of scope of s

Note! Even though the scope of s has disappeared, its String object is still in heap space. A garbage collection mechanism is required for recycling

2.4 create a new data type: Class

Use the class keyword to create a class

class AtypeName {  /*Object properties and methods*/}

AtypeName a = new ATypeName()

2.4.1 fields and methods

There are two types of elements in a class: fields (data members) and methods (member functions)

Field example

class DataOnly{
    int i;
    double d;
    boolean b;
}

DataOnly data = new DataOnly()
data.i = 47
data.d = 1.1

Default values for basic attribute fields

Basic typeDefault value
booleanfalse
charnull
byte0
short0
int0
long0L
float0.0f
double0.0d

2.5 methods, parameters and return values

Functions in C + + or C language are essentially the same as methods in Java.

Basic form of method

Return type method name(parameter list...){
    Method body
}

In java, methods can only be created as part of a class

Class name.Method name()
//perhaps
 Object name.Method name()

2.5.1 parameter list

In addition to the basic type, all passed in the method are references

// Return int type
int sname(String s){
    return s.length() //When a return is encountered, the method ends
}

// void has no return value
void nothing(){
    
}

2.6 building a Java program

2.6.1 name visibility, URL reversal

java uses the method of class name + method name to prevent name duplication.

In order to prevent name duplication, it is customary to use URL inversion to determine the method name

For example, my domain name is mindview net

Then I define the tool class nine tail net mindview. utility. flobles

2.6.2 using other to build import

Use import to reduce the length of writing

import java.util.ArrayList
import java.util.* //Import all at once

2.6.3 static keyword

static means that the property and method are only related to the class, not the object

It is also called class attribute and class method defined by static

Static static property

class StaticTest{
    static int i = 47 ; //Define a static property
}

StaticTest t1 = new StaticTest()
StaticTest t2 = new StaticTest()
    
System.out.println(t1.i) ; //Output 47
System.out.println(t2.i) ; // Output 47
System.out.println(StaticTest.i) ;//Output 47
class StaticTest{
    static int i = 47 ; //Define a static property
	static void increment(){ //Define a static method
        StaticTest.i ++   ; 
    }
}

2.7 your first Java program

//The program imports Java by default lang
import java.util.* //Import other classes

public class HelloDate{
    public static void main(String[] args){
        System.out.println("Hello world");
        System.out.println(new Date()); //Print current time
    }
    
}

2.7.1 compilation and operation

You need to install jdk and configure environment variables. java and javac can be used in cmd

In cmd, enter javac hellodate Java to compile

Enter java HelloDate in cmd to run the program

2.8 notes and embedded documents

There are two annotation styles in java

  1. /**/
  2. //
/*
This is a multiline comment
 This is a multiline comment
 This is a multiline comment
*/

// This is a single line comment


2.8.1 annotation documents

Through special annotation syntax, documents and classes can be written in the same folder

javadoc is a tool for extracting comments, and it is also installed by jdk

The output of javadoc is an HTML file

2.8.2 javadoc documentation

There are three types of annotation documents

  1. Class annotation
  2. Domain annotation
  3. Method notes
//: object/Documentation1.java
/** A class comment Class annotation */
public class Documentation1{
    /** Domain annotation */
    public int i;
    /** Method notes */
    public void f() { }
}

**Note: * * javadoc can only annotate documents for public and protected members

2.8.3 embedded HTML

javadoc transmits HTML commands through the generated HTML documents, making full use of HTML

2.8.4 some label examples

  1. @See refers to other classes and links to documents of other classes through @ see
@see classname
@see fully-qualified-classname
@see fully-qualified-classname#method-name
  1. {@link package.class#member label}

    This tag is very similar to @ see, except that it is used in the line and uses "label" as a hyperlink

  2. {@docRoot}

    This tag generates a relative path to the document root directory for explicit hyperlinks to document tree pages

  3. {@inheritDoc}

    The tag from the basic related document in the most direct base class of the current class to the current document comment

  4. @version

    @version version-infomation

    Displays information about the document

  5. @author

    @author author-information

    Show author information

  6. @since

    Used to specify the JDK version

  7. @param

    @param parameter-name description

    Parameter details for method

  8. @return

    @return description

    Return value description

  9. @throw

    @throws fully-qualified-class-name description

  10. @deprecated

    Old features are replaced by new features, and warnings will appear when compiling

2.8.5 document examples

//: object/HelloDate.java
import java.util.*
    
/** Describe this class, which is the first class of the project
 *  Shows the weather of the day
 *  @author Half Zhen
 *  @author www.baidu.com
 *  @version 4.0
 */
public class HelloDate{
    /** This is a main method
     *  @param args Parameters passed in at runtime
     *  @throws exception No exception thrown
     */
    public static void main(String[] args){
        System.out.println("Hello world")
        System.out.println(new Date())
        
    }/* Output: (55% match) Because the results of each run are different, there is only 55% correlation
    Hello world
    Tue June 21 15: 25: 36 MDT 2021
    *///
    
}

2.9 coding style

  1. Class name should be capitalized
  2. Using the hump naming method, each word is capitalized class all the colors of the rainbow

Keywords: Java Programming

Added by romzer on Thu, 20 Jan 2022 14:22:59 +0200