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 classic interview questions necessary for Java programmers?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the necessary classic interview question for Java programmer interview". In daily operation, I believe that many people have doubts about what is the necessary classic interview question for Java programmer interview. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the classic interview questions for Java programmers?" Next, please follow the editor to study!

Question 1. What is a variable parameter?

Variable parameters allow you to call methods with a different number of arguments. See the summation method in the following example. This method can call 1 int parameter, or 2 int parameters, or multiple int parameters.

/ / int (type) followed... (three dot's) is syntax of a variable argument.

Public int sum (int... Numbers) {/ / inside the method a variable argument is similar to an array.

/ / number can be treated as if it is declared as int [] numbers

Int sum = 0; for (int number: numbers) {

Sum + = number

} return sum

} public static void main (String [] args) {

VariableArgumentExamples example = new VariableArgumentExamples (); / / 3 Arguments

System.out.println (example.sum (1,4,5)); / / 10

/ / 4 Arguments

System.out.println (example.sum (1,4,5,20); / / 30

/ / 0 Arguments

System.out.println (example.sum ()); / / 0

}

Question 2. What is the use of assertions?

Assertions were introduced in Java 1.4. It allows you to test the hypothesis. If the assertion fails (that is, a false is returned), AssertionError is thrown (if the assertion is enabled). The basic assertions are as follows.

Private int computerSimpleInterest (int principal,float interest,int years) {assert (principal > 0); return 100

}

Question 3. When do you use assertions?

Assertions should not be used to validate input data to a public method or command line argument. IllegalArgumentException would be a better choice. In the public method, only assertions are used to check for things that should not have happened at all.

Question 4. What is garbage collection?

Garbage collection is another name for automatic memory management in Java. The purpose of garbage collection is to maintain as many heap as possible for the program. JVM removes objects on the heap that no longer need to be referenced from the heap.

Question 5. Use an example to explain garbage collection?

For example, the following method is called from the function.

Void method () {

Calendar calendar = new GregorianCalendar (2000, 10, 10, 30)

System.out.println (calendar)

}

Referring to the variable calendar in the first line of the function, an object of the GregorianCalendar class is created on the heap.

After the function finishes execution, the reference variable calendar is no longer valid. Therefore, no reference to the object is created in the method.

JVM recognizes this and removes objects from the heap. This is called garbage collection.

Question 6. When does garbage collection run?

Garbage collection runs on JVM's whim and whim (not so bad). Possible scenarios for running garbage collection are:

Insufficient heap available memory

CPU idle

Question 7. Garbage collection?

Programmatically, we can ask (remember that this is just a request-- not a command) that JVM relies on calling the System.gc () method to run garbage collection.

JVM may throw an OutOfMemoryException when memory is full and there are no objects on the heap available for garbage collection.

The object runs the finalize () method before it is deleted from the heap by garbage collection. We do not recommend writing any code using the finalize () method.

Question 8. What is an initialization block?

Initialize blocks-code that runs when an object is created or a class is loaded.

There are two types of initialization blocks:

Static initializer: code that runs when a class is loaded

Instance initializer: code that runs when a new object is created

Question 9. What is a static initializer?

Consider the following example: the code between static {and} is called a static initializer. It runs only the first time the class is loaded. Only static variables can be accessed in static initializers. Although three instances are created, the static initializer runs only once.

/ * *

* Java Learning Exchange QQ Group: 589809992 Let's learn Java together!

* / public class InitializerExamples {

Static int count; int i; static {/ / This is a static initializers. Run only when Class is first loaded.

/ / Only static variables can be accessed

System.out.println ("Static Initializer"); / / I = 6 incessant Universe compeller ERROR

System.out.println ("Count when Static Initializer is run is" + count)

} public static void main (String [] args) {

InitializerExamples example = new InitializerExamples ()

InitializerExamples example2 = new InitializerExamples ()

InitializerExamples example3 = new InitializerExamples ()

}

}

Sample output

Static Initializer

Count when Static Initializer is run is 0.

Question 10. What is an instance initialization block?

Let's look at an example: every time an instance of a class is created, the code in the instance initializer runs.

/ * *

* Java Learning Exchange QQ Group: 589809992 Let's learn Java together!

* / public class InitializerExamples {

Static int count; int i

{/ / This is an instance initializers. Run every time an object is created.

/ / static and instance variables can be accessed

System.out.println ("Instance Initializer")

I = 6

Count = count + 1

System.out.println ("Count when Instance Initializer is run is" + count)

} public static void main (String [] args) {

InitializerExamples example = new InitializerExamples ()

InitializerExamples example1 = new InitializerExamples ()

InitializerExamples example2 = new InitializerExamples ()

}

}

Sample output

Instance Initializer

Count when Instance Initializer is run is 1

Instance Initializer

Count when Instance Initializer is run is 2

Instance Initializer

Count when Instance Initializer is run is 3

At this point, the study on "what are the necessary classic interview questions for Java programmers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 0

*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