Dark Horse Programmer--java Basic Learning Note 6

Source: Internet
Author: User

Dark Horse Programmer--java Basic Learning Note 6I. Summary of the contents of the notes:Object-oriented (Overview & example), relationships between classes and objects, class and object representation & details, memory representation of objects, differences between member variables and local variables, class type parameters, anonymous objects, basic data type parameter pass plots, reference data type parameter pass plots, encapsulation & Code examples, object-oriented (encapsulation & thought), two-dimensional arrays & definition methods & Memory plots & another way of defining & scenarios. second, the introduction of common content:1. Object-oriented features:A. Encapsulation: The specific action is encapsulated, convenient to call, improve the code reusability and security. B. Inheritance: After inheriting a class, you can invoke the parent class's methods directly, or override the parent class's methods to improve the functionality of the class. c. Polymorphism: A class exists in many forms. For example, the equilateral right triangle, both belong to right triangle, but also belong to isosceles triangle, but refers to the same thing. 2. Class-To-object relationships:The description of things in Java is embodied in the form of classes, which are abstract, conceptual definitions of specific things. An object is an individual that has a real existence for that kind of thing. 3. The difference between a member variable and a local variable:member variables:A. Member variables are defined in the class and can be accessed throughout the class. B. Member variables are created as the object is created, disappearing as the object disappears and exists in the object'sheap memory. c. Member variables have default initialization values. Local Variables:A. Local variables are defined only within the local scope, such as within a function, within a statement, and only in the area to which they belong. B. Local variables exist in the stack memory, the scope of the action ends, and the variable space is automatically released. c. Local variables do not have default initialization values. 4.new Keywords:as long as the entity defined by the new operator opens up a new space in the heap memory,and each object has a property of its own, so a single-instance design pattern appears later onmode to ensure that only one entity object exists in memory to improve performance and save memory space. 5. First class? Or is there an object? In Java, you create an object by using class new, so there are classes first, then objects. The default value for the 6.double data type is the default value of the 0.0,int data type is 0, and the default value for the reference data type is null. Iii. Classic Examples and explanations:1. Definition of two-D arrays:
Package com.date6;/** * Understand the pattern of this two-dimensional array  */public class Test1 {public static void main (string[] args) {int[][] arr = new int [][]{{1,2,3},{4,5,6}};for (int i=0;i<2;i++) {for (int j=0;j<3;j++) {System.out.print (arr[i][j]+ "\ t");} System.out.println ();}}}
Output effect:

2. Some properties of the two-D array:
Package Com.date6;public class Test2 {public static void main (string[] args) {int[][] arr = new int[3][]; System.out.println (arr); System.out.println (arr[0]);//output NULL, because it is not initialized System.out.println (arr[0][0]),//Because the Arr[0] array is empty, if you look up an array of elements,// Will definitely report null pointer NullPointerException exception}}
Output effect:

3. Understand the performance of the package:
Package com.date6;/** * Encapsulation: Refers to the properties and implementation details of hidden objects, providing public access only to the outside. * Benefits: * Isolate changes, ease of use, improve reusability and improve security. * * Package Principle: * * To hide content that does not need to be provided externally. Hide properties, provide public methods to access them. For example, Getxxx,setxxx. * * * The private is only an embodiment of the package. *2.private keyword: is a permission modifier that is used to decorate members (member variables and member functions), and the privatized members are only valid in this class. One of the most common scenarios: privatization of member variables, access to the corresponding set and get methods, and improved security of data * The  smallest package in Java is a function, a class, a framework is also a wrapper. */class person{//private: Private, is a permission modifier used to decorate//do not want others to directly access the assignment, you need to hide the property by privatization private int age;//provides set, get public method access to it void Setage (int a) {//In the Set method you can restrict the assignment of a property if (a>0 && a<130) {age = A;} ELSE{SYSTEM.OUT.PRINTLN ("bad Data");}} public int getage () {return age;} void Speak () {System.out.println ("age=" +age);}} public class Test8 {public static void main (string[] args) {person p = new person ();//access P.setage by other means;p. Speak ();//Assignment not Valid, the Set method is not allowed to successfully assign a value of P.setage (-20);}}
4. Use of Anonymous objects:
Package com.date6;/** * Anonymous objects two usage: * 1. When only one call is made to an object method. * 2. An anonymous object can be passed as an actual parameter. */class car{static String color = "Red"; static int num = 4;public static void Run () {System.out.println ("function run is RU Nning ");} public static void Show () {System.out.println (num+ "..." +color);}} public class Test5 {public static void main (string[] args) {//For an object method to be called only once, you can use the anonymous object new Car (). Show ();// Anonymous objects can be passed as actual parameters show (new Car ());} public static void Show (car car) {Car.color = "black"; car.num = 3; System.out.println ("function show is running!"); System.out.println (car.num+ "..." +car.color);}
5. Characteristics of the basic data type parameters passed in the function:
Package com.date6;/** * Basic data type parameter pass  *1.JVM call the Main method, the main method into the stack. Set the value of the X variable to 3. The *3.main method calls the Show method, and 3 assigns a value to the show method parameter x as a basic data type parameter, * that is, at this point the Show method's parameter x value is 3. After the *4.show method executes x = 4, the parameter x value of the Show method becomes 4. *5.show method execution ends, show method out stack. The Show method parameter X also comes out of the stack. The *6.main method prints the value of x. At this point, X refers to the value of the x variable in the main method (the parameter X in the Show method * has been stacked with the show method). Therefore, the printed x value is 3 instead of 4. *7.main method execution end, out of the stack. */public class Test6 {public static void main (string[] args) {int x = 3;show (x); System.out.println ("x=" +x); The following two ways to print the X value are 3//way a public static int show (int x) {x = 4;return x;} Mode two public static void Show1 (int x) {x = 4;}}
6. Characteristics of a reference data type parameter passed in a function:
Package com.date6;//reference data type parameter pass/** * Execution Procedure Description: * 1.JVM Call the Main method, the main method into the stack. * 2. Create a Test7 object D (created in heap memory, D as a reference variable, point to the entity object created in heap memory), and * Set the value of attribute x in the entity object pointed to by D to 9. * The 3.main method calls the Show method, and D assigns a value to the show method parameter T as a reference data type parameter, that is, the parameter T of the Show method and the variable D in the Main method point to the same entity object in the heap memory at the same time. * 4. Call the property in heap memory by T.x and modify its value to 4. * The end of the 5.show method, the end of the life cycle of T, the Show method out of the stack, but actually it has changed the value of the property in the heap memory, * Also because the main method of the variable d still refers to the heap in the memory of the entity objects, so the heap in memory of the entity objects are not purged by the garbage collector * The 6.main method prints the value of the D.x, at which point D points to the referenced variable x of the modified object in the heap memory, and the resulting output is 4 rather than 9. * The 7.main method ends, out of the stack, and if that object in the heap memory is not called, it will be reclaimed by the JVM's garbage collector *. *  Summary: * in Java, the transfer of method parameters is always passed, and this value, for the base data type, is the value you assign to the variable. * And for reference data types, this value is a reference to the object, not the object itself. */public class Test7 {int x = 3;public static void Main (string[] args) {Test7 d = new Test7 ();d. x = 9;show (d); System.out.println (d.x);} The result of printing x in the following two ways is 4//way a public static void show (Test7 t) {t.x = 4;} Mode two public static int show1 (Test7 t) {t.x = 4;return t.x;}}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse Programmer--java Basic Learning Note 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.