Java programming-sun Xin Java is no difficulty lesson5 common Java packages, data types and reference types, function parameter transfer, cloning Mechanism
Highlights of this section:
1. Common Java packages
2. "=" and "equals" Usage
3. Basic data types and reference types
4. String and stringbuffer
5. Clone an object)
Details:
1. Common Java packages
Java. Applet: Contains classes used to create Java small applications.
Java. AWT: Contains classes used to write graphic interface (GUI) applications unrelated to the platform.
Java. IO: Contains classes used for input/output (I/O) processing.
Java. lang: contains basic and core classes of some java languages, such as string, math, integer, system, and runtime. It provides common functions. All classes in this package are implicitly imported.
Java.net: Contains classes used to establish network connections. java. Io can be used simultaneously to complete network-related read/write.
Java. util: contains some Utility Classes and data structure classes.
2. "=" and "equals" usage -- Method for Determining the equality between variables and object values
(1) Reference Type
In Java, Boolean, byte, short, Int, long, Char, float, and double are basic data types, and the rest are reference types.
(2) Determination of equality between variables and object values
"=" Is to compare whether the values of the two variables are equal, and "equals" is to compare whether the content of the objects represented by the two object variables is equal.
Test code:
// Determination of equality between variables and object values
String str1 = new string ("ABC ");
String str2 = new string ("ABC ");
String str3 = new string ("ABC ");
If (str1 = str2)
System. Out. println ("str1 = str2 ");
Else
System. Out. println ("str1! = Str2 "); // str1! = Str2 = compare the values of two variables
If (str1 = str3)
System. Out. println ("str1 = str3 ");
Else
System. Out. println ("str1! = Str3 "); // str1! = Str3 = compare the values of two variables
If (str1.equals (str2 ))
System. Out. println ("str1 = str2"); // str1 = str2 equals compares the objects referenced by the two variables for Equality
Else
System. Out. println ("str1! = Str2 ");
3. Basic data types and reference types-Memory Model Analysis
(1) memory allocation for basic data types
Memory Allocation Process:
A. Declare the referenced variable, allocate the stack memory, and the referenced variable value is null.
B. The new operator allocates space in the heap memory and returns the heap memory address.
C. Assign the heap memory address to the referenced variable
Memory allocation for basic data types is as follows:
Statement num = NULL; disconnect from the original array, for example:
(2) Object Memory Allocation
For the allocation process, see:
(3) Memory Model for function calling
In Java, parameters are all transmitted as values.
For the basic data type, the data is copied; for the reference type, the referenced copy is passed.
A. Basic data type data exchange
Data exchange of the basic data type does not actually exchange two data, because when the parameter is passed, a copy of the original data will be copied in the function, instead of operating the original data.
// The test code and memory model are shown in the following figure:
B. Data Exchange of reference types
The data exchange of the reference type will actually implement two data exchanges, because it is actually an operation on the same object.
// The test code and memory model are shown in the following figure:
4. Features and usage of string and stringbuffer
(1) string "+" and "+ =" are the only operators that are overloaded in Java. in Java, programmers are not allowed to overload operators.
The string can be added to any type of variable.
(2) string is a constant, which can be understood as assigning the address of a heap object to this string object for reference, therefore, the value of the string class will retain the address reference of the object and cannot be changed.Why is this mechanism used?
This mechanism can avoid statements like:
String str1 = new string ("ABC"); // allocate heap memory and assign the address to the referenced variable str1
Str1 = "BCD"; // re-allocate heap memory
This will cause a waste of memory. Therefore, Java can replace the stringbuffer class to avoid modifying the content in the string. Use the stringbuffer class in a large number of programs that process strings.
(3) For more operations such as insert and delete, see the API help documentation.
// String test code
// String usage
String str1 = new string ("ABC ");
Int I = 3;
Float F = 1.5f;
Char CH = 'F ';
Boolean B = false;
// System. Out. println (str1 + I + F + CH + B); // The OK string class allows the addition of any types abc31.5ffalse
Stringbuffer sb = new stringbuffer ();
SB. append (str1). append (I). append (f). append (CH). append (B );
// System. Out. println (sb. tostring (); // OK
SB. Delete (); // 4-7error when an index is calculated, it contains the previous index but does not contain the last index.
SB. insert (4, "inserttest"); // insert a string
System. Out. println (SB); // OK automatically calls tostring
5. Clone mechanism-solves Data Protection of reference type
(1) cloning Mechanism
When passing parameters in a function, passing parameters of the reference type may cause changes to the original data. To avoid changes to the original data, the clone mechanism is used. The clone mechanism copies a copy of the original object without directly performing operations on the original data.
(2) clone Mechanism
The clone mechanism must implement the cloneable interface in the derived class, overwrite the clone () method of the base class, declare it as public, and call Super. Clone () in the clone () method of the derived class ().
Why call Super. Clone ()?
During runtime, the object clone () identifies the object you want to copy, allocates space for the object, and copies the object, copy the content of the original object to the bucket of the new object one by one.
// Test code
Class student implements cloneable
{
Int age;
String name;
Student (string name, int age)
{
This. Name = Name;
This. Age = age;
}
// Copy at a shortest level
Public object clone ()
{
Object o = NULL;
Try
{
O = super. Clone ();
}
Catch (clonenotsupportedexception E)
{
System. Out. println (E. tostring ());
}
Return O;
}
}
// In the main function
// Object cloning
Student stu1 = new student ("zhangsan", 18 );
Student stu2 = (student) stu1.clone (); // the data of the original object is not affected after cloning.
Stu2.name = "Lisi ";
Stu2.age = 20 ;//Data of stu1 will not be modified
System. Out. println ("stu1.name =" + stu1.name + "stu1.age =" + stu1.age );
// Running result
Stu1.name = zhangsan stu1.age = 18
(3) clone mechanism-deep-level cloning
// Test code
Class aggregsor
{
Int age;
String name;
Partition sor (string name, int age)
{
This. Name = Name;
This. Age = age;
}
}
Class student implements cloneable
{
Int age;
String name;
Extends sor P; // reference type
Student (string name, int age, partition sor P)
{
This. Name = Name;
This. Age = age;
This. P = P;
}
// Copy at a shortest level
Public object clone ()
{
Object o = NULL;
Try
{
O = super. Clone ();
}
Catch (clonenotsupportedexception E)
{
System. Out. println (E. tostring ());
}
Return O;
}
}
// In the main function
Partition sor P = new partition sor ("wangwu", 50 );
Student stu1 = new student ("zhangsan", 18, P );
Student stu2 = (student) stu1.clone ();
Stu2.p. Name = "zhaoer ";
Stu2.p. Age = 44; // The data of stu1.p will be modified
System. Out. println ("stu1.p. Name =" + stu1.p. Name + "stu1.p. Age =" + stu1.p. Age );
// Running result
Stu1.p. Name = zhaoer stu1.p. Age = 44
It indicates that the data of stu1 of the original object is still modified during cloning. Solution: implement the cloneable interface and clone method by using the plusor.
// Modified test code
Class Partition sor implements cloneable
{
Int age;
String name;
Partition sor (string name, int age)
{
This. Name = Name;
This. Age = age;
}
// Supports cloning
Public object clone ()
{
Object o = NULL;
Try
{
O = super. Clone (); // The Copied object must be recognized at runtime instead of the base class object.
}
Catch (clonenotsupportedexception E)
{
System. Out. println (E. tostring ());
}
Return O;
}
}
Class student implements cloneable
{
Int age;
String name;
Extends sor P; // reference type
Student (string name, int age, partition sor P)
{
This. Name = Name;
This. Age = age;
This. P = P;
}
// Deep copy
Public object clone ()
{
Student s = NULL;
Try
{
S = (student) Super. Clone ();
}
Catch (clonenotsupportedexception E)
{
System. Out. println (E. tostring ());
}
S. P = (partition SOR) S. P. Clone (); // copy the member variable of the reference type
Return S;
}
}
// In the main function
Partition sor P = new partition sor ("wangwu", 50 );
Student stu1 = new student ("zhangsan", 18, P );
Student stu2 = (student) stu1.clone ();
Stu2.p. Name = "zhaoer ";
Stu2.p. Age = 44 ;//Data of stu1.p will not be modified
System. Out. println ("stu1.p. Name =" + stu1.p. Name + "stu1.p. Age =" + stu1.p. Age );
// Running result
Stu1.p. Name = wangwu stu1.p. Age = 50
This indicates that stu1's data is not modified.
6. command line parameters in the main function
// Obtain command line parameters
For (INT I = 0; I <args. length; I ++)
System. Out. println (ARGs [I]);