JAVA learning notes (1), JAVA learning notes (

Source: Internet
Author: User

JAVA learning notes (1), JAVA learning notes (

JDK: a toolkit for Java developers, including Java development tools and JRE. In other development tools, the compilation tool (javac.exe?#runtime tool (java.exe##packaging tool (jar.exe) and so on.
JRE: Java Runtime Environment, including the core class libraries required for Java Virtual Machine and Java program running.
JVM: Java virtual machine.
Relationship between the two:
JDK = JRE + Development Tool
JRE = JVM + core class library

Configure jdk environment variables in JAVA
JAVA_HOME C: \ Program Files \ Java \ jdk1.7.0 _ 21
Path % JAVA_HOME % \ bin;

When using the jar package, use "java-cp test. jar Test"
Remember the first Easy mistake in Java programs: java. lang. NoClassDefFoundError
-This error is often encountered by beginners. In 99.9%, it is because you forget it in CLASSPATH.
Drop the required jar package
-If you encounter this error, go back and find all your jar packages, and then go to the java Community to ask questions.

All the letters in the keyword are in lower case;
Java Naming rules:
Package name: when multiple words are made up, all letters are in lowercase, and the domain name is written backwards (com. ****. www)
Class Name Interface Name: when multiple words are made up, the first letter of all words is capitalized.
Variable name and function name: when multiple words are made up, the first letter of the first word is lowercase, and the second word starts with the first letter of each word.
Constant name: All letters are uppercase. When multiple words are used, each word is connected by an underscore.

Java data type:
Byte 1 byte (8 bits)-2 ^ 7------2 ^ 7-1 =-128 to 127
Short integer two bytes (16 bits)-2 ^ 15------2 ^ 15-1 more than 3 W
Int integer 4 bytes (32-bit)-2 ^ 31------2 ^ 31-1 hundreds of millions
Long Integer 8 bytes (64-bit)-2 ^ 63------2 ^ 63-1 (followed by L)
Float 4 bytes (32-bit) (followed by f)
Double 8 bytes (64-bit) Dual precision
Char 2 bytes
Boolean 1 byte true false
Forced type conversion: (byte) (a + 10) -- convert a + 10 to byte
Computation of characters and numbers is converted into ASCII code numbers and numbers.
In the switch function expression, before java 1.6 (including), only data equivalent to the basic int type is supported: byte, short, char, int (none of the others can be ). 1.7 The new features can support data of the String type.
Method (function ):
Note:
1) methods cannot be defined in another method.
2) You can call another method.
3) if a method does not return a value, you do not need to write the declaration of the return Method to void. If a method does, you need to write the return method.
4) The method is not called and is not executed.
Naming rules: if a word is in lowercase, if it is in two words, the first letter of the second word is in uppercase.
What is overload: in the same class, if the method name is the same, the type and number of parameters of the parameter are different. Note that it is irrelevant to the return value.

Memory analysis:
Stack memory: used to store local variables. when data is used up, the occupied space is automatically released. When the method runs, it is transferred to the stack memory. When it is no longer used, only the methods in the stack memory are deleted, and the methods in the method area are retained.
Heap memory: the array and object are stored in the heap memory through the new instance. Each object has a memory address value, and all variables in the object have a default initialization value; the object is no longer used and will be recycled by the Garbage Collector within an uncertain period to store global variables.
Method Area (data area): used to store methods, divided into static method area and non-static method area.

Note: int [] [] arr = new int [10] []; a one-dimensional array must have a value before it can be correctly created.

Array features:
1) The size is given during creation (0 can be given, but an error will be reported when printing or assigning values)
2) array Initial Value: int type default 0 float Type default 0.0f boolean Type default false
Char type defaults to '\ u0000', which is equivalent to '(null character)
3) The array has subscripts and starts from 0.
4) Get the array size attribute: array name. length

