Http://saebbs.com/forum.php?mod=viewthread&tid=37567&page=1&extra=
This is the first part of a series of advanced Java interview questions. This section discusses Java core issues such as mutable parameters, assertions, garbage collection, initializers, tokens, dates, calendars, and so on.
Programmer Interview Guide:Https://www.youtube.com/watch?v=0xcgzUdTO5M
Java Interview Questions Collection guide:Https://www.youtube.com/watch?v=GnR4hCvEIJQ
What is a mutable parameter?
The purpose of the assertion?
When do I use assertions?
What is garbage collection?
Use an example to explain garbage collection?
When do I run a garbage collection?
Best Practices for garbage collection?
What is a block of initialization data?
What is a static initializer?
What is an instance initialization block?
What is a regular expression?
What is token?
Give an example of a token?
How do I use the Scanner class (Scanner Class) token?
How do I add an hour (hour) to a Date object (date Objects)?
How do I format a Date object?
What is the purpose of the Calendar class in Java?
How do I get an instance of a calendar class in Java?
Explain some of the important methods in calendar classes?
What is the purpose of the numeric formatting class (number format Class)?
What is a mutable parameter?
Variable parameters allow methods to invoke different numbers of parameters. Take a look at the summation method in the example below. This method can call 1 int parameters, 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 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
}
The purpose of the assertion?
The assertion was introduced in Java 1.4. It allows you to verify assumptions. If the assertion fails (that is, returns false), 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 I use assertions?
assertions should not be used to validate input data to a Public method or command-line argument. IllegalArgumentException will be a better choice. in the public method, only assertions are used to check that they should not occur at all.
What is garbage collection?
Garbage collection is another term for automatic memory management in Java. The purpose of garbage collection is to keep as many available heaps as possible for the program. The JVM deletes objects on the heap that are no longer required to be referenced from the heap.
Use an example to explain garbage collection?
For example, the following method is called from a function.
void Method () {
Calendar calendar = new GregorianCalendar (2000,10,30);
SYSTEM.OUT.PRINTLN (Calendar);
}
An object of the GregorianCalendar class is created on the heap through the reference variable in the first line of the function calendar.
When the function finishes executing, the reference variable calendar is no longer valid. Therefore, references to objects are not created in the method.
The JVM recognizes this and removes objects from the heap. This is called garbage collection.
When do I run a garbage collection?
Garbage collection runs at the whim and whim of the JVM (not so bad). The possible scenarios for running garbage collection are:
Heap is low on available memory
CPU Idle
Best Practices for garbage collection?
In a programmatic way, we can ask (remember that this is just a request-not a command) the JVM runs garbage collection by calling the System.GC () method.
The JVM may throw outofmemoryexception when the memory is full and no objects on the heap are available for garbage collection.
An object runs the Finalize () method before it is removed from the heap by garbage collection. We recommend that you do not write any code with the Finalize () method.
What is a block of initialization data?
Initialize the data block-code that runs when the object is created or when the class is loaded.
There are two types of initialization data blocks:
Static initializers: code to run when the class is loaded
instance initializer: code to run when creating a new object
What is a static initializer?
See the following example: static{ The code between and } is called a static initializer. It runs only when the class is loaded for the first time. Only static variables can be accessed in the static initializer. Although three instances were created, the static initializer runs only once.
Public class Initializerexamples {
static int count;
int i;
static{
//this is a static initializers. Run only if Class is first loaded.
//only static variables can be accessed
& nbsp System.out.println ("Static Initializer");
//i = 6;//compiler ERROR
System.out.println ("Count when Static Initializer are 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 are run is 0.
What is an instance initialization block?
Let's take a look at an example: every time you create an instance of a class, the code in the instance initializer runs.
public class Initializerexamples {
static int count;
int i;
{
This was 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 are 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 are run is 1
Instance Initializer
Count when Instance Initializer are run is 2
Instance Initializer
Count when Instance Initializer are run is 3
What is a regular expression?
Regular expressions make it easy to parse, scan, and split strings. Regular expressions commonly used in Java--patter,matcher and scanner classes.
What is token?
token refers to dividing a string into substrings based on a delimiter. For example, delimiter; split string ac;bd;def;e to four substrings ac,BD,def and e .
The delimiter itself can also be a common regular expression.
The String.Split (regex) function takes a regex as a parameter.
Give an example of a token?
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 use the Scanner class (Scanner Class) token?
private static void Tokenizeusingscanner (String string,string regex) {
Scanner Scanner = new Scanner (string);
Scanner.usedelimiter (regex);
list<string> matches = new arraylist<string> ();
while (Scanner.hasnext ()) {
Matches.add (Scanner.next ());
}
System.out.println (matches);
}
Tokenizeusingscanner ("Ac;bd;def;e", ";"); /[AC, BD, Def, E]
How do I add an hour (hour) to a Date object (date Objects)?
Now, let's look at adding hours to a Date object. All date operations on date need to be completed by adding milliseconds to date. For example, if we want to add 6 hours, then we need to convert 6 hours into milliseconds. 6 hours = 6 * 60 * 60 * 1000 Ms. Take a look at the example below.
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?
The formatted 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));//10/16/12 5:18 AM
The formatted date with the locale is as follows:
System.out.println (Dateformat.getdateinstance (
Dateformat.full, New Locale ("It", "it"))
. Format (date));//marted "Ottobre 2012
System.out.println (Dateformat.getdateinstance (
Dateformat.full, Locale.italian)
. Format (date));//marted "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 the Calendar class in Java?
Calendar class (YouTube video link- https://www. youtube.com/ WATCH?V=HVNL ybt1ve0 ) is used in Java for processing dates. The Calendar class provides an easy way 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 in Java?
The Calendar class cannot be created by using the new calendar. The best way to get an instance of the Calendar class is to use the getinstance () static method in the calendar.
Calendar calendar = new Calendar (); COMPILER ERROR
Calendar calendar = Calendar.getinstance ();
Explain some of the important methods in the Calendar class?
It is not difficult to set the day, month, or year on the calendar object. Call the appropriate constant set method 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 for a specific date-September 24, 2010. We can use the calendar get method. The parameters that have been passed represent the values we want to get from the calendar--day or month or year or ... Examples of values you can get from the calendar are:
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));//267
System.out.println (Calendar.getfirstdayofweek ());//1-Calendar.sunday
What is the purpose of the numeric formatting class (number format Class)?
Number formats are used to format numbers into different regions and in different formats.
Number format using the default locale
System.out.println (Numberformat.getinstance (). Format (321.24f));//321.24
Use number formats for locales
Format numbers using the Dutch locale:
System.out.println (Numberformat.getinstance ("NL")). Format (4032.3f));//4.032,3
Format numbers using the German locale:
System.out.println (Numberformat.getinstance (locale.germany). Format (4032.3f));//4.032,3
Formatting currencies using the default locale
System.out.println (Numberformat.getcurrencyinstance (). Format (40324.31f));//$40,324.31
Format currency using locale settings
Format currency using the Dutch locale:
System.out.println (Numberformat.getcurrencyinstance ("NL")). Format (40324.31f));//? 40.324,31
License
This article, as well as any related source code and files, is based on the code Project Open License (Cpol).
Translation Links: http://www.codeceo.com/article/20-java-advanced-interview-questions.html
English Original: Advanced Java Interview Questions
translation Code Agricultural Network -Xiao Feng
[Turn] 20 advanced Java Face questions summary