Java Process Control, array entry (j2se entry 3)

Source: Internet
Author: User

Java Process Control
Control Flow
If ()
If ().... Else
If ()..... Else if ().... Else

Note: else is only paired with the nearest if () on the same layer above it.

switch(){
case 'a':……..
case 1:……break;
default:
…………
}

Note:The data type in switch () is byte short char Int.Only the above four types can be used in switch. When break is not added to the case block, the following statements are executed sequentially.

Loop statement

For (INT I = 0; I <n; I ++ ){}
While (){}
Do {} while (); ----------- pay attention to the plus points

Example:
Loop: For (INT I = 0; I <n; I ++)
{
For (Int J = 0; j <m; j ++)
{
If (3 = J)
{
Break loop; // -- the loop can only be used in loop statements, and is used to jump from loop to outer loop.
}
}
}

Analysis:
Int X, A = 6, B = 7;
X = A ++ B ++; // ---------- A = 7, B = 8, x = 13
Int x = 6; X = ~ X; // ---------------- the binary 0110 of 6 is reversed to 11001 and then converted to a complement code (reversed to 1) 10111 =-7


Break, jump out of the cycle of this layer, execute the following code, and continue, terminate this loop in advance, and then exit the loop after the loop or loop conditions are met or not met. Break Tag Name; Continue tag name; the knowledge of these two statements indicates that a tag loop exists and a tag loop is terminated in advance. It can only be used in a loop Statement (multi-layer loop nesting, nested loops are used to jump to the outer loop.

Note: When using a for loop, be sure not to forget the two ";" in (). The statement of an endless loop is for (;) {} or
While (true ){}

Note: system. out. println ("... "+ a) when using this statement, it will convert non-string values into strings (not all data types are acceptable ).
The array in Java contains two parts: array reference and array space.

Declare an array
1) A set of data of the same type (can be a class;
2) An array is an object;
3) declaring an array does not create an object;
4) The array can be declared as follows:
Int [] I or Int I []
Car [] C or car C []
* In C ++, only car C [] can be used.
* Car [] C is recommended in Java;
5) The definition of arrays is as follows:
Int [] A (array reference Declaration) = new int [10] (Declaration of the array space, and assign the first address of the space to the reference of the array)
Int [];
A = new int [20];

Create an array
1) Create an array of basic data types int [] I = new int [2];
2) create a reference data type array car [] C = new car [100];
3) The array is created with an initial value.
Number Type: 0 boolean type: false reference type: NULL

Note: When you access the value in an uninitialized array, an exception (nullpointerexception) is thrown. in Java, only the address of one array is consecutive, A two-dimensional array is actually a reference stored in a one-dimensional array.

Initialize an array
1) Separate initialization, creation, and Declaration
Int [] I;
I = new int [2];
I [0] = 0;
I [1] = 1;
2) initialization, creation, and Declaration at the same time
Int [] I = {0, 1 };
Car [] C = {new car (), new car ()};

Multi-dimensional array
1) valid definition
Int [] [] I1 = new int [2] [3]; (one-dimensional, two-dimensional space is also given)
Int [] [] I2 = new int [2] []; (given one-dimensional space, two-dimensional space to be determined)
I2 [0] = new int [2], I2 [1] = new int [3];
* Int [] [] = new int [] [3] in C ++; valid
2) invalid definition
Int [] [] I1 = new int [] [3];
3) array length ---------- length of the array attribute (in a two-dimensional array, this attribute only represents the length of the first dimension)
Int [] I = new int [5];
Int Len = I. length; // Len = 5;
Student [] [] ST = new student [4] [6];
Len = ST. length; // Len = 4;
Len = sT [0]. length; // Len = 6;

Copy Array
System. arraycopy (Object SRC, int srcpos, object DEST, int destpos, int length );
Srcpos: SRC Source array. srcpos is copied from the nth position. the start position of the Dest target array and the start position of the destpos target array are length, which indicates the length to be copied.
Copy an array to another array.

Class Object creation and object Array

Multiple classes can be defined in a XXX. Java file, but only one public class can be modified. The class name of this class can only be used as the file name of. java.

To create a Class Object in Java, you must first create a reference to this object, for example, car C. Then, use the new keyword to create an object instance (Object Space) for example: C = new car (); then, the first address of the object's instance space is assigned to the object reference. Multiple objects can be referenced from the instance of the same object at the same time, but object references can only reference the instance of one object.

The reference of an object and the instance of an object are like the lines and balloons that hold the balloon.

Note: Only an instance of an object that is not referenced by any object will wait for garbage collection.

Object Array

Example: Car [] C = new car [3];
C [0] = new car ();
 
Note:The data that stores basic types of arrays is directly stored in the array space, while the array of objects is stored in the array space as a reference of objects..

The attribute of the class defined in the class is the instance variable, and the variable defined in the class method is the local variable. Instance variables are stored in the object space, while local variables are allocated space for method calls. After the call is completed, the space is released.

Note: The attribute definition and method definition must be written in the class definition.

Note: The system will automatically initialize the instance variable. The numeric type is 0, the boolean type is false, and the reference type is null. Local variables must be initialized and must be assigned an initial value. Compilation fails if no initial value is assigned.

There are two types of parameter passing in method calls in Java. One is to use value passing (directly passing the value of the parameter) for the parameter as the basic type, and the other is to pass the reference, it is used for a parameter as a class object, and it passes a reference to this object.

Related Article

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.