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
- register
- stack
- heap
- constant storage
- 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
- boolean, char, byte, short, int, long, float,double,void
The corresponding reference types are
- ,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 type | Default value |
---|---|
boolean | false |
char | null |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.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
- /**/
- //
/* 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
- Class annotation
- Domain annotation
- 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
- @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
-
{@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
-
{@docRoot}
This tag generates a relative path to the document root directory for explicit hyperlinks to document tree pages
-
{@inheritDoc}
The tag from the basic related document in the most direct base class of the current class to the current document comment
-
@version
@version version-infomation
Displays information about the document
-
@author
@author author-information
Show author information
-
@since
Used to specify the JDK version
-
@param
@param parameter-name description
Parameter details for method
-
@return
@return description
Return value description
-
@throw
@throws fully-qualified-class-name description
-
@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
- Class name should be capitalized
- Using the hump naming method, each word is capitalized class all the colors of the rainbow