Java Programming Ideas (Chapter2, 4, 6)

Source: Internet
Author: User

It's all about objects. Manipulating objects with reference to manipulate identifiers in Java are actually "references" to objects. For example, if you want to manipulate a string, you can create a string reference. String S; Here s is just a reference. Storage location base Type/object reference: Stack object: The heap scope scope is determined by {}, which determines the Variable nameVisibility and life cycle.
1 {2     int x =12; 3     {4         int//illegal5    } 6 }

Illegal, can't hide.

{    new String ("str");}
The reference s disappears at the end of the scope, while the string object pointed to by S still occupies the memory space until the GC is destroyed. Questions about value passing or reference passing only value passing exists in Java1. There is no doubt about the delivery of the basic type, with a value of 2. Object deliverywhen a parameter is passed, there is only a problem passing the base type and object reference, and it does not pass directly to the object itself

A method cannot modify a parameter of a base data type

A method can change the state of an object parameter

A method cannot have an object argument referencing a new object

Refer to the topic: is Java really a value pass or a reference pass? Portal for a parameter is a string type and is not modified because the string object is FinalType. Initialization and cleanup initialization order with one instance description
 public  class   tag { public  Tag (int   marker) {System.out.println ( "tag" +    marker); }}
 public  class   Card {tag tag  = new  tag (1);  public   Card () {System.out.println (        span> "card ()" );    Tag3  = new  Tag (33 = new  tag (2 void   F () {System.out.println ( "f    () "); } tag Tag3  = new  tag (3 
 Public class Init01 {    publicstaticvoid  main (string[] args) {        new Card ();        CARD.F ();    }}

Result

Tag1tag2tag3card () tag33f ()
View CodeYou can see that the initialization of the variable is performed before the constructor call. On this basis, if there is static data, what is the order?
 public  class   Bowl { int   marker) {System.out.println ( "bowl" +marker);  protected  void  F (int   marker) {System.out.println ( "F" +    Marker); }}
 public  class   Table { static  Bowl b1 = new  Bowl (1);  public   Table () {System.out.println (        span> "table ()" );    B2.F ( 1);  void  F2 (int   marker) {System.out.println ( "F2" +marker);  static  Bowl b2 =new  Bowl (2);}
 Public class Cupboard {    =new Bowl (3);     Static New Bowl (4);    Cupboard () {        System.out.println("Cup ()");        B4.F (2);    }     void f3 (int  marker) {        System.out.println ("F3" +marker);    }     Static New Bowl (5);}
 Public classTest { Public Static voidMain (string[] args) {System.out.println ("Create Cup in main"); Newcupboard (); System.out.println ("Create Cup in main"); Newcupboard (); T2.F2 (1); T3.F3 (1); }    StaticTable t2 =NewTable (); Staticcupboard T3 =Newcupboard ();}

Result

Bowl 12145323232  11
View CodeAs can be seen, the initialization of static data (only once), then the initialization of ordinary variables, and then call the constructor. Array initialization each time an array is accessed, the bounds of the array are detected. Constructor for reusing class with parameters if the class does not have a default parameter, or if you want to invoke a base class constructor with parameters, you must write the statement that calls the base class constructor with the super display.
 Public classC3extendsc2{ PublicC3 (inti) {Super(i);//System.out.println ("C3"); }     Public Static voidMain (string[] args) {NewC3 (1); }}classc1{ PublicC1 (inti) {System.out.println ("C1"); }}classC2extendsc1{ PublicC2 (inti) {Super(i);//System.out.println ("C2"); }}
Here, two super (i) are required, or they will be an error. Initialization and loading of classes
 Public classInsect {protected inti = 9; protected intJ; protected intx3 = Print ("Static INSECT.X3 init");  Publicinsect () {System.out.println ("I=" +i+ ", j=" +j); J=39; }    Private Static intX1 = Print ("Static insect.x1 init"); Static intprint (String s) {System.out.println (s); return47; }}
 Public classBeetleextendsinsect{Private intk = Print ("BEETLE.K init");  PublicBeetle () {System.out.println ("K=" +k); System.out.println ("J=" +j); }    Private Static intx2 = Print ("Static BEETLE.X2 init");  Public Static voidMain (string[] args) {System.out.println ("Beetle construct"); Beetle Beetle=NewBeetle (); }}

Result

Static insect.x1 Init Static beetle.x2 Initbeetle Construct Static insect.x3 initi=9, j=0beetle.kinitk =47J=39
View Code

As can be seen, the first class loading, first base class again subclass. The static variable/block executes during the initialization period of the class. New is then executed in the order in which the constructors are initialized first.

Java Programming Ideas (Chapter2, 4, 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.