Java Interview Basics Issues integration

Source: Internet
Author: User
Tags aop static class volatile stringbuffer
Directory

Catalog 1

1, the difference between the integer and int in Java. 3

2, the difference between StringBuffer and string in Java. 3

3, Java species of thread safety and non-thread security differences: 4

4. How to ensure thread safety: 4

5. How to Implement thread safety: 4

6, how to Achieve thread synchronization: 5

7. Thread-Safe and unsafe collections: 5

8, single case mode: 5

9. Factory Mode: 7

10, bubble sort: 9

11. Quick Sort: 10

12, Spring AOP Implementation principle: 11

13, chain-type queue implementation principle: 12

14, the Java Virtual machine memory partition and function: 14

15, the time complexity of BST: 16

16, BST Java implementation of 16

17, the data structure to map the shortest path of two points 17

18, the Java Database Connection pool implementation and working principle: 19

19, the realization of the two-point search: 23

20, Java implementation of the full arrangement: 24

21, Java in the role of volatile 25

22, dynamic Agent: 26

23. Java garbage Collection mechanism (GC) 31

24. Difference between basic type (original type) and wrapper class (reference type): 34

25, HashMap and hashset principle of implementation: 34

26, the difference between HashMap and Hashtable: 36

27, the acid:36 of the database

28. Java Thread Pool: 37

29. Four ways to resolve the conflict between Kazakhstan and Greece: 41

30. What are the methods in the object class: 41

31, the memory overflow solution Method 42

32, string turn out int type, how to judge can turn. How to turn. 43

33, the principle of Multithreading: 43

34, the principle of data Warehouse: 44

35, map have what kind: 45

36, HashMap iterations three ways: 46

37, equals and "=" The Difference 47

38, looking for a small number of k, to explain the complexity of. 47

39, the concept and characteristics of the index; 48

40, using the Linux command to find a file in a ip/string occurrence of the number of times: 49

41. Re-entry lock, Object lock, class lock relationship 51

42, Spin lock 52

43, Stack memory overflow reason: 53

44, bidirectional linked list before inserting, the end inserts, the finger position inserts the realization. 53

45, abstract class and interface difference: 56

46, TCP and UDP difference: 57
1, the difference between the integer and int in Java.

1 int is the basic type, and integer is the packing class for int package, that is class

2 a variable declared as int does not need to be instantiated, and a variable declared as Interger needs to be instantiated

3 The default default value of int is null for 0,integer default.

4 int is mainly used in numerical calculation and parameter transfer value, the integer is mainly used in the object reference, such as map, list, set value 2, Java StringBuffer and string differences.

1 StringBuffer is a string variable, string is a literal constant

2 The contents of the StringBuffer object can be modified, and the string object cannot be modified once it is produced

3 each action on string generates a new string object, which is actually two objects, and string operates by changing the assignment address instead of changing the value operation.

4 The StringBuffer object has a certain buffer capacity, the string size exceeds the capacity, automatically increase the capacity of 16bye

5 The StringBuffer class approach is multithreaded and secure, while StringBuilder is not thread-safe

6) Running speed:StringBuilder > StringBuffer > String

7 String Advantage The compiler can set the string to be shared; it is often necessary to modify a string, such as INSERT, Delete, and so on, and use StringBuffer to fit

8) Common methods of StringBuffer:

Sb.append (TRUE); End Add True variable

Sb.deletecharat (1); Delete 1-bit

SB. Delete (1,4)//1 to 3;

Sb.insert (4, "false");//4-bit insert false

Sb.reverse (); Reverse

Sb.setcharat (1, ' D '); 1-bit change to D

Sb.length (); Length

Sb.replace (0,1, "QQQ"); 0 to 1 replacement for QQQ

Sb.tostring ()//Convert to String: 3, the difference between thread-safe and non-thread-safe for Java species:

1 Thread-Safe judgment: Multiple threads in a process running this code at the same time, if the results of each run and single-threaded run the same, and the value of other variables is the same as expected, is thread-safe.

2 thread safety is divided into five parts:

A. Immutable objects: String Integer

B. Thread safety: how the Pipe runtime environment is arranged, and the threads do not require any additional synchronization

C. Conditional thread Safety: Hashtable, Vector; separate operation thread safety, some operations require external synchronization

D. Thread Compatibility: ArrayList, HASHMAP, not thread-safe, can be safely used in concurrency through synchronization

E. Thread antagonism: A class that cannot be rendered in a concurrent security, whether or not an external synchronization is invoked; call System.setout ()

3 Thread safety principle: Using the lock mechanism, when a thread accesses a data of the class, protect, the other process cannot access it until the process is read. There is no data inconsistency or data contamination. 4. How to ensure thread safety:

1) do not share state variables between threads

2 Change the state variable to an immutable variable

3 implement synchronization 5 when accessing the state variable , how to implement thread safety:

1 Use the Synchronized keyword to obtain the lock implementation synchronization. (Java objects have a built-in lock)

2) synchronization method decomposition. (using a dynamic lock-release-lock-release method) synchronized keywords are used in code blocks instead of methods, which are released to other methods when the method is not in use, and are then brought back when needed.

3) Inner class. (a method can construct a nested class when it needs to call a class periodically)

4 Event-driven processing. (Use the Wait (), notify (), and Notifyall () method)

5 access to slow resources-files, directories, network sockets, and databases-methods, placed in a separate thread, preferably outside any synchronized code 6, how to achieve thread synchronization:

1 method of synchronized keyword modification (synchronous method)

2 Synchronized keyword-decorated statement block (Sync code block)

3 Call the inner class and create the thread.

4 Implement thread synchronization using special domain variables (volatile) and add volatile keywords to the synchronization variable.

