1,Math. What is round (11.5) equal to? Math. How much is round (- 11.5)?
public class Test { public static void main(String[] args) { System.out.println("Math.round(11.5)="+Math.round(11.5)); System.out.println("Math.round(-11.5)="+Math.round(-11.5)); } }
Math. The return value of round (11.5) is 12, math The return value of round (- 11.5) is - 11. The principle of rounding is to add 0.5 to the parameter and then round it.
2. Can switch work on byte, long and String?
In switch(expression) before Java 5,
expression can only be byte, short, char and int. strictly speaking, Java 5 used to only support int. the reason why byte short char can be used is because there is automatic type conversion.
Starting from Java 5, enumeration type is introduced into Java, and expression can also be enum type.
Starting from Java 7, expression can also be a String, but long is not allowed in all current versions.
3. Does the array have a length() method? Does String have a length() method?
The array does not have a length() method, but has a length attribute.
public class Test { public static void main(String[] args) { int a[] = {12,3,45,6,7,8}; System.out.println(a.length); } }
String has a length() method. In JavaScript, the length of the string is obtained through the length attribute, which is easy to be confused with Java.
4. What is the difference between String, StringBuilder and StringBuffer?
The Java platform provides two types of strings: String and StringBuffer/StringBuilder, which can store and manipulate strings. The differences are as follows:
● String is a read-only String, which means that the String content referenced by String cannot be changed. Beginners may have this misunderstanding:
String str = "abc"; str = "bcd";
As above, the string str can be changed! In fact, str is just a reference object, which points to a string object "abc".
The meaning of the second line of code is to make str point to a new string "bcd" object again, and the "abc" object has not changed, but the object "abc" has no reference to it.
● string objects represented by StringBuffer/StringBuilder can be modified directly.
● StringBuilder is introduced in Java 5. Its method is exactly the same as that of StringBuffer. The difference is that it is used in a single threaded environment. Because all its methods are not synchronized, its efficiency is theoretically higher than that of StringBuffer.
5. Please say the output of the following program?
class Test { public static void main(String[] args) { String s1 = "Programming"; String s2 = new String("Programming"); String s3 = "Program"; String s4 = "ming"; String s5 = "Program" + "ming"; String s6 = s3 + s4; System.out.println(s1 == s2); //false System.out.println(s1 == s5); //true System.out.println(s1 == s6); //false System.out.println(s1 == s6.intern()); //true System.out.println(s2 == s2.intern()); //false } }
Supplementary: to answer the above interview questions, you need to know the following two knowledge points:
● the intern() method of the String object will get the reference of the corresponding version of the String object in the constant pool (if there is a String in the constant pool and the equals result of the String object is true), if there is no corresponding String in the constant pool, the String will be added to the constant pool, and then the reference of the String in the constant pool will be returned;
● the essence of String + operation is to create a StringBuilder object for append operation, and then process the spliced StringBuilder object into a String object with toString method. This can be done with javap - C stringequaltest The class Command obtains the JVM bytecode instruction corresponding to the class file.
6. How to get the date, hour, minute and second?
import java.time.LocalDateTime; import java.util.Calendar; class DateTimeTest { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println(cal.get(Calendar.YEAR));//year System.out.println(cal.get(Calendar.MONTH)); //Month 0 - 11 System.out.println(cal.get(Calendar.DATE));//day System.out.println(cal.get(Calendar.HOUR_OF_DAY));//hour System.out.println(cal.get(Calendar.MINUTE));//minute System.out.println(cal.get(Calendar.SECOND));//second // Java 8 LocalDateTime dt = LocalDateTime.now(); System.out.println(dt.getYear());//year System.out.println(dt.getMonthValue()); //January - December System.out.println(dt.getDayOfMonth());//day System.out.println(dt.getHour());//hour System.out.println(dt.getMinute());//minute System.out.println(dt.getSecond());//second } }
7. How to get the number of milliseconds from 0:0:0 on January 1, 1970 to the present?
class GetTime { public static void main(String[] args) { System.out.println("First:" + Calendar.getInstance().getTimeInMillis()); System.out.println("Second:" + System.currentTimeMillis()); System.out.println("Third:" + Clock.systemDefaultZone().millis()); } }
8. How to get the last day of a month?
class GetLastDay { public static void main(String[] args) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //Get the first day of the current month: Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH, 1);//Set to No. 1, and the current date is the first day of the month String first = format.format(c.getTime()); System.out.println("first:" + first); //Gets the last day of the current month Calendar ca = Calendar.getInstance(); ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); String last = format.format(ca.getTime()); System.out.println("last:" + last); //Java 8 LocalDate today = LocalDate.now(); //The first day of the month LocalDate firstday = LocalDate.of(today.getYear(), today.getMonth(), 1); //The last day of the month LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth()); System.out.println("The first day of the month" + firstday); System.out.println("The last day of the month" + lastDay); } }
Operation results:
9. How do I format dates?
java. text. The format(Date) method in a subclass of dataformat, such as the SimpleDateFormat class, formats the date. Java. Net can be used in Java 8 time. format. Datetimeformatter to format the time and date. The code is as follows:
import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Date; class DateFormatTest { public static void main(String[] args) { SimpleDateFormat oldFormatter = new SimpleDateFormat("yyyy/MM/dd"); Date date1 = new Date(); System.out.println(oldFormatter.format(date1)); // Java 8 DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); LocalDate date2 = LocalDate.now(); System.out.println(date2.format(newFormatter)); } }
Operation results:
Add: Java's time and date API has always been criticized. In order to solve this problem,
A new time and date API has been introduced in Java 8,
Including LocalDate, LocalTime, LocalDateTime, Clock, Instant and other classes,
These classes are designed using invariant patterns, so they are thread safe.
10. Print the current time of yesterday?
import java.util.Calendar; class YesterdayCurrent { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); System.out.println(cal.getTime()); } }
//java-8 import java.time.LocalDateTime; class YesterdayCurrent { public static void main(String[] args) { LocalDateTime today = LocalDateTime.now(); LocalDateTime yesterday = today.minusDays(1); System.out.println(yesterday); } }
11. What is the difference between JSR310 specification and joda time?
In fact, Stephen coleborne, the specification leader of JSR310, is also the creator of joda time. JSR310 is established on the basis of joda time and refers to most API s, but it does not mean that JSR310 = joda time. The following obvious differences are:
● the most obvious change is the package name (from org.joda.time to java.time)
● JSR310 does not accept NULL value, and joda time regards NULL value as 0
● the difference between computer related time (Instant) and human related time (DateTime) of JSR310 becomes more obvious
● all exceptions thrown by JSR310 are subclasses of DateTimeException. Although DateTimeException is a RuntimeException.