Java programming -- Sun Xin Java is no difficulty lesson2 bit operations and Object-Oriented Programming
1. bitwise operation
There are three shift operators in Java
Left shift: <
Shift to the right with a symbol:>
Unsigned shift to the right:> For more information, see any introduction to computer operating systems or computers.
The bit operation test program is as follows:
//***********************************
Class test {public static void main (string [] ARGs) {int I = 0 xffffffff; //-1 int c = I <2; int d = I> 2; // shifts the signed int e = I >>> 2; // shifts the unsigned system to the right. out. println (I); //-1 system. out. println (c); //-4 system. out. println (integer. tohexstring (c); // It is signed to shift left fffffc system. out. println (integer. tohexstring (d); // shifts the value to the right of ffffffff system. out. println (integer. tohexstring (e); // unsigned right shift 3 fffffff }}
//***********************************
2. object-oriented concepts
(1) Concepts
In short, object-oriented Programming (OOP) describes the interaction between objects. Object-Oriented Programming and process-oriented programmingDifferences: For procedural programming, the algorithm is first determined before the data structure is determined. For object-oriented programming, the data structure is first determined before the calculation. Process-oriented programmers are used to establishing data structures to store data and defining methods (functions) to operate data. Object-oriented programmers construct an object model, organize data and methods together.
(2) Object-oriented solution-rectangular circumference and Area Calculation
The rectangle test program is as follows:
//***********************************
class Rectangle{int w,l;int perimeter(){ return 2*(l+w);}int area(){ return l*w;}public static void main(String[] args) { Rectangle rect1=new Rectangle();Rectangle rect2=new Rectangle();rect1.l=10;rect1.w=5;rect2.l=4;rect2.w=6;System.out.println("the perimeter of rect1 is "+rect1.perimeter());//the perimeter of rect1 is 30System.out.println("the area of rect2 is "+rect2.area());//the area of rect2 is 24}}
//***********************************
3. Constructor
(1) default constructor. To view the function and member list of a class, run the following command:
F: \ Java \ javalesson \ lesson2> javap point
//***********************************
The obtained information is
//***********************************
Compiled from "point. Java"
Class Point {
Int X;
Int y;
Point (); // default constructor
Void output ();
Public static void main (Java. Lang. String []);
}
//***********************************
(2) default value provided by default constructor
Numeric Type 0
Boolean false
Char '\ 0'
Object null
(3) subclass cannot inherit the constructor of the parent class
The constructor of the subclass calls the constructor without parameters of the parent class by default. If the constructor without parameters does not exist in the parent class, an error occurs. The error message is as follows:
Animal. Java: 33: Error: unable to apply the constructor animal in the class animal to the given type;
Required: int, int
Found: No Parameter
Cause: the actual parameter list and format parameter list have different lengths.
(4) This must be the first statement to call other constructors of this class. Otherwise, the error message is as follows:
Point. Java: 9: error: the call to this must be the first statement in the constructor this );
4. Functions of the New Keyword
A. allocate memory space for objects
B. Call the object constructor
C. Return a reference for the object
5. Static static methods and static member variables
(1) Overview
Static methods and static variables belong to a class instead of class objects. The reference of static methods and static variables is directly referenced by the class name.
You cannot call non-static methods or reference non-static member variables in static methods. Otherwise, you can.
You can use the object OBJ of the class to call the static method (), for example, obj. Method ().
(2) An error occurs when a non-static member variable is accessed in a static member function. The error code and information are as follows:
Static void output ()
{
System. Out. println ("output called! ");
System. Out. println (X );
System. Out. println (y );
}
Point. Java: 29: Error: Unable to reference non-static variable X from Static Context
System. Out. println (X );
^
Point. Java: 30: Error: Unable to reference non-static variable Y from Static Context
System. Out. println (y );
(3) constant Initialization
A constant is defined by the final keyword and must be assigned an initial value. If it is non-static, it can be placed in the constructor,
To avoid copying the data of each object, the data can be declared as static and shared data, which must be initialized during definition.
Define final double pi. The error message is as follows when no Initialization is performed.
Point. Java: 14: error: the variable PI may not be initialized.
Static constant Declaration: static final double Pi = 3.1415926;
The point test procedure is as follows:
//***********************************
Class Point {int X, Y; // object variable static int Z; // class variable belongs to the class itself, an object that does not belong to a certain object // a constant must be assigned an initial value. If it is defined as non-static, it can be placed in the constructor. // It is defined as static. Share the data to avoid copying each object. At this time, it must be initialized during definition. static final double Pi = 3.1415926; // The static constant must be initialized again at point () {// Pi = 3.1415926; // OK the non-static constant can be initialized here this ); // call the constructor with parameters must be the first statement} Point (int A, int B) {// Pi = 3.1415926; // OK non-static constants can be initialized here X = A; y = B;} void output (INT X, int y) {This. X = x; this. y = y; // The internal member variable is hidden. The parameter is only visible. Therefore, this}/* // instance method void is used. Output () {system. out. println (x); system. out. println (y);} * // static method static void output () {system. out. println ("output called! "); System. out. println (x); // The error static method references the non-static member variable system. out. println (y); // error} */static void output () {system. out. println ("output called! "); System. out. println (z);} public static void main (string [] ARGs) {/* point pt; Pt = new point (); PT. X = 10; PT. y = 10; PT. output (); // output class member variable information * // * point pt; Pt = new point (3, 3); PT. output (5, 5); PT. output (); // result 3. Use this to solve the problem of duplicate names of function parameters and member variables/* point pt; Pt = new point (); PT. output (); // result without a parameter constructor by default, you can call a constructor with parameters * // * point pt; Pt = new point (); point pt2 = new point (); PT. X = 6; pt2.x = 7; system. out. println (pt. x); system. out. println (pt2.x); // The method has only one copy, but the data member has two different copies * // call point of the static void output () function. output (); // call Pt = new point (); PT. output (); // whether the object calls the static method is actually calling through the class * // The shared point pt1 = new point () of the static variable z (); point pt2 = new point (); pt1.z = 6; pt2.z = 7; // pt1 and pt2 share the Z variable system. out. println (pt1.z); // result 7 system. out. println (pt2.z); // result 7 */}}
//***********************************
6. Special variable this -- reference of the class itself
Further descriptions of instance methods and instance data members
The member methods called by all instances (objects) of a class have only one copy in the memory, although there may be multiple objects in the memory, the data member has a copy in the memory of each object of the class. This variable allows the same instance method to work for different objects. Every time an instance method is called, this variable is set to a specific class object that references the instance method. The method code is then associated with the specific data of the object represented by this.
7. Special variable super-reference of parent class member variables and functions
The special variable super provides access to the parent class.
You can use super to access hidden variables or override methods of the parent quilt class.
The first statement of each subclass constructor implicitly calls super (),
If the parent class does not have such constructor, an error will be reported during compilation.
The animal and fish test programs are as follows:
//***********************************
Class animal {int height, weight;/* // The constructor cannot inherit animal () {system. out. println ("animal construct"); // first call the base class constructor} */animal (int x, int y) {system. out. println ("animal construct"); // first call the base class constructor} void eat () {system. out. println ("Animal eat");} void sleep () {system. out. println ("Animal sleep");} void breath () {system. out. println ("Animal breath") ;}// JAVA does not allow many inherited class fish extends animal {int height; fish () {// super (); // call the constructor super (30, 30) without parameters. // call the constructor system with parameters in the parent class. out. println ("Fish construct"); // call the derived class constructor} void breath () // override of the function occurs between the parent class and the subclass. The function overload is within the class {// super. breath (); // call the parent class method super. height = 40; // call the parent class member variable system. out. println ("Fish bubble");} class Integration {static void FN (animal an) {. breath ();} public static void main (string [] ARGs) {// animal An = new animal (); fish FH = new fish (); // FH. height = 30; // FH. weight = 30; //. breath (); // FH. breath (); // polymorphism // If a subclass exists, the Child class is called. If not, the Child class/* animal an; fish FH = new fish (); An = FH; integration. FN (an); // actually call the breath Method of the subclass * // type judge animal An = new animal (30,40); fish FH = new fish (); // fish is an animal, not fisconditioned (an instanceof animal) {system. out. println ("Animal instance");} If (FH instanceof fish) {system. out. println ("Fish instance");} * // * animal An = new animal (30,40); fish FH = new fish (); An = FH; If (an instanceof fish) {system. out. println ("an is fish instance"); // although an is declared as animal, it is assigned as a fish reference an is fish instance} else {system. out. println ("an is animal instance");} If (FH instanceof animal) {system. out. println ("FH is animal instance"); // fish is animal FH is animal instance} else {system. out. println ("FH is fish instance ");}*/}}
//**********************************