Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the interview questions in Java development

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the interview questions in Java development". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the interview questions in Java development"?

I. the usage and function of the super () method?

There are the following Java classes:

Public class Bird {private String name; public Bird () {} public Bird (String name) {this.name = name;} public void walk () {System.out.println ("walking");} public String getName () {return name;} public void setName (String name) {this.name = name;}}

There is another class, Chicken, that inherits the above Bird class

Public class Chicken extends Bird {private String crest; public Chicken () {super ();} public Chicken (String name,String crest) {super (name); this.name = name;}. . }

In the second custom class Chicken, the super () method appears twice, namely super () and super (name). What is the meaning of super () and how do you explain it here?

Reference answer:

1. The constructor of the parent class must be called during the construction of the subclass.

two。 A subclass can use super () in its own constructor to call the constructor of the parent class.

(1) use this to call another constructor of this class.

(2) if you call super, you must write it on the * * line of the subclass constructor.

3. If the constructor of the subclass does not show up calling the constructor of the parent class, the system defaults to calling the constructor of the parent class without parameters.

4. If the constructor of the subclass does not show the constructor that invokes the parent class, and there is no constructor without parameters in the parent class, the compilation error occurs.

Then the super () that you have here is called the Bird () method of the above Bird class!

Super (name) this parameter is to call public Bird (String name) {

This.name = name; this method! Super () is the constructor that calls the parent class. In your example, there are two methods: Bird () and Bird (String name). Super () calls the Bird () constructor, and super (name) calls the Birth (String name) constructor. Note that super () calls the parent class constructor with the same number and type of parameters. Public Chicken (String name,String crest) {super (name); this.name = name;} should be repeated, super (name) should be this.name = name.

2. What is the difference between volatile and synchronized in Java?

The volatile variable in the Java language can be thought of as a "lighter synchronized"; the volatile variable requires less coding and less runtime overhead than the synchronized block, but what it can do is only part of the synchronized.

We know that the operation of setting the value of a variable in Java is atomic except for variables of type long and double, that is, there is no need to synchronize simple read and write operations on the value of a variable.

Prior to JVM 1.2, the memory model implementation of Java always read variables from main memory and did not require special attention. With the maturity and optimization of JVM, the use of the volatile keyword in a multithreaded environment becomes very important.

Under the current Java memory model, threads can store variables in local memory (such as machine registers) instead of reading and writing directly in main memory. This may cause one thread to modify the value of a variable in main memory, while another thread continues to use a copy of its variable value in the register, resulting in data inconsistency.

To solve this problem, you just need to declare the variable as volatile (unstable), as in this program, which indicates that JVM, this variable is unstable, every time it is used to read into the main memory. Generally speaking, the flags shared among tasks in a multitasking environment should be decorated with volatile.

Each time a Volatile-decorated member variable is accessed by a thread, it forces the value of the member variable to be reread from shared memory. Also, when a member variable changes, the thread is forced to write back the change value to shared memory. So that at any time, two different threads always see the same value of a member variable.

The Java language specification states that in order to achieve * speed, threads are allowed to keep private copies of shared member variables and compare them with the original values of shared member variables only when the thread enters or leaves the synchronous code block.

In this way, when multiple threads interact with an object at the same time, it is necessary to pay attention to the changes of shared member variables in time.

The volatile keyword is to prompt VM that you cannot keep a private copy of this member variable, but should interact directly with the shared member variable.

Usage suggestion: use volatile on member variables accessed by two or more threads. Do not need to be used when the variable to be accessed is already in the synchronized code block or is constant.

This keyword is inefficient because it blocks the necessary code optimizations in VM, so be sure to use this keyword only when necessary.

Note: if the current value of a simple variable declared as volatile is related to the previous value of the variable, the volatile keyword does not work, which means that none of the following expressions are atomic operations:

N = n + 1; nasty +

If you want to make this an atomic operation, you need to use the synchronized keyword, such as the code above can be changed to the following form:

Public static synchronized void inc () {nemesis;}

Change n=n+1 to inc (), where the inc method uses the synchronized keyword for method synchronization. Therefore, in the use of the volatile keyword to be cautious, not as long as a simple type of variable using volatile modification, all operations on this variable are the original operation, when the value of the variable is determined by its own previous, such as n=n+1, nasty +, etc., the volatile keyword will be invalid, only when the value of the variable has nothing to do with its last value, the operation on the variable is atomic level, such as n = m + 1, this is the original level. So be careful when using volatile. If you are not sure, you can use synchronized instead of volatile.

What is a mutex? What are the considerations for using mutexes?

1. For boys and girls, each girl is an object and every boy is a thread. Every girl has her own lock pool. Every boy may be waiting in the lock pool.

Class Girl {Public void hand () {} Public syncronized void kiss () {}} Class Boy extends Thread {Public void run () {}}

If there are too many lock tags, there will be a situation in which threads wait for other threads to release lock tags without releasing their own lock tags for other threads to run. It's a deadlock.

There are two ways to deal with deadlocks: uniformly arranging the lock order (solving the access to multiple shared resources in different methods).

The method of object 1

Synchronized (a) synchronized (b)

The method of object 2

Synchronized (a) synchronized (b)

2. Communication between threads (that is, coordination between threads)

The space used for communication between threads is called the object wait pool (wait pool), and the queue also belongs to the object space.

Enter the waiting pool

Using the wait () method in the Object class, in the run state, the thread calls wait (), which means that the thread will release all its lock marks and CPU occupancy while entering the object's wait pool. The state of the wait pool is also blocked, except that the thread releases its own lock flag.

Exit the waiting pool to enter the lock pool

Notify (): removes an arbitrary thread from the object's wait pool and places it in the lock pool, where the object waits until the object's lock tag can be obtained.

NotifyAll (): all threads waiting for that object are removed from the waiting pool and placed in the lock pool. Only the thread in the lock pool can obtain the lock token of the object, which allows the thread to continue running from the place where it was last interrupted by a call to wait ().

Note: only wait () and notify () can be performed on locked resources.

1) wait (): surrender the lock and the occupation of CPU

