Java face question

Source: Internet
Author: User
Tags finally block

1. The difference between overloading (overload) and overriding (override). Can overloaded methods be differentiated according to the return type?

overloading occurs in a class with a method of the same name if there are different parameter lists (different parameter types, different number of arguments, or both)

overrides occur between subclasses and parent classes , and overrides require subclasses to be overridden by methods that have the same return type as the parent class being overridden by the method, which is better accessed than the parent class is overridden by the method, and cannot be declared by more exceptions than the parent class is overridden (the Richter substitution principle). Overloads do not have special requirements for return types.

2,the difference between string and StringBuilder, StringBuffer?

String is a read-only string, which means string content referenced by string cannot be changed

The string object represented by the Stringbuffer/stringbuilder class can be modified directly

StringBuilder is introduced in Java 5, and it is exactly the same as the StringBuffer method, except that it is used in a single-threaded environment because all aspects of it are not synchronized decorated, so its efficiency is higher than StringBuffer.

3. does the number of I + 1 < I exist ()

Answer: Presence

Parse: If I is an int type, then when I is int can represent the largest integer, i+1 overflow becomes negative, at this time not <i it.

Extension: Existence makes I > J | | I <= J not established number ()

Answer: Presence

Parse: such as Double.NaN or Float.nan

4, recently encountered a face test, but also with automatic packing and unpacking a bit of a relationship, the code is as follows:

[HTML]View PlainCopy 
  1. public class Test03 {
  2. public static void Main (string[] args) {
  3. Integer f1 = F2 = 150, f3 = f4
  4. System.out.println (f1 = = F2); True
  5. System.out.println (F3 = = F4); False
  6. }
  7. }

Simply put, if the value of the integer literal is between 128 and 127 , then the integer object in the constant pool is not new, but the f1==f2 result in the above interview is true, and f3== The result of F4 is false.

5,the difference between & and &&?

The & operator is used in two ways: (1) bitwise and; (2) logical and both require that the Boolean value on both sides of the operator is true the value of the entire expression is true

& Left if not set, will also verify the right;&& left is not established on the exit (short-circuit operation)

6, explain the final, finally, finalize the difference.
For:
-final: Modifier (keyword) has three usages: if a class is declared final, it means that it can no longer derive a new subclass, i.e. it cannot be inherited, so it and abstract are antonyms. Declaring variables as final ensures that they are not changed in use, and that the variable declared as final must be given the initial value at the time of declaration, whereas in subsequent references only the non-modifiable can be read. A method that is declared final can also be used only and cannot be overridden in a subclass.
-finally: usually put in try...catch ... The subsequent construct always executes the code block, which means that the program executes either normally or unexpectedly, the code here can be executed as long as the JVM is not closed, and the code that frees the external resource is written in the finally block.
-the method defined in the Finalize:object class allows the use of the Finalize () method in Java to do the necessary cleanup before the garbage collector clears the object from memory. This method is called by the garbage collector when the object is destroyed, and by overriding the Finalize () method, you can defragment the system resources or perform other cleanup work.

7. What are the data types supported by Java? What is auto -unboxing?

The 8 basic data types supported in the Java language are:

    • Byte
    • Short
    • Int
    • Long
    • Float
    • Double
    • Boolean
    • Char

Auto-Boxing is a conversion of the Java compiler between the base data type and the corresponding object wrapper type . For example: Convert int to integer,double into double, and so on. The reverse is automatic unpacking.

8. What is the difference between a process and a thread?

A process is an application that executes, and a thread is an execution sequence within a process. A process can have multiple threads. Threads are also called lightweight processes.

9. How do I ensure that n threads can access n resources without causing a deadlock?

When using multithreading, a very simple way to avoid deadlocks is to specify the order in which locks are acquired and force threads to acquire locks in the order specified. Therefore, if all the threads lock and release the lock in the same order, there will be no deadlock.

such as resource ABCD, that all threads must acquire a resource to apply for B resources, and so on

10. What is the result of the following program (s)?

Class Helloa {public    Helloa () {        System.out.println ("Helloa");    }        {System.out.println ("I ' M A class");}        static {System.out.println ("static A");}} public class Hellob extends Helloa {public    Hellob () {        System.out.println ("Hellob");    }        {System.out.println ("I ' M B class");}        static {System.out.println ("static B");}        public static void Main (string[] args) {new Hellob (); }}

Answer:

Static Astatic BI ' m A classhelloai ' m B classhellob

Parsing: To be honest I think this is a good question, to examine the static statement blocks, construct the block of statements (that is, the one with the curly braces), and the execution order of the constructors.

Object Initialization Order: (1) After the class is loaded, the static modified statement is executed from top to bottom (from parent to subclass), (2) when the static statement is executed, then the main method is executed, and (3) If the statement new has its own object, the construction code block is executed from top to bottom, Constructors (both of which can be said to be bound together).

Java face question

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.