Java Fundamentals 05: Object-oriented
One, array
memory management for arrays : A contiguous space to store elements.
Int [] arr = new int[];
Create an array of type int,arr is just a variable, just a reference address to the array, local variables exist in a stack,java There are no global variables, only member variables , stored in the heap area
Common errors in arrays:
1.NullPointerExcepation: null pointer exception
Cause: The referenced type variable does not point to any object, and in this case it also accesses its properties, an object that is not released immediately if it is used, but instead becomes a garbage object that is freed by the garbage collection mechanism itself. Programmers have no way to operate release time
2.ArrayIndexOutOfBoundsExcepation: Array subscript out of bounds
Find out if the array has this element :binarysearch(the array to find, the number to find) , and the Number (index) of an int type.
There are multidimensional: two-dimensional arrays in an array in Java, three-dimensional ...
Two-dimensional array definition format:
Data Type [] [] array name = new data type [] [];
Dynamic initialization:
Data Type [] [] array name = new data type [] [];
Note: The preceding brackets value refers to the number of two-dimensional array elements, followed by the number of elements in a one-dimensional array in a two-dimensional array
Static initialization:
Data Type [ Row ] [ column ] Array name = {{ element 1 ...}, { element 1 ...}, { element 1 ...}};
Int[] [] arr = {{1, 2, 3}, {4, 5}, {6, 7}}; Int num = arr [2] [1]; //7 |
The elements in a two-dimensional array must be one-dimensional arrays
To receive an element from a two-dimensional array, you first need to have a one-dimensional array to receive
Summary: features of the array
1. Only data of the same data type can be stored in an array
2. The array 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
Second,Java also belongs to
Object Oriented
of programming:
Three main features of object-oriented: encapsulation, inheritance, polymorphism
Object: The only thing that really exists
(Object-oriented programming:OOP) thought: Trying to keep things in computer language as consistent as possible in nature
Object-oriented Core: Find the right object to do the right thing
How to find the right object in Java:
1.sun already assigned classes, creating objects from custom-made classes, you need to recognize these classes
2. Custom classes, creating objects with custom classes
The class contains:
Properties and Methods
The definition format of the class:
class name {
Attribute (member variable): Describe the public characteristics of a thing
format: Data type Variable name
String name;
Method: Describe the behavior of a thing (what to do)
modifier return value type Method Name (parameter list) { concrete implementation }
Public: A publicly available method that anyone can access
private: Privately, only for your own use
Cases: public void Run () { System.out.println ( "Teacher lectures, hurry back to the classroom!") "); } } |
Access properties:
Object . Property name
To set properties:
Object . Property name = set the value;
The object's reference variable is present on the stack, but the object itself is present in the heap area, and the member is also in the heap with the object
the difference between a member variable and a local variable :
A) the location defined:
1. Member variables are defined in the class, outside of the method
2. Local variables are defined in the method
B) on the scope:
1. Member variables can be used in the current class
2. Local variables can only be used in methods
C) on the life cycle:
1. Member variables: Created as objects are created and disappear as objects disappear
2. Local variables: Created with the invocation of the method, the scope of the method is destroyed
D) The initial value to differentiate :
1. member variable, if not assigned, it has a default value of
int By default is 0
Float default is 0.0f
Double default is 0.0
Boolean default is false
Char default ' '
String (reference type)null;
2. Local Variables:
Local variables default to no initial values , and must be initialized (declaring variables and assigning values) before they can be used
Member variables can be used directly in the current class, and if the variables need to be used in other classes, the object needs to be created (as it is now)
iii. Relationship of classes and objects
A class is an abstraction of the same class of things (objects), and objects are entities of that class that actually exist
Java Foundation