2) notify (): removes an arbitrary thread from the object's wait pool and places it in the lock pool, where the object waits until the object's lock tag can be obtained.

3) notifyAll (): all threads waiting for that object will be removed from the waiting pool and placed in the lock pool. Only the thread in the lock pool can obtain the lock tag of the object, which allows the thread to continue running from the place where it was last interrupted by the call to wait ().

Note: Vector and HashTable are thread-safe in the java.io package because each method is decorated with synchronized. The Static method can add a synchronized, and the lock is on the class object.

But Vector is jdk 1.0. ArrayList is jdk1.2, so practical applications still use ArrayList.

Example:

Producers and consumers

A certain number of products at a counter cannot be produced when the counter is full and cannot be bought in free time.

4. What are the classifications of streams in Java?

1) divided into byte stream and character stream according to data type

Byte stream class:

Abstract parent class: InputStream,OutputStream

Implementation classes include the following:

BufferedInputStream buffered flow-over-filtered BufferedOutputStreamByteArrayInputStream byte array flow-Node flow ByteArrayOutputStreamDataInputStream handles JAVA standard data stream-over-filtered DataOutputStreamFileInputStream processing file IO flow-Node flow FileOutputStreamFilterInputStream implements over-filtered flow-Byte overfiltered parent FilterOutputStreamPipedInputStream pipeline flow PipedOutputStreamPrintStream contains print () and println () RandomAccessFile supports random files

Abstract parent class: Reader, Writer

Implementation class:

BufferedReaderBufferedWriterPrintWriterCharArrayReaderCharArrayWriterFileReaderFileWriterFilterReaderFilterWriterInputStreamReaderOutputStreamWriterPipedReaderPipedWriterStringReaderStringWriter

2) divided into input stream and output stream in terms of data direction

InputXXXXX, OutputXXXXX

3) according to the function of flow, it is divided into node flow and filter flow.

