Java learning notes

xiaoxiao2021-03-06  49

I am learning the notes made in Java, I hope to help everyone. 1. About the initialization order of Static Class Bowl {Bowl (Int Marker) {System.out.Println ("Bowl (" Marker ")");} Void F (Int Marker) {system.out.println ("f (" MARKER ") ")");}} Class Table {Static Bowl B1 = New Bowl (1); Table () {System.out.Println ("Table ()"); b2.f (1);} Void F2 (INT Marker) {system.out.println ("F2 (" Marker ")");} static bowl b2 = new bowl (2);

Class Cupboard {Bowl B3 = New Bowl (3); Static Bowl B4 = New Bowl (4); Cupboard () {System.out.Println ("Cupboard ()"); b4.f (2);} void F3 Int marker {system.out.println ("F3 (" marker ")") ")") ")");} static bowl b5 = new bowl (5);}

Public class staticinitialization {public static void main (String [] args) {system.out.println ("Creating new cupboard () in main"); new cupboard (); System.out.Println ("CREATING New Cupboard () in "); new cupboard (); t2.f2 (1); t3.f3 (1);} static table t2 = new table (); static cupboard t3 = new cupboard ();} ///: ~ output result To: Bowl (1) Bowl (2) Table () f (1) Bowl (4) Bowl (5) Bowl (3) Cupboard () F (2) Creating New Cupboard () in Mainbowl (3) Cupboard () f (2) CREANG (2) CREANG New Cupboard () in Mainbowl (3) Cupboard () F (2) F2 (1) F3 (1)

1. First run Static Table T2 = new table (); define a Table type object T2, first initialize B1 in the table, call the Bowl method, output Bowl (1); then return to Table Internalization B2, once again Call BOWL and output Bowl (2). After initializing STATIC in the table, it began to initialize the Table method in the Table class and output Table (); then execute B2.f (1), output f (1); 2. Then run Static Cupboard T3 = New Cupboard () Define a CupBoard type object T3, and then initialize B4 within the Cupboard; call the Bowl method, output Bowl (4), then return to the initialization of B5, output Bowl (5), then initialize B3, output Bowl (3) Then, CupBoard () is output. Then call the F method to output f (2); 3. Subsequently, the main method is started, first output Creating New Cupboard () in main, this is a New Cupboard (); enter the class Cupboard, run first Bow B3 = New Bowl (3), output BOWL (3), then outputs Cupboard (), then runs B2.f (2), output f (2) .4. Then return to the main method, first output Creating New Cupboard () in main, I encountered New Cupboard (), as described above, output Bowl (3), outputs Cupboard (), output f (2) .5. Below is running T2.f2 (1); because of the previous definition Good T2, at which time T2 is called directly, the F2 method is called, and the F2 method is called, and the F2 method outputs F2 (1); the operation T2.f3 (1) is also, the method F2 in the CupBoard is output, and F3 (1) is output. About the initialization of the object, let me say, wrong, I am sorry: For initialization mainly contain these aspects: static variable, non-static variable, constructor, new object is established.

1. Initialization of Static Variable: When the PULIC CLASS is loaded, it will begin to initialize the Static variable because the reference of the Static variable is stored in Static Storage (static storage). There is no initialization of the Non-Static variable and constructor, because there is no object of the object, just put a certain type loadin. Note that only the Static variable is only initialized once, when there is a new object generated, he will not be reopened, that is, his REFRENCE has been fixed, but his value can be changed.

2. When an object is generated, the Non-StATIC variable in this Class (Type) is started to initialize, and then the constructor is initialized. Generate an initialized Object object.

3. Perform other functions in order.

4, for inherited Class (type): derivedclass2, derivedclass1, baseclass; because of the inheritance relationship between them, if you want to Laodin DerivedClass2, you must first loadin deiveClass1, if you want Laodin DerivedClass1, first loadin baseclass. That is, the Laodin sequence is: baseclass, derivedclass1, deriveclass2 ..., whenever loadin is a Class, press "First" to initialize (initialize the Static variable in the Class). 5. When using the inheritance Class When using the NEW, press BASECLASS, DERIVEDCLASS1, DERIVECLASS2 ..., and each class is initialized in each class. Note that the constructor of Derived Class must meet Baseclss to be initialized.

