Java Interview ② Basics section

Source: Internet
Author: User
Tags connection pooling thread class

2.1.1 A brief talk on the cross-platform principle of Java

Because the instruction set supported by each operating system (Windows,linux) is not exactly the same, it will allow our program to execute different program code on different operating systems, Java has developed a Java virtual machine for different operating systems and bits to shield the differences between the systems, providing a unified interface, For our Java developers, you only need to install a different Java virtual machine on different systems, and your Java program can run Java programs on all operating systems as long as you follow the Java specification.

Java through different systems, different versions, different bits of the Java Virtual Machine (JVM), to block different system instruction set differences and unified Interface (Java API), for our common Java developers, only need to follow the interface development, if my system needs to deploy a different environment , you only need to follow the corresponding version of the virtual machine on the system.

2.1.2 Steps to build a Java development environment

What does the Java development environment need?

1. JDK for our development environment

2. Corresponding development environment eclipse

3. Requires a Web server (tomcat)

1), download the corresponding components

2), installation

JDK, install the normal process, configure our Java_home, because later Eclipse and Tomcat will rely on this variable.

Eclipse Normal decompression on OK, set the default encoding of workspace

Tomcat normal decompression on OK, the integration of Tomcat into Eclipse, install plug-in OK,

....

Svn/git

2.1.3 about how many bytes of int data in Java

What are some basic data types in Java?

8 kinds

2.1.4. What are the aspects of object-oriented features

There are four basic features: encapsulation, abstraction, inheritance, polymorphism

1) Encapsulation, the object is encapsulated into a highly autonomous and relatively closed individual, the object state (attributes) by the object's own behavior (method) to read and change.

Zhang San this person, his name and other attributes, to have the method of acquiring and changing which he provides himself to operate, (Getter,setter)

2) abstraction is the identification of similarities and commonalities of things, which are then categorized into a class that only considers similarities and commonalities of these things, and ignores those aspects that are irrelevant to the current subject and goal, focusing on the aspects related to the current goal.

is to abstract the objects of real life into classes.

3) When defining a class, it can be done on the basis of an existing class, the content defined by the already existing class as its own content, and can add a number of new content, or modify the original method to make it more suitable for special needs, this is the inheritance

4) polymorphism refers to the specific type of reference variable defined in the program and the method call issued by the reference variable is not determined at programming time, but is determined during the program's run, that is, a reference variable that the bottom will point to which class of the instance object, the reference variable emitted by the method call is exactly what class implemented in the method, Must be determined by the time the program is running.

A reference variable of a parent class or interface can point to a subclass or an instance object of a specific implementation class, and the method called by the program is dynamically bound during run time, that is, the method that refers to the specific instance object to which the variable is pointing, that is, the method of the object that is running in memory, not the method that references the variable's type

Principles: When it comes to comparing abstract issues, illustrate

2.1.5 you have a basic data type, why would you want to wrap the type?

Basic data types, 8 basic data types are available in Java, Boolean int float, etc.

Package Type: Each basic data type will be one by one for one package type,

Boolean-boolean

Int-integer

Packing and unpacking

Convert the basic data type to the corresponding package type,

Integer i = 1; auto-boxing, which in fact calls the Integer.valueof method to boxing at compile time

Unpacking: Converts the wrapper type to the base data type, the base data type name = the corresponding wrapper type.

Int J = I automatic unpacking

Int J = i.intvalue () Manual unpacking

Auto-unpacking: Actually in the compilation call Intvalue

Java is an object-oriented language, and the basic data types do not have object-oriented features.

Null Integer-->null int-->0 represents the ID of the person class with an Integer and an int, respectively

Max Maximum Value

Min min value

Cache Value: Object cache, Integer i = 1, Integer j = 1;--> i = = j;

Cache -128~127

2.1.6 say what's the difference between "= =" and Equals method

Very classic one-face question? Tell me one thing first, another one?

= = is used to determine whether the values between the two variables are equal, the variables can be divided into basic data type variables, reference types, if the basic data type of variables directly compare values and reference types to compare the corresponding reference to the first address of the memory.

equals: Used to compare whether the two objects are the same length, judging if some of the two objects are the same. is actually a comparison of the calling object's Equals method.

2.1.7 tell me the difference between string and StringBuilder, the difference between StringBuffer and StringBuilder?

1) provides three classes in Java string, StringBuilder, StringBuffer to represent and manipulate strings, which are a collection of multiple characters.

String is the immutable string of content, and STIRNG uses an immutable array of characters (final char[]).

