Java Development interview Problem small finishing (ii)

Source: Internet
Author: User
Tags date1 time and date

Anonymous Inner Class (anonymous inner Class) can inherit other classes? Is it possible to implement an interface?

A: You can inherit other classes or implement other interfaces, which are commonly used in swing programming and Android development to implement event snooping and callbacks.

The inner class can reference its members of the containing class (The outer Class)? Are there any restrictions?

A: An internal class object can access the members of the external class object that created it, including private members.

What are the uses of the final keyword in Java?

A: (1) Modifier class: Indicates that the class cannot be inherited;

(2) Modification method: Indicates that the method cannot be rewritten;

(3) Modifier variable: Indicates that the value cannot be modified (constant) after the variable can be assigned only once.

The operation results of the following program are indicated.

classA {Static{System.out.print ("1"); }     PublicA () {System.out.print ("2"); }}classBextendsa{Static{System.out.print (A); }     PublicB () {System.out.print ("B"); }} Public classHello { Public Static voidMain (string[] args) {A ab=NewB (); AB=NewB (); }}

A: Execution result: 1a2b2b. When you create an object, the constructor is called in the order that it initializes the static member, then calls the parent class constructor, initializes the non-static member, and finally calls itself the constructor.

Tip: If you can't give the correct answer to this question, the 21st question of Java class loading mechanism is not fully understood, and then look at it.

convert between data types:
-How do I convert a string to a base data type?
-How do I convert a base data type to a string?

For:
-Call the method Parsexxx (string) or valueof (string) in the wrapper class corresponding to the base data type to return the corresponding base type;
-One way to do this is to concatenate the base data type with an empty string ("") to get its corresponding string, or to call the ValueOf () method in the string class to return the corresponding string.

How do I implement a string reversal and substitution?

A: There are many ways to write your own implementation or you can use the methods in string or Stringbuffer/stringbuilder. One common interview topic is recursive implementation of string inversion, as shown in the following code:

    public static String reverse(String originStr) {        if(originStr == null || originStr.length() <= 1) return originStr; return reverse(originStr.substring(1)) + originStr.charAt(0); }

How do I convert a GB2312 encoded string to a iso-8859-1 encoded string?

A: The code looks like this:

String s1 = "你好";String s2 = new String(s1.getBytes("GB2312"), "ISO-8859-1");

Date and time:
-How do I get the date, hour and minute seconds?
-How to get the number of milliseconds from January 1, 1970 0:0 0 seconds to now?
-How to get the last day of a month?
-How do I format a date?

For:
Issue 1: Create a Java.util.Calendar instance, call its get () method to pass in a different parameter to get the value corresponding to the parameter. Java 8 can be obtained using Java.time.LocalDateTimel, as shown in the code below.

 Public classDatetimetest { Public Static voidMain (string[] args) {Calendar cal=calendar.getinstance ();        System.out.println (Cal.get (calendar.year));    System.out.println (Cal.get (calendar.month)); //0-11System.out.println (Cal.get (calendar.date));        System.out.println (Cal.get (calendar.hour_of_day));        System.out.println (Cal.get (Calendar.minute));        System.out.println (Cal.get (Calendar.second)); //Java 8LocalDateTime dt =Localdatetime.now ();        System.out.println (Dt.getyear ());     System.out.println (Dt.getmonthvalue ()); //1-12System.out.println (Dt.getdayofmonth ());        System.out.println (Dt.gethour ());        System.out.println (Dt.getminute ());    System.out.println (Dt.getsecond ()); }}

Issue 2: The following methods are available for this number of milliseconds.

// Java 8

Issue 3: The code is shown below.

Calendar time = calendar.getinstance (); Time.getactualmaximum (calendar.day_of_month);

Issue 4: Use the format (date) method in Java.text.DataFormat subclasses (such as the SimpleDateFormat Class) to reformat dates. In Java 8, you can use Java.time.format.DateTimeFormatter to format the time date, as shown in the code below.

ImportJava.text.SimpleDateFormat;Importjava.time.LocalDate;ImportJava.time.format.DateTimeFormatter;Importjava.util.Date;classDateformattest { Public Static voidMain (string[] args) {SimpleDateFormat oldformatter=NewSimpleDateFormat ("Yyyy/mm/dd"); Date Date1=NewDate ();        System.out.println (Oldformatter.format (date1)); //Java 8DateTimeFormatter Newformatter = Datetimeformatter.ofpattern ("Yyyy/mm/dd"); Localdate Date2=Localdate.now ();    System.out.println (Date2.format (newformatter)); }}

Add: Java time and date API has always been criticized for something, in order to solve this problem, Java 8 introduced a new time and date API, including Localdate, LocalTime, LocalDateTime, Clock, instant and other classes, The design of these classes uses the invariant pattern and is therefore a thread-safe design. If you don't understand these things, you can refer to my other article, "summarizing and thinking about Java concurrency programming."

A. print the current moment of yesterday.

For:

Import Java.util.Calendar; class yesterdaycurrent {    publicstaticvoid  main (string[] args) {        = calendar.getinstance ();         -1);        System.out.println (Cal.gettime ());}    }

In Java 8, you can implement the same functionality with the following code.

Import Java.time.LocalDateTime; class yesterdaycurrent {    publicstaticvoid  main (string[] args) {        = Localdatetime.now ();         = Today.minusdays (1);        System.out.println (yesterday);    }}

compare Java and javasciprt.

A: JavaScript and Java are the two different products developed by the two companies. Java is the object-oriented programming language of the original Sun Microsystems company, especially suitable for Internet application development, while JavaScript is the product of Netscape company, An object-based and event-driven, interpreted language that can be embedded in a Web page, developed to extend the functionality of the Netscape browser. The predecessor of JavaScript was LiveScript, while Java was formerly the Oak language.
The following comparisons are made between the two languages:
-Object-based and object-oriented: Java is a true object-oriented language, and even if you are developing a simple program, you must design the object; JavaScript is a scripting language that can be used to create complex software that is independent of the network and interacts with the user. It is an object-based (object-based) and event-driven (Event-driven) programming language, so it itself provides a very rich internal object for designers to use.
-Interpretation and Compilation: Java's source code must be compiled before it is executed. JavaScript is an explanatory programming language whose source code does not need to be compiled and interpreted by the browser. (Almost all current browsers use JIT (Instant compilation) technology to improve the efficiency of JavaScript)
-Strongly typed variables and weakly typed variables: Java uses strongly typed variables to check that all variables must be declared before they are compiled, that the variables in JavaScript are weakly typed, that they are not even declared before they are used, and that the JavaScript interpreter checks to infer its data type at run time.
-code format is not the same.

add: The four points listed above are the so-called standard answers circulated online. In fact, the most important difference between Java and JavaScript is that one is a static language and one is a dynamic language. The development trend of current programming language is functional language and dynamic language. In Java classes are class-one citizens, and JavaScript functions are a one-off citizen, so JavaScript supports functional programming, you can use lambda functions and closures (closure), and of course Java 8 also began to support functional programming, providing support for lambda expressions and functional interfaces. For such questions, in the interview is best to use their own language to answer will be more reliable, do not recite the online so-called standard answer.

Java Development interview Problem small finishing (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.