Java Note 4

Source: Internet
Author: User

Fourth day notes

1. Arrays

Concept: A container for storing the same data type.

1.1 The definition of an array in Java:

Data Type [] Array name = new data type [length]

1.2 the length of the array can be specified or not specified

int[] arr = new Int[0];

1.3 create an array of type int

Arr: is a variable just a reference address to an array

Local variables exist in the stack

member variables --Properties---> in the heap area

static variable ---> Shared area

1.4 using tools in arrays:Arrays

1.4.1 Find if there is a number in the array, the corresponding index position

int result = Arrays.binarysearch (arr, num);

1.4.2 do sort

Arrays.sort (ARR1);

for (int i = 0;i < arr1.length; i++)

{

System.out.println (Arr1[i]);

}

1.5 in Java as arrays exist multidimensional: two-dimensional arrays, three-dimensional arrays, ....

1.5. Definition format for 12-D arrays:

Data Type [] Array name = new data type [];

(1) Dynamic initialization:

Data Type [] Array name = new data type [];

Note : The number of two-dimensional array elements in the preceding brackets , The latter one refers to the two-dimensional array

The number of elements in a one-dimensional array.

(2) Static initialization:

Data Type [] Array name = {{element 1,..},{element 1,..},{element 1,..}};

Note: The elements in a two-dimensional array must be one-dimensional arrays.

Cases:

int[][] arr = new INT[3][4];

Arr[0][0] = 1;

Int[][] arr1 = {{1,2,3},{2,3},{1,4}};

int num = arr1[2][1]; 4

you want to receive elements from a two-dimensional array : You need a one-dimensional array to receive.

int[] arr2 = new int[];

ARR2 = new Int[3]; Defines a two-dimensional array, an array of element-dimension initialization arrays

int arr3[] = {}; It is not recommended to write like this.

Summary: Features of the array

1. Only data of the same data type can be stored in an array.

2. Array He assigns an index value to the element that is deposited by default, and the index starts at 0.

3. Once the array is initialized, the length is fixed.

4. The memory address of the elements in the array is contiguous.

2. Object-oriented

2.1 Java also belongs to object-oriented programming:

2.1.1 Three main features of object-oriented: encapsulation, inheritance, polymorphism.

Class 2.2, object.

Object : The only thing that really exists.

Object-Oriented programming (OOP), which tries to make a description of things in computer language and things in nature

Keep it as consistent as possible.

Object-oriented core : Find the right object to do the right thing.

2.3 How to find the right object in Java:

1.sun classes are already assigned, and you need to know these classes to create objects from custom-made classes.

2. Customize the class to create the object from the custom class.

the 2.4 class contains :

Properties , method.

The 2.4.1 class defines the format:

Class Name {

Properties (member variable): Describe the public characteristics of a thing.

Method : Describe the behavior of a thing (what to do)

}

Create an object from a custom class

Format:

class name Object Name (variable name) = new class name ();

2.4.2 member variable and local variable amount difference:

the location defined :

1. Defined in the class, outside of the method.

2. Local variables are defined in the method.

On the role:

1. Member variables are public properties used to describe the same class of things (objects).

2. A local variable is to provide a variable for the method to use

Life cycle :

1. The member variable is created as the object is created and disappears as the object disappears.

2. Local variables are created as a method call, and the scope of the method disappears.

Initial values to differentiate:

1. member variables, if you do not assign a value, it has a default value.

int by default is 0

Float default is 0.0f

Double default is 0.0

Boolean Default False

Char default ' '

String (reference type) null;

2. Local Variables:

A local variable has no initial value and must be initialized before it can be used.

The 2.4.3 member variable can be used directly from the variable name in the current class, and if the variable needs to be used in another class, it needs to be created (now the

member Variable : The scope is the entire class, if you need to use the member variable in the current class, directly get the variable name can be used

Java Note 4

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.