Basic problems of Java

Time:2021-2-22

1long(8) and float(4)Who is the largest value range?

becauselongyes8Bytes,floatyes4It’s a byte. As a rule, we can’t move up. But becausefloatIt is a floating-point type, and its calculation method is different from that of shaping

First of alllongCan be automatically converted tofloat;

For example:

Long number=88888;//88888

Float newNumber=number;//88888.0

longRange of values indicated:2^63-1

floatRange of values indicated:3.4 * 10^38

longandfloatAll of the storage structures are inconsistent:

1 longIs an integer type, directly108Bit storage;

2 floatFloating point type, binary conversion with decimals, stores significant digits (scientific counting method)

   

2The characteristics of XOR operators are as follows

^Features: one data bit XOR twice to another data bit, the number itself does not change.

int a = 1;

int b = 2;

a = a ^ b;

b = a ^ b;//b = 1

a = a ^ b;//a = 2

The essence of XOR operator isConvert the decimal system to binary, and then the same bit is the same0,The difference is1.

decimal system

Binary

1 = 0* 2^1  +  1 * 2^0

01

2 = 1 * 2^ 1 +  0 * 2^0

10

11= 1 * 1^2 + 1 * 1^1 =3

11

101= 1 * 2^2 + 0 +1 * 2^0=5

101

   

   

3, bit operation efficiency is the fastest, its core is binary

For example:2 * 8 = 2 * 2^3 = 2 << 3

   

4switchsupportbyte/lang/String(1.8).

   

5continuebreak return What’s the difference?

Continue Jump out of this loop and only use it in the loop.

Break : ends the current loop. It can only be used in the loop.

Return Terminate the current method, not just the loop.

   

6Three characteristics of object-oriented

Encapsulation, inheritance and polymorphism

   

7, array

One dimensional array initialization:

String[] strs = new String[x]. xIt’s a fixed lengthJust a statementstrsCount RegxString array of

String[] strs = {strs1,strs2…}; Declare and initialize

Initialization of two-dimensional array: a two-dimensional array is a collection of one-dimensional arrays.

String[][] strs = {arr1,arr2…}; arr1:Index group

String[][] strs = new String[4][3]; 4: index group length,3: refers to the maximum length (tested) that contains each array,If you assign a value to a two-dimensional array directly, you will report an error. It’s useless at other times.

String[][] strs = new String[4][];[]The value assigned to represents lengthening.

   

8, inherit execution order

   

   

   

   

   

Execution sequence:father_static > son_static >father_codePiece>father_comnstructor>son_codePiece>son_constructor

Note: the static code block is called only once, and the construction code block is executed once before the construction method is executed.

   

9Overloading and rewriting of methods

Overload Overload: overload can change the type of return value. The overload of method only depends on the different parameter list.

Override Rewriting: generally, the subclass rewrites the parent class method, and the rewriting is only related to the return value type. The return value of a subclass method can only be a subclass of the return value of a parent class.

   

10Abstract methods and interfaces

keyword

abstrct

Interface

definition

An abstract class cannot create an instance, it can only be inherited as a parent class. Abstract class is the parent class abstracted from many concrete classes, which has a higher level of abstraction. An abstract class is abstracted from many classes with the same characteristics, and this abstract class is used as the template of its subclass, so as to avoid the randomness of subclass.

Interface is just a behavior specification, and it is a requirement to meet the rule to use

method

Abstract class can but does not have to have abstract properties and abstract methods, but once there are abstract methods, this class must be declared as an abstract class

Only method declaration, not method body

rule

A specific derived class must cover the abstract methods of the base class. If it does not cover all the abstract methods, then the derived class is still an abstract class

  

Fields cannot be included in an interface(field), constructor, destructor, static member

Construction method

There can be

There can’t be

Common member variable

There can be

There can’t be,There can be constants

Static method

There can be

There can’t be

inherit/realization

OneclassOnly one abstract method can be inherited

OneclassMultiple interfaces can be implemented simultaneously

Abstract method modification

Can’t compete withprivate(subclass call)\final(final state attribute)/method/Category)\staticUse together

Can’t compete withprivateprotectedfinalUse together

   

realization/After Succession

Inheritance abstract methods can implement or not implement abstract methods

To implement an interface class, you must implement every interface method. But the abstract method can implement the interface class, at the same time, it can not implement the interface method. Leave it to subclasses to implement.

  

  

   

11finalandstatic

Final:

Modify class, class cannot be inherited

Modify method. Method cannot be overridden