Overall thought: static variables ... Non-static variables ... constructor.

*********************************************************** ***************************************************

2. Initialization of static variables. Class value {static int C = 0; value () {c = 15;} value (int i) {c = i;} static void inc () {C ;}} class count {public static void PRT (String S) {System.out.println (s); value v = new value (10); static value V1, v2; static {PRT ("v1.c =" v1.c "v2.c =" v2.c ); V1 = new value (27); PRT ("v1.c =" v1.c "v2.c =" v2.c); v2 = new value (15); PRT ("v1.c =" v1.c "v2.c =" v2.c);

Public static void main (string [] args) {count ct = new count (); PRT ("ct.c =" ct.vc); PRT ("v1.c =" v1.c "v2.c = " v2.c); v1.inc (); PRT (" v1.c = " v1.c " v2.c = " v2.c); PRT (" ct.c = " ct.vc) }}

The operation results are as follows: v1.c = 0 v2.c = 0V1.C = 27 v2.c = 27v1.c = 15 v2.c = 15ct.c = 10v1.c = 10 v2.c = 10v1.c = 11 V2 .c = 11ct.c = 11

All variables in the Class will be initialized before calling any function (including constructor). *********************************** *********************************************************** ***********

3. About SSTR1 == SSTR2 SSTR1.EQUALS (SSTR2) SSTR1.COMPARETO (SSTR2) doubts

/ * * Where sstrl == str2 and sstr1.equals (sstr2); what is going on, the same object should be equal, * /

import java.io *; class EqualsTest {public static void main (String args []) throws IOException {String sStr1, sStr2; sStr1 = "using java language"; sStr2 = sStr1; System.out.println (. "string1:" SSTR1); System.out.Println ("String2:" SSTR2); System.out.Println ("Same Objext?" (SSTR1 == SSTR2)); SSTR2 = New String (SSTR1); System.out. Println ("String1:" SSTR1); System.out.Println ("Strint2:" SSTR2); System.out.Println ("Same Object?" (SSTR1 == SSTR2)); // (1) SYSTEM .out.println ("Same Value?" SSTR1.EQUALS (SSTR2))); // (2) System.out.Println ("Same Value?" SSTR1.Compareto (SSTR2)); // (3) SYSTEM Run the result is:

String1: using java languageString2: using java languageesame Objext? TrueString1: USING JAVA LANGUAGESTRINT2: USING JAVA LANGUAGESAME OBJECT? FALSESAME VALUE? TRUESAME VALUE? 0

Answer: SSTR1.EQUALS (SSTR2): The return value is a boolean value, it compares whether the value of the two strings is equal; SSTR1.Compareto (SSTR2): The return value is an integer, and Compareto () is not only a small, but also determined. The order of arranging. If SSTR1 is larger than SSTR2, a positive integer is returned, if SSTR1 is less than SSTR2, return a negative integer, if SSTR1 is equal to SSTR2, returns 0.

SSTR1 = "Using Java Language"; // Initialize a string SSTR2 = New String (SSTR1); // New a string object, the value of the two, but point to different memory space // SSTR1 == SSTR2 Returning False, if you want to compare the contents of the two strings, use the equals method sstr1 == SSTR2 not only whether its content is the same, but also compare it to the same memory address. SSTR1 = "Using Java Language"; SSTR2 = SSTR1; System.out.Println ("String1:" SSTR1); System.out.Println ("String2:" SSTR2); System.out.Println ("Same Object? " (SSTR1 == SSTR2); here SSTR1 =" Using Java Language "; first initialize a string SSTR2 = SSTR1; this step, pointing SSTR2 points to the address of SSTR1, the same string" Using Java Language " So, at this time, not only the content is the same, but even the address is the same, so returns TRUE.

Create a String class object, when you initialize it, you should be a string, so after you New, although the initialization is SSTR1, but only passes only the value of SSTR1. So or different objects

Create a string object, the same value, but point to different memory spaces

SSTR1 == SSTR2 is not only the same, but also compares whether it is to point to the same memory address.

SSTR1.EQUALS (SSTR2): The return value is a boolean value, which compares whether the value of the two strings is equal;

SSTR1.COMPARETO (SSTR2): The return value is an integer, but CompareTo () is not only a small smaller, but also the order of the arrangement.

*********************************************************** **************************************************

4. Display issues related to the date

import java.util.Date; import java.text *;. class TodayDate {public static void main (String [] args) {DateFormat defaultData = DateFormat.getDateInstance (); System.out.println (defaultData.format (new Date ( ))); DateFormat shortTime = DateFormat.getTimeInstance (DateFormat.SHORT); System.out.println (shortTime.format (new Date ())); DateFormat longTimestamp = DateFormat.getDateTimeInstance (DateFormat.FULL, DateFormat.FULL); System .out.println (longtimestamp.format (new date ())); DATEFORMAT myFORMAT = New SimpleDateFormat ("YYYY.MM.DD"); //? System.out.print (MyFormat ()); try {date leapday = myFormat.Parse ("1985.01.27");} catch (parseException e) {} system.out.println (); } / * * 2004-10-29 12: 52, October 29, 2004, Friday, Friday, 45 seconds, 45 seconds, CST2004.52.29 /// How to display 52, not a month? DateFormat myformat = new simpledateformat ("YYYY.MM.DD"); change MM to mm * /

*********************************************************** **********************************************

5. Import java.util.timer; import java.util.timertask; public class recnder {timer Timer; public reminder (INT Seconds) {Timer = new timer (); timer.schedule (New Remindtask (), Seconds * 1000);} Class Remindtask Extends Timertask {Public Void Run () {System.out.Println ("Time's Up!"); Timer.Cancel ();}}} public static void main (String args []) {New Reminder (3); // for (int i = 0; i <10000; i ) // for (int J = 0; j <10000; J ); System.out.Println ("Task Scheduled.");} } / * time.schedule (new remindtask (), seconds * 1000); task scheduled.time's up!

I change this to Timer.Schedule (New Remindtask (), Seconds * 0); after the Task Scheduled.Time's Up! When the delay is 0, it is the same as the delay time of 3 seconds, I I don't know how this program is called. I am always Time's Up! Task Scheduled. The program is changed to: New reminder (0); for (int i = 0; i <10000; i ) for (int J = 0; J <10000; J ); // Delay time result is: D: / Eclipse / Workspace / Hello> Java Remindertime's Up! Task Scheduled. * /

*********************************************************** *************************************************

6. When an object is created, initialization is completed in the order in the following: 1. Set the value of the member to the default initial value (0, false, null) 2. Call the object constructor (but no construct Method 3. Calling the constructor of the parent class 4. Initialize the member 5 using the initializer and the initial block, execute the construction method Class A {INT A = f (); int F () {Return 1;}} Class B Extends A {int b = 37; int f () {return b;}} public class ctordemo4 {public static void main (string args []) {b bobj = new b (); system.out.println (bobj.a) System.out.println (bobj.f ());}}

0 37 The order should be: 1. Allocate A, B and Method pointers from Heap, the memory of the Class pointer 2. Initize A and B to the data default value, such as INT 0, the object pointer is null.3. Call B (), Note: The constructor is not entered, and the number of pass parameters is used. 4. Call A (). 5. Execute a () function body, return .6. Tune b (). F, C (). F, this time B = 0 !!!!!!!! 7. Execute B () The function body, returns 8. Pair B INITIALIZE, call B = 379. Bobj points to the final creation completed in the HEAP.

转载请注明原文地址:https://www.9cbs.com/read-81883.html

New Post(0)