Summary of Java advanced interview questions

This is the first part of an advanced Java interview series. This part discusses the core issues of Java, such as variable parameters, assertions, garbage collection, initializers, tokenization, dates, calendars and so on. Next, let's look at the specific problems.

1. What are variable parameters?

2. What is the purpose of the assertion?

3. When to use assertions?

4. What is garbage collection?

5. Explain garbage collection with an example?

6. When does garbage collection run?

7. Best practices for waste recycling?

8. What is an initialization block?

9. What is a static initializer?

10. What is an instance initialization block?

11. What is a regular expression?

12. What is tokenization?

13. Give an example of tokenization?

14. How to tokenize using scanner class?

15. How to add hours to a date object?

16. How to format date objects?

17. What is the purpose of calendar class in Java?

18. How to get an instance of calendar class in Java?

19. Explain some important methods in the calendar class

20. What is the purpose of number format class?

When we encounter these problems, how should we answer them? Please keep looking down.

What are variable parameters?

Variable parameters allow methods with different numbers of parameters to be called. Look at the summation method in the following example. This method can call 1 int parameter, 2 int parameters, or multiple int parameters

Number.

Purpose of assertion?

Assertions were introduced in Java 1.4. It allows you to test hypotheses. If the assertion fails (i.e. returns false), an assertion error is thrown (if assertion is enabled).

The basic assertions are as follows.

When to use assertions?

Assertions should not be used to validate input data to a public method or command line parameter. The illegalargumentexception would be a better choice. In the public method, only assertions are used to check what should not happen at all.

What is garbage collection?

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

Explain garbage collection with an example?

For example, the following method will be called from a function.

By 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 is executed, the reference variable calendar is no longer valid. Therefore, no reference to the object was created in the method.

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

When does garbage collection run?

Garbage collection runs on the whim and whim of the JVM (not that bad). Possible situations for running garbage collection are:

(1) The heap is low on available memory

(2) CPU idle

Best practices for garbage collection?

Programmatically, we can ask (remember that this is just a request -- not a command) the JVM to run garbage collection by calling the system. GC () method.

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

The finalize () method is run before the object is deleted from the heap by garbage collection. We recommend not writing any code with the finalize () method.

What is an initialization block?

Initialization data block -- code that runs when an object is created or a class is loaded.

There are two types of initialization data blocks:

(1) Static initializer: the code that runs when the class is loaded

(2) Instance initializer: the code that runs when a new object is created

What is a static initializer?

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

Sample output:

What is an instance initialization block?

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

Sample output:

What is a regular expression?

Regular expressions make it easy to parse, scan, and split strings. Regular expressions commonly used in Java -- pattern, matcher and scanner classes.

What is tokenization?

Tokenization refers to dividing a string into several substrings based on the separator. For example, separator; Split string AC; bd; def; E is the four substrings AC, BD, def and E.

The delimiter itself can also be a common regular expression.

String. The split (regex) function takes regex as an argument.

Give an example of tokenization?

How to tokenize using scanner class?

How do I add hours to a date object?

Now, let's look at how to add hours to a date object. All date operations on date can only be completed by adding milliseconds to date. For example, if we want to add 6 hours, we need to convert 6 hours into milliseconds. 6 hours = 6 * 60 * 60 * 1000 milliseconds. Look at the following example.

How do I format date objects?

Formatting the date needs to be done using the dateformat class. Let's look at a few examples.

The formatted date with locale is as follows:

What is the purpose of calendar class in Java?

Calendar Class (YouTube video link)- https://www.youtube.com/watch?v=hvnlYbt1ve0 )Used to process dates in Java. The calendar class provides a simple way to increase and decrease the number of days, months, and years. It also provides many details related to dates (which day of the year, which week, etc.)

How to get an instance of calendar class in Java?

The calendar class cannot be created by using new calendar. The best way to get an instance of the calendar class is to use the getInstance () static method in calendar.

Explain some important methods in calendar class?

It is not difficult to set the day, month or year on the calendar object. Call the appropriate constant set method for day, month or year. The next parameter is the value.

Calendar get method

To get information on a specific date - September 24, 2010. We can use the calendar get method. The passed parameters represent the values we want to get from the calendar - day or month or year or... The values you can get from the calendar are as follows:

What is the purpose of the number format class?

The number format is used to format numbers into different regions and different formats.

Use the number format of the default locale

Number format using locale

Format numbers using the Dutch locale:

Format numbers using the German locale:

Format currency using default locale

Format currency using locale

Format currency using Dutch locale:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>