String str = new String ("BBB"); ----Private final char value[]

While StringBuilder and StringBuffer are strings that can be changed, StringBuilder and StringBuffer are using a mutable array (not decorated with final)----Char value[]

2) The most classic is stitching strings

1.String stitching string c = "a" + "B";

2.StringBuilder or StringBuffer

Striingbuilder sb = new StringBuilder (); Sb.append ("a"). Append ("B");

Splicing string cannot be spliced using string, use Stirngbuilder or Stringbudder

3) StringBuilder is thread insecure and efficient, while stringbuffer is thread-safe and less efficient.

2.1.8 the Java Collection

The collection in Java is Value,key-value (MAP) Two kinds of

The stored value is divided into list and set

The list is ordered and repeatable.

Set is unordered and non-repeatable. Judging by Equals and hashcode, the Hashcode method must be overridden if an object is to be stored in the set.

Save Key-value as Map

The difference between 2.1.9 ArrayList and Linketset

List of commonly used ArrayList and LinkedList. Distinguishing and using scenarios

The ArrayList is used in arrays, and LinkedList uses a linked list.

Array queries have index query-specific elements that are faster, while insertions and deletions and modifications are slow (arrays are contiguous memory in memory, if deleting inserts requires moving memory)

Linked list does not require memory is continuous, in the current element to hold the next or previous address, the query needs to start from the head, one by one, so inefficient, inserted without the need to move the memory, just change the point, so the insertion efficiency or delete high efficiency.

ArrayList use more in queries, but with fewer insertions and deletions, while LinkedList uses fewer queries and inserts and deletes in more cases

2.1.10 tell the difference between HashMap and Hashtable? HashMap and Concurrenthashmap, but don't.

1) Both HashMap and Hashtable can be used to store key-value data.

2) HashMap can be null as a key or value, and Hashtable is not possible

3) HashMap is thread insecure, high efficiency. Hashtable is thread-safe and less efficient.

By dividing the entire map into n segment (similar to Hashtable), the same thread safety can be provided, but the efficiency increases by N times and the default is 16 times times higher.

2.1.11 a tool class that implements a copy file uses a byte stream or a character stream.

The files we copy are not sure to contain only the character stream, there may be a byte stream (picture, sound, image, etc.). To consider commonality, use a byte stream.

2.1.12 tell me a few ways to implement a thread?

1. Implementation method:

1) Implement a thread by inheriting the thread class.

2) One thread by implementing the Runable interface

Inherited extensibility is not strong, Java only supports single inheritance, if one class inherits the thread, it cannot inherit other classes.

2. Starting mode:

Thread t = new Thread () inherits the thread's object/implementation Runable object.

T.setname () sets a thread name.

T.start ();

The start thread uses the Start method, and the Run method is executed after it is started.

3. How to differentiate threads, there are many threads in a system, each thread will print the log, I distinguish which thread prints what to do.

T.setname () "Set a thread name"; This is a specification that requires setting a name after the thread is created.

2.1.13 have you ever used a thread concurrency library?

A simple look. Creates a thread pool.

Java provides four static methods through Excute to create four thread pools, respectively:

Newcachedthreadpool creates a cacheable thread pool that, if the thread pool length exceeds the processing needs, flexibly reclaims idle threads and is not recyclable, then creates new threads.

Newfixedthreadpool creates a thread pool that controls the maximum number of concurrent threads, and the excess threads wait in the queue.

Newscheduledthreadpool creates a fixed-line pool that supports timed and recurring task execution.

Newsinglethreadexecutor creates a single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority).

The role of the thread pool:

1) Limit the number of threads, which will not cause the system to run slowly or crash due to excessive threads.

2) The thread pool does not need to be created or destroyed every time, saving resources.

3) thread pool does not need to be created each time, response time is fast.

Connection pooling is the same

2.1.14 tell me what is design mode? What are the common design patterns?

Design pattern is through the previous countless practice summary, the design process can be reused, can solve the specific problem design method.

Singleton mode: Lazy mode, a hungry man mode

1) The construction method is privatized, so that in addition to their own class can be created elsewhere can not create

2) Create a single instance in your own class (The A Hungry man mode is the one that creates a single instance, and the lazy pattern is created when needed)

3) provides a way to get the instance object (creation requires method synchronization)

Factory mode: Spring IOC uses Factory mode

The creation of the object is given to a factory to create

Proxy mode: Spring AOP is the dynamic proxy used

Packing mode:

Java Interview ② Basics section

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.