In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 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 hot advanced Java interview questions". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the hot advanced Java interview questions"?
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}
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;}
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.
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.
Use an example to explain garbage collection?
For example, the following method is called from the function.
Void method () {Calendar calendar = new GregorianCalendar (2000); System.out.println (calendar);}
An object of the GregorianCalendar class is created on the heap by referring to the variable calendar in the line code of the function *.
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.
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
The practice of garbage collection?
Programmatically, we can ask (remember that this is just a request-- not a command) for JVM to run garbage collection by calling the System.gc () method.
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.
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
What is a static initializer?
Consider the following example: the code between static {and} is called a static initializer. It runs only when the class is loaded * * times. Only static variables can be accessed in static initializers. Although three instances are created, the static initializer runs only once.
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; "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.
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.
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
What is a regular expression?
Regular expressions make parsing, scanning, and splitting strings very easy. Regular expressions commonly used in Java-Patter,Matcher and Scanner classes.
What is tokenization?
Tokenization refers to the division of a string into several substrings based on the delimiter. For example, the delimiter; the split string ac;bd;def;e is divided into four substrings ac,bd,def and e.
The delimiter itself can also be a common regular expression.
The String.split (regex) function takes regex as an argument.
Give an example of tokenization?
Private static void tokenize (String string,String regex) {String [] tokens = string.split (regex); System.out.println (Arrays.toString (tokens));} tokenize ("ac;bd;def;e", ";"); / / [ac, bd, def, e]
How do I tokenize using the scanner class (Scanner Class)?
Private static void tokenizeUsingScanner (String string,String regex) {Scanner scanner = new Scanner (string); scanner.useDelimiter (regex); List matches = new ArrayList (); while (scanner.hasNext ()) {matches.add (scanner.next ());} System.out.println (matches);} tokenizeUsingScanner ("ac;bd;def;e", ";"); / / [ac, bd, def, e]
How do I add hours (hour) to a date object (Date Objects)?
Now, let's see how to add hours to a date object. All date operations on date need to be done 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. Take a look at the following example.
Date date = new Date (); / / Increase time by 6 hrs date.setTime (date.getTime () + 6 * 60 * 60 * 1000); System.out.println (date); / / Decrease time by 6 hrs date = new Date (); date.setTime (date.getTime ()-6 * 60 * 60 * 1000); System.out.println (date)
How do I format a date object?
Formatting the date needs to be done using the DateFormat class. Let's look at a few examples.
/ / Formatting Dates System.out.println (DateFormat.getInstance (). Format (date)); / 5:18 on 10-16-12, the format date of AM with locale is as follows: System.out.println (DateFormat.getDateInstance (DateFormat.FULL, new Locale ("it", "IT")) .format (date)) / / marted "16 ottobre 2012 System.out.println (DateFormat.getDateInstance (DateFormat.FULL, Locale.ITALIAN) .format (date)); / / marted" 16 ottobre 2012 / / This uses default locale US System.out.println (DateFormat.getDateInstance (DateFormat.FULL) .format (date)); / / Tuesday, October 16, 2012 System.out.println (DateFormat.getDateInstance () .format (date)) / / Oct 16, 2012 System.out.println (DateFormat.getDateInstance (DateFormat.SHORT) .format (date)); / 10-16-12 System.out.println (DateFormat.getDateInstance (DateFormat.MEDIUM) .format (date)); / / Oct 16, 2012 System.out.println (DateFormat.getDateInstance (DateFormat.LONG) .format (date)); / / October 16, 2012
What is the purpose of calendar class (Calendar Class) in Java?
The Calendar class (Youtube video link-https://www.youtube.com/watch?v=hvnlYbt1ve0) is used in Java to process dates. The Calendar class provides easy ways to increase and decrease the number of days, months, and years. It also provides a lot of date-related details (which day of the year? What week? Etc.)
How do I get an instance of the calendar class (Calendar Class) in Java?
The Calendar class cannot be created by using new Calendar. The * way to get an instance of the Calendar class is to use the getInstance () static method in Calendar.
/ / Calendar calendar = new Calendar (); / / COMPILER ERROR Calendar calendar = Calendar.getInstance ()
Explain some important methods in the calendar class (Calendar Class)?
It is not difficult to set the day (day), month (month), or year (year) on the Calendar object. Call the set method of the appropriate Constant on Day,Month or Year. The next parameter is the value.
Calendar.set (Calendar.DATE, 24); calendar.set (Calendar.MONTH, 8); / / 8-September calendar.set (Calendar.YEAR, 2010)
Calendar get method
To get information on a specific date-September 24, 2010. We can use the calendar get method. The parameters that have been passed represent the values we want from calendar-day or month or year or... Examples of values you can get from calendar are as follows:
System.out.println (calendar.get (Calendar.YEAR)); / 2010 System.out.println (calendar.get (Calendar.MONTH)); / / 8 System.out.println (calendar.get (Calendar.DATE)); / / 24 System.out.println (calendar.get (Calendar.WEEK_OF_MONTH)); / / 4 System.out.println (calendar.get (Calendar.WEEK_OF_YEAR)); / / 39 System.out.println (calendar.get (Calendar.DAY_OF_YEAR)) / / 267System.out.println (calendar.getFirstDayOfWeek ()); / / 1-> Calendar.SUNDAY
What is the purpose of the number formatting class (Number Format Class)?
Numeric formats are used to format numbers into different regions and formats.
Use the default locale number format
System.out.println (NumberFormat.getInstance (). Format (321.24f)); / / 321.24
Use locale number format
Format numbers using the Dutch locale:
System.out.println (NumberFormat.getInstance (new Locale ("nl")) .format (4032.3f); / / 4.032
Format numbers using the German locale:
System.out.println (NumberFormat.getInstance (Locale.GERMANY) .format (4032.3f)); / / 4.032
Format currency using the default locale
System.out.println (NumberFormat.getCurrencyInstance (). Format (40324.31f)); / / $40324.31
Format currency using locale
Format the currency using the Dutch locale:
System.out.println (NumberFormat.getCurrencyInstance (new Locale ("nl")) .format (40324.31f); / / 40.324031 so far, I believe you have a deeper understanding of "what are the hot advanced Java interview questions". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.