Maximum value:
Int maxElement = a [0];
For (int x = 1; x <a. length; x ++ ){
If (a [x]> maxElement ){
MaxElement = a [x];
}
}
Return maxElement;
Select sort:
For (int I = 0; I <arr. length-1; I ++ ){
For (int j = I + 1; j <arr. length; j ++ ){
If (arr [I]> arr [j]) {
Int min = arr [j];
Arr [j] = arr [I];
Arr [I] = min;
}
}

Bubble Sorting:
For (int I = 0; I <arr. length-1; I ++) {// control the number of comparisons-1: because it is a second-to-second comparison, the number of comparisons is less than the array length 1
For (int j = 0; j <arr. length-1-i; j ++) {//-1: because it is a second comparison;-I: Each comparison, the last value must be at the end, so the maximum value does not need to be compared.
If (arr [j]> arr [j + 1]) {// place a small front and a large back
Int temp = arr [j + 1];
Arr [j + 1] = arr [j];
Arr [j] = temp;
}
}
Binary Search:
Int min = 0;
Int max = arr. length-1;
Int mid = (min + max)/2;
While (arr [mid]! = Key ){
If (key> arr [mid]) {
Min = mid + 1;
}
Else if (key <arr [mid]) {
Max = mid-1;
}
If (max <min ){
Return-1;
}
Mid = (max + min)/2;
}
Return mid;

Sort Arrays:
Arrays. sort () method: sorts Arrays in ascending order. You need to introduce the java. util. Arrays package.
Search for Arrays:
Java. util. Arrays. binarySearch (object [] arr, object key) // an array is the number to be queried.
Returned value: If the subscript is found,-1 is not found.
Note: the bottom layer of this method uses the binary lookup method (half lookup method). Therefore, this method is sorted first.
Copy an array:
Java. lang. System. arraycopy (source array name, source array start position, target array, destination array insert position, length to be copied)
No return value
Note: for example, if there are 6 in the source number group and we need to copy 7 to the target array, an exception occurs when the array subscript is out of bounds.

Process-oriented:
Each step is done by ourselves. It has disadvantages, low efficiency, difficult to maintain, and poor reusability.

Object-oriented features: simplifying complex events
Changed from Executor to Conductor
High efficiency, easy maintenance, and good reusability.

Note: class name. Method Name ()
Object Name. What is the difference between method name?

Class Name. Method Name () method name with static
Object Name. Method Name () method name without static
A class file can have multiple classes, but the class file name must be public before the class, and only one class name is public.
A class can be called without the main () method, but cannot be called by JVM.

Note:
Class name variable name = new Class Name ();
Variable name = null;
Variable name. Attribute = value.
Variable name. Method (); a null pointer exception is reported

What is the difference between local variables and global variables?
1. Global variables are set in the class, and the entire class can be used
The local variables are defined in the method. In the code block, only the region in the statement is valid.
2. Global variables exist in heap and local variables exist in stack.
3. global variables exist with the creation of objects and disappear with the disappearance of objects.
Local variables exist with the execution of the region, and the execution ends.
4. global variables all have default values and cannot be used.
The local variable has no default initial value and cannot be used if it is not assigned a value.

Duplicate local variables and global variables
Duplicate names first search for variables in the region where they are located. If the region is not found globally, use the variables in the region if both are available.

Class type (reference type) when the parameter: the attributes in the object are the same, we can write their common attributes in a method.

Anonymous object: new Ren ();
Anonymous Object Features: if the methods or attributes of this object are called only once, you can use an anonymous object. Its main function is to simplify code writing by programmers.

Basic data type when a parameter: The method parameter is a numerical value
Reference data type when the parameter: The method parameter is an address

Java access modifier
1. public: public permission modifier class, attribute, and method. Can be accessed by any class.
2. protected: modify attributes and methods for protected permissions. It can be accessed by the same class. If it is not the same class, it must be a subclass of this class.
3. default: Permission modification class, attribute, and method of the same package. It can only be accessed by the same class.
4. private: modify attributes and methods with private permissions. It can only be accessed in this class.

Encapsulation: hides the attributes and implementation details of an object and only provides public access.
Benefits: changes are isolated for ease of use, reusability is improved, and security is improved.
Encapsulation principle: Hide all content that does not need to be provided externally, hide all attributes, and provide public methods to access them.
Encapsulation mainly uses private
Use Myeclipse to write set () get ()
Right-click the class ----- "source ---> getters and setters
Note: private is an embodiment of encapsulation. private is encapsulation, but encapsulation is not just private.

Why is the constructor used?
It is used to create objects.
What is the classification of constructor?
Default constructor and custom Constructor
Constructor features:
The class name and method name are the same. The access permission modifier is the same as the access permission modifier of the class (which can be omitted ).
The constructor does not return values and does not use void.
What is the difference between the default constructor and the custom constructor?
1) The default constructor has no parameters. The custom constructor has parameters.
2) There can be multiple custom constructor methods. Multiple constructor methods exist in the form of overloading,
There is only one default constructor.
3) The default constructor exists by default (not written) in the class.
4) if a custom constructor exists in the class but no default constructor is written, the default constructor disappears.

When can I use a custom constructor?
When creating an object, you can use a custom constructor to create an object with some specified values.
Use the default constructor if the default value is used.

What is the difference between constructor and normal method?
1. constructor: when an object is created, the constructor automatically initializes the class.
Normal method: A normal method can be called only after an object is created.
2. When an object is created, the constructor will only call it once.
Common method: After an object is created, it can be called multiple times.
Note: The constructor method does not conflict with the value assignment method.

This: indicates the current object.
How to use this?
1. When the parameter name and global variable are the same, use this! When assigning values !!!!!! (Required ),
2. Use this when calling an object property (not required or omitted)
3. this is used when an object calls a method (not required or omitted)
4. The default constructor calls the custom constructor: Book3 (,,)
1) in the default constructor, this (,) must be placed in the first line of the current constructor.
2) Call the default constructor for custom constructor Book3 ()
The custom constructor this () must be placed in the first line of the current constructor.
Note: Do not use an endless loop when calling the constructor using this.

Static: static. You can modify attributes, methods, and code blocks.
Attribute:
1) Use the class name. attribute name
2) shared by all objects
Method: Use class name. method name ----- object name. Method Name ()
Code block: it is executed once when the class is loaded.
Static code block> construction code block> Construction Method

Static code block
Load with the class, only once
Used for class initialization
Construct a code block (the constructor initializes the object)
{}
Priority when both the static code block and the constructed code block appear
Static code block> construction code block> Construction Method
Static usage time:
Static Property: If this property is shared by all objects.
Static modification method: This method accesses the static variables in the object.

The static method can only access static variables. Non-static methods can access static and non-static variables.
Static methods cannot use the this keyword, and local variables cannot be modified using static.

What is the difference between non-static variables and static variables?
The two variables have different lifecycles.
Non-static variables exist with the creation of objects and are released with the collection of objects.
Static variables exist as the class is loaded and disappear as the class disappears.
Different call Methods
Non-static variables can only be called by objects
Static variables can be objects or classes.
Different aliases
Non-static variables are called instance variables.
Static variables are called class variables.
Different data storage locations
Non-static variables are stored in heap memory objects, also known as object-specific data.
Static variable data is stored in the static zone of the Method Area (data area ).

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.