5 The Java.util.concurrent package is added to the JavaSE5.0 to support synchronization. Re-entry Lock (Reentrantlock Class)

6 Use the Wait (), notify () and Notifyall () method 7, thread-safe, and unsafe collections:

Thread Safety

thread is not secure

Vector

Arraylist

StringBuffer

StringBuilder

HashTable (null values are not allowed)

HASHMAP (allow null value), HashSet

Statck (inherited vector)

TreeSet (sort), TreeMap

8. Single Case mode:

A singleton pattern is a mode method that ensures that a class has only one instance and provides a global access point for the entire system.

a hungry man: An instance is already created before the method is called. Thread Safety

1.///A Hungry man type single case class. is instantiated when class is initialized

2. public class Singleton1 {

3. Private Singleton1 () {}

4. Private static final Singleton1 single = new Singleton1 ();

5.//Static Factory method

6. public static Singleton1 getinstance () {

7. return to single;

8.}

9.}

Lazy Singleton: Creates an instance when a method call gets an instance. Thread not secure

How to make it thread-safe:

1method plus synchronized keyword

· Lazy single case class. Instantiate yourself at the first call

· public class Singleton {

· Private Singleton () {}

· private static synchronized Singleton single=null;

· Static Factory method

· public static Singleton getinstance () {

· if (single = null) {

· Single = new Singleton ();

· }

· return single;

· }

· }

2) double check lock

· Lazy single case class. Instantiate yourself at the first call

· public class Singleton {

· Private Singleton () {}

· private static Singleton Single=null;

· Static Factory method

· public static Singleton getinstance () {

· if (single = null) {

Synchronized (Singleton. Class) {

if (single = null) {

· Single = new Singleton ();

}

· }

}

return single;

· }

· }

3 Static internal class (not only solves the security, but also avoids the performance problem)

1. public class Singleton {

2. private static class Lazyholder {//Static nested class

3. Private static final Singleton INSTANCE = new Singleton ();

4.}

5. Private Singleton () {}

6. public static final Singleton getinstance () {

7. return lazyholder.instance;

8.}

9.}

a hungry man singleton mode: instantiating a static object while it is being created. Always take up memory. Thread safety.

Lazy Singleton mode: An object is instantiated the first time it is used. Security can be achieved through three of these forms.

If you do not need to pass the parameters to the outside through the constructor, use the A Hungry man type, otherwise use the lazy type. When using a lazy type, remember to consider thread-safety issues 9, Factory mode:

When you create an object, you encounter: You may need to compute or get the initial settings of the object; Select which child object instance to generate; Or you must generate some accessibility objects before you can generate the objects you need.

Building a factory model will solve the problem.

The factory model can be divided into three categories:

1) Simple Factory mode (easy Factory)
2 Factory Mode (Factory method)
3 Abstract Factory Model (abstracts Factory)

differences between the factory method pattern and the abstract factory pattern:

Factory method Mode:
an abstract product class that can derive multiple specific product classes.
An abstract factory class that can derive multiple specific factory classes.
Each specific factory class can create only one instance of a specific product class.
Abstract Factory mode:
multiple Abstract product classes, each abstract product class can derive multiple specific product classes.
An abstract factory class that can derive multiple specific factory classes.
Each specific factory class can create instances of multiple specific product classes.


Simple Factory mode


Factory method Mode


Abstract Factory mode 10, bubble sort:

Class test{

public void Mpsort (int []a,int N) {

Int temp;

for (int i=0;i<n;i++) {

for (int j=n-1;j>i;j--) {

If (A[j]<a[j-1]) {

TEMP=A[J-1];

A[J-1]=A[J];

A[j]=temp;

}

}

}

}

Optimized bubble sort (add a judgment to judge whether the back is ordered)

Class test{

public void Mpsort (int []a,int N) {

int temp;

int f=1;

for (int i=0;i<n&&f;i++) {

f=0;

for (int j=n-1;j>i;j--) {

if (A[j]<a[j-1]) {

TEMP=A[J-1];

A[J-1]=A[J];

A[j]=temp;

f=1;

}

}

}

}


11. Quick Sort:

public class test{

public static void sort (int array[],low,high) {

int i;

Int J;

int index;

if (Low>high) {

RETURNN;

}

I=low;

J=high;

Index=array[i];

while (I<J) {

while (I<j&&array[j]>=index)

j--;

if (I<J)

ARRAY[I++]=ARRAY[J];

while (I<j&&array[i]<index)

i++;

if (I<J)

Array[j--]=array[i];

}

Array[i]=index;

Sort (array,low,i-1);

Sort (Array,i+1,high);

}

public static void quicksort (int array[]) {

Sort (array,0,array.lengh-1)

}

}


12. Spring AOP Implementation principle:

AOP is aspect-oriented programming

OOP is Object oriented programming

AOP is equivalent to the complement and perfection of OOP

The role of AOP is to isolate the various concerns in the system, separating the core concerns from the crosscutting concerns .

core concern: The main process of business processing;

crosscutting concerns: not related to business logic processes, often occurring at many points in the core focus, and are basically similar everywhere. such as permission authentication, log, transaction processing.

AOP represents a horizontal relationship, if "object" is a hollow cylinder, which encapsulates the object's properties and behavior, so the aspect-oriented programming approach, like a razor, cuts these hollow cylinders open to get inside the message.

the technology to implement AOP is divided into two major categories :

The first is to use the Dynamic Agent technology , using the method of intercepting the message to decorate the message, in order to replace the execution of the original object behavior;

The second is to introduce a specific syntax to create "aspects" by means of static weaving , so that the compiler can weave the "aspect" code during compilation.

AOP Usage Scenarios

AOP with

Related Article

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.