Modify a variable, and the variable becomes a constant, which can only be assigned once.

Naming conventions for constants:

Public static final String STRING_NAME = “zhangsan”;

Modify the variable of the reference class:

 

static:

Load as class loads

Prior to object existence

Shared by all objects of the class

·If a member variable is shared by all objects, it can be defined as static.

   

① The static method can be used directlyclassName.staticFunction()call

You can also use objects.staticFunction()call

   

Static members cannot access regular members, but regular members can access static members

   

③ There are no static methodsthisKeywords, becausethisKeywords exist as objects are created

If a class is full of static members, it is recommended to set the constructor of the class toprivate.

   

12package import class The order relation of

order package > import > class

Package : The package of the current class has and must only have1individual

Import : other classes introduced can havenindividual

Class:Modifying the current class. Must have.

   

13, outer class and inner class

   

    public static void main(String[]args){

        Person.Sonps = new Person().newSon();

            ps.showNum();

        }

   

14, anonymous inner class

    Public interface A{

        Void show();

    }

      

    Public class B {

        //Fill in the blanks

 Public static A aFunction(){

            Return new A(){

                @override

                Show(){

                    Syso(“helloworld”);

                }

            }

        }

    }

      

    Class Test{

        Main{

            B.aFunction.show();//Will output“helloWorld”

        }

    }

   

15StringThe characteristics of the system.

Cannot be inherited because it is inheritedfianlModify, and there are no subclasses.

   

16==andequalsThe difference between

== The comparison basic data type is literal quantity, and the comparison reference data type is memory address

Equals: Only reference data types can be compared if the object is not overriddenequalsMethods, then and==There is no difference (literal quantity).

After rewriting, it generally compares every attribute in the object.

17, abnormal

Try…catchandthrowsThe differences between them are as follows:

Try…catchThe program will not be interrupted, andthrowsWill interrupt the program. Usually you can handle it yourselftry…catch,Use it if you can’t handle itthrows.

   

throwandthrowsThe differences between them are as follows:

theowIn generalcatch() inside

Throws-Move behind the method

18JVMIs it multithreading?

yes

1, main programmainMethod start

2finalizaThe garbage collection mechanism will also be activated

   

19, adapter mode(adapter

Overview: without modifying the original2In this case, they can cooperate through transformation.

Definition: transform the interface of a class into the interface expected by customers.

The configuration modes are3Species:

Target: Expected interface(USB

Adaptee:Adapted interface(PS2)

AdapteeSon:Implement the adapted interface

Adapter adapter class

Class Adapter

Interface Target:

>> void usb();//There is no method body here

Interface Adaptee:

>>void ps2();//There is no method body here

AdapteeSon implement Adaptee:

>>void ps 2(){};

Adapter extends Adaptee implements Target :

>>void usb(){

Super.ps2(); //Implementation interfaceyesDirect useImplementation method of parent class (configured)

}

object adapter

Target:

>>void usb(){};

Adaptee:

>>void ps2(){};

Adapter extends Target:

Adaptee adptee;

Public Adaptee(Adapter adapter){this.adapter = adapter}

@overload

Void usb(){

Adapter.ps2();

}

③ Interface adapter

In short, it isThere are two kinds of interfacesnThere are three ways

And it happens that you’re going to use it23There are three ways

At this point, the implementation of the interface is to implement all the methods in the interface, which is certainly not ideal.

thereforeThere is the interface adapter:

Define aabstrctClass, implementing implementsClass. But only implement what you need to use23There are two ways.

Define an adapterAdapterClasses, inheritingabstrctClass. That’s itokIt’s too late.

   

  

advantage

shortcoming

Adapter

1Through the adapter, you can call the unified interface, which is simpler and more direct

2Decouple the target class and the adapter class, and reuse the adapter class without modifying the source code by introducing an adapter class.

3One object adapter can adapt to multiple adaptersAdaptee

1For replacement adapter, the implementation of object adapter is more complex

2

Application scenarios

1, the system needs to use the class, but theseClass does not conform to the interface of the system

22Classes are similar, but have different interfaces

3Using third-party plug-ins, the definition of plug-in interface is different. At the same time, you don’t want to modify your completed class.

  

   

Recommended Today

MySQL partition

MySQL partition Advantages of partitioning Store more data To optimize the query, only scan one or more necessary partitions. For count () and sum (), only summarize the partition statistics For data that is out of date or does not need to be saved, partitioning is faster Data queries are distributed across multiple disks to […]