The node stream is used to transmit data.

The filter flow is used to encapsulate the node flow or other filter flow, thus adding a function to the node flow or other filter flow.

5. The relationship between parent class and subclass in Java

The non-privatized properties of the parent class (subclasses of different packages cannot access the default modifier) and methods can inherit to the subclass by default.

Class Son extends Father {}

If the private method in the parent class is called by the subclass, the compilation error is reported.

The constructor subclass of the parent class can not be inherited, let alone the problem of overwriting.

Therefore, the subclass constructor calls the parent class's parameterless constructor by default. (so get into the habit of writing no-parameter constructions)

If the subclass accesses the parameterized constructor of the parent class, you must use super (parameter) on the line of the subclass constructor.

When constructing an object, the system first constructs the parent object, and then constructs the subclass object.

Public class BMWcar extends Car {Public BMWcar () {Super (int alength); / / explicitly calls the construction of the parent class, and the default invokes the no-parameter construct / / so if the parent class has no no-parameter construct, the subclass will report an error if it calls other constructs without display. The super here is a reference to the parent class}}

Sixth, Java multi-thread often meet with test questions

1. Define threads

1) extend the java.lang.Thread class.

There is a run () method in this class, and you should pay attention to its usage:

Public void run () if the thread is constructed using a separate Runnable running object, the run method of the Runnable object is called; otherwise, the method does nothing and returns.

A subclass of Thread should override this method.

2) implement the java.lang.Runnable interface.

Void run ()

When you create a thread using an object that implements the interface Runnable, starting the thread causes the object's run method to be called in a thread that executes independently.

The general convention of the method run is that it may perform any required operation.

2. Instantiate thread

1) if it is a thread that extends the java.lang.Thread class, you can simply new it.

2) if it is a class that implements the java.lang.Runnable interface, use the construction method of Thread:

Thread (Runnable target) Thread (Runnable target, String name) Thread (ThreadGroup group, Runnable target) Thread (ThreadGroup group, Runnable target, String name) Thread (ThreadGroup group, Runnable target, String name, long stackSize)

3. Start the thread

The start () method is called on the thread's Thread object instead of run () or anything else.

Before calling the start () method: the thread is in a new state, which means there is a Thread object, but there is not a real thread yet.

After calling the start () method: a series of complicated things happened

Start a new thread of execution (with a new call stack)

The thread transitions from the new state to the runnable state

When the thread gets a chance to execute, its target run () method runs.

Note: there is nothing special about the run () method for Java. Like the main () method, it's just that the new thread knows the name (and signature) of the method being called. Therefore, it is legal to call the run method on Runnable or Thread. But does not start a new thread.

4. Examples

1) A multithreaded example that implements the Runnable interface.

/ * * classes that implement the Runnable interface * * @ author leizhimin 2008-9-13 18:12:10 * / public class DoSomething implements Runnable {private String name; public DoSomething (String name) {this.name = name;} public void run () {for (int I = 0; I < 5; iTunes +) {for (long k = 0; k < 100000000; KGB +); System.out.println (name + ":" + I) } / * Test multithreaded programs implemented by the Runnable class * * @ author leizhimin 2008-9-13 18:15:02 * / public class TestRunnable {public static void main (String [] args) {DoSomething ds1 = new DoSomething ("A San"); DoSomething ds2 = new DoSomething ("Li Si"); Thread T1 = new Thread (ds1); Thread T2 = new Thread (ds2); t1.start (); t2.start ();}}

Execution result:

4Process finished with exit code 4: 0 A 3: 0 Li 4: 1 A 3: 1 Li Si: 2 Li 4: 3 A 3: 2 Li 4: 4 A 3: 3 A San: 4Process finished with exit code 0

2) an example of extending the multithreading implementation of the Thread class.

/ * Test the multithreaded program implemented by the extended Thread class * * @ author leizhimin 2008-9-13 18:22:13 * / public class TestThread extends Thread {public TestThread (String name) {super (name);} public void run () {for (int I = 0 int I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 256

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report