Basic knowledge in Java


A, Java operator precedence

Operators Associativity
[]. () (Method call) From left to right
! ~ + (Plus from) - (decrement) + (n) - (negative) From right to left
* / % From left to right
+ (Addition) - (subtraction) From left to right
<< >> >>>(移位) From left to right
<<=>> = (Comparison operator) the instanceof From left to right
==! = (Assignment operator) From left to right
& From left to right
^ From left to right
| From left to right
&& From left to right
|| From left to right
? :( ternary operator) From right to left
= From right to left

Second, the data type

Bit ( 'bit ): i.e., a binary bit, the computer is the smallest unit of information
byte ( Byte ): i.e., eight adjacent bits, referred to as B. Byte is a measure of the amount of information, the basic unit of information storage, the smallest unit, as well as commonly used in KB (kilobytes), MB (megabytes), GB (gigabytes), with its conversion relationship is:

1KB = 1024B 1MB = 1024KB = 1024*1024B

1GB = 1024MB = 1024*1024KB = 1024*1024*1024B

Computer smallest storage unit : bits bit -> bit b

1B = 8bit

1KB = 1024B

1MB = 1024KB

1GB = 1024MB …

Here Insert Picture Description
Here Insert Picture Description


Third, the assignment operator

Here Insert Picture Description


Fourth, the logical operators

Here Insert Picture Description
Short circuit and &&

a&&b -> If a is false, then b is not performed

Short circuit or ||

a||b -> If a is true, then b is not performed


Five, java is passed by value

- For changing the basic data types of the parameters, in the form of parameters, does not affect the value of the actual parameter
- changing a reference to the type of parameter, formal parameter values affect the actual parameters


by the method of passing a value of a reference type
if the method of changing the pass over reference value type and influential.

import java.util.Arrays;

public class MethodTran {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        changeArrValue(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void changeArrValue(int[] arr) {
        arr[0] = 0;
        arr[1] = 0;
        arr[2] = 0;
    }
}

Output:
Here Insert Picture Description

If the method of changing the address value pass over the reference type, no effect (see particular code)

public class TestCSDN {
    public static void main(String[] args) {
        int[] arr = new int[0];
        int[] tranArr = tranValue(arr);
        int[] arrCy = arr;
        System.out.println(Arrays.toString(tranArr));
        System.out.println(Arrays.toString(arrCy));
    }

    private static int[] tranValue(int[] arr) {
        arr = new int[3];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        return arr;
    }
}

? Whether the above output is [0, 1, 2] but it is not.
Output:

Here Insert Picture Description
Not to say good value passed it? Why method does not change the value of the array arr of it?

Look at the following chart analysis slag slag?
Here Insert Picture Description
Summary

Here Insert Picture Description
Source: "Java core technology Volume"


Sixth, method overloading -> Brother principle

6.1 Method overloading

  • A plurality of methods in the same class - 父亲(类)相同
  • The method has a plurality of the same method name - brothers姓氏(方法)相同
  • The method of the plurality of different parameters - between Brothers名字(参数)不同

    Way to distinguish different parameters:

    1. A different number of parameters, such as method (int) and method (int, int)
    2. Different types of parameters, such as method (int) and method (String)
    3. Different order parameters, such as method (int, String) and method (String, int)
      Note that a different order parameters of the premise must be in a different order on several parameters types can not be the same as
      , for example, if a method (int, int), then No matter how changing the order can not be distinguished

Method overloading no relationship with the Return Type

6.2 method overrides

Rewrite the condition is met


Seven members and local variables

into the new heap, left push

Enter 堆内存, it will open up a space, with default values
and 栈内存will not. You need to initialize local variables before use

  • Member variables (variables other than the method in the class): role in the whole class 堆内存
    - as member variables to create an object is created , with the disappearance of the object disappear
  • Local variables (method or form parameters): role in that process 栈内存
    - as a local variable called method created , along with the method call is completed disappear

Note:
If 成员变量the 局部变量of the same name, then the value will shield member variables and therefore the local variables of the same name and try not to member variables in the name!


Eight, string concatenation heap memory conditions

For string concatenation, first of all want +to connect, but will consume a lot of memory, the speed is very slow, it is recommendedStringBuilder

Analyze why +take up a lot of memory?
Here Insert Picture Description

From the above chart, the original string and string concatenation will not disappear, but if the constant pool does not exist, it will re-open a memory space to store the new string, and the string after stitching.

And StringBuilderin each call append () method, are the same object, so the strings and +stitching compared saves time! it is recommended StringBuilder类to operate a string splicing.


Nine, Java access modifiers

Access modifier
For the FIG can be understood (from: Java scope of public, private, protected, and when the difference is not written )

  • public : no matter whether it is the same package or whether it is inherited, can access.
  • Private : In addition to the current class can not use other

The above two easy to remember, the other two may think, first of all be clear

  1. Friend is the same package
  2. Whether or not in the same package, there is the family inheritance
  • The default (that is, do not write modifiers) : understood to be friendly (friendly) so in addition to their access to, is that friends can (ie under the same package).
  • protected : protection means, in addition to its protection, friends and family can protect you, so they can access the same package (friend), with a subclass inheritance can also access the (family).

Summarizes
1) only visible to the present class -private
2) visible to all classes -public
. 3) of the package and all subclasses visible -protected
. 4) of the package visible - no default modifier ()


Ten, final keyword

  • Is defined as a private, this can only be called class methods;
  • Defined as static, the only one stressed and is executed only once;
  • Defined as final, it indicates that it is a constant and can not be modified
  • final class can not be inherited, no sub-classes, methods, final class default is final, not including the member variables.

  • final methods can not override methods by subclasses, but can be inherited.

    1. The method of the parent class private member can not be overridden methods subclasses, so private final type of process is the default type.
  • final member variable represents a constant, can only be assigned once, after the value assigned does not change.

    1. If the basic data types of variables, values can not be changed after initialization.
    2. If a reference type variable initialization after not let it points to another object. However, you can change the value of the object pointed to members
  • method can not be used to modify the final construction.


XI and implicit parameter display parameters

Display parameters: parameters of the method in

a hidden parameter: class method calls the domain class instance. Such as this, the call object is initializing method or object is a hidden parameter; or super.

public class People{
	private String name;
	private int age;
	public People() {}
	public People(String name, int age) {
		this.name = name; // this为正在初始化的对象
		this.age = age;
	}
	public void setName(String name) {
		this.name = name; // this为调用该方法的对象
	}
}

Java foundation of classical problems: a





If you feel you find this helpful, I hope you can support what the point of praise. Of course I will continue to write something useful, I can focus on continuing to see behind the content. Thanks for the support.

Subtle movements, such as rose-like fragrance

He published 190 original articles · won praise 153 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_36852780/article/details/90314744