(i) Java interview easy to forget topic selection (ON)

Source: Internet
Author: User

1. What is the difference between int and integer?

A: Java is a nearly pure object-oriented programming language, but for the convenience of programming or the introduction of basic data types, but in order to be able to use these basic data types as Object operations, Java for each basic data type has introduced the corresponding wrapper type (wrapper class), The wrapper class for int is integer, and the automatic boxing/unpacking mechanism is introduced from Java 5, allowing the two to be converted to each other.

Java provides the wrapper type for each primitive type:
-Original type: boolean,char,byte,short,int,long,float,double
-Package Type: boolean,character,byte,short,integer,long,float,double

class autounboxingtest {    publicstaticvoid  main (string[] args) {         New Integer (3);         = 3;                  // automatically boxing 3 into an integer type        int c = 3;         = = b);     // false Two references do not reference the same object        System.out.println (A = = c);     // true a automatically unboxing into int type and C comparison     }}

Recently encountered a face test, but also with automatic packing and unpacking a bit of a relationship

 Public class Test03 {    publicstaticvoid  main (string[] args) {        = F2 = 100, F3 = F4 =.         = = F2)        ; = = f4);}    }

The first thing to note is that F1, F2, F3, and F4 Four variables are all integer object references, so the following = = Operation compares values instead of references. What is the nature of boxing? when we assign an int value to an integer object, the static method of the integer class is called valueof, and if you look at the source code of the valueof, you know what happened.

    Public Static Integer valueOf (int  i) {        if (i >= integercache.low && i <= integercache . High)            return integercache.cache[i + (-integercache.low)];         return New Integer (i);    }

To put it simply, if the value of an integer literal is between 128 and 127, then the integer object in the constant pool is not new, and the result of F1==F2 in the above interview is true, and the result of F3==F4 is false.

2. What is the difference between & and &&?

The:& operator is used in two ways: (1) bitwise and; (2) Logical AND. The && operator is short-circuiting and arithmetic. The difference between the logic and the short-circuit is very large, although both require that the Boolean value on both sides of the operator is true the value of the entire expression is true. && is called a short-circuit operation because if the value of the expression on the left side of && is false, the expression on the right will be shorted out directly and will not be evaluated. Most of the time we may need to use && instead of &, for example, to verify that the user's name is not NULL and is not an empty string when validating the login, it should be written as: Username! = null &&!username.equals ("") , the order of the two cannot be exchanged, not the & operator, because if the first condition is not true, the equals of the string cannot be compared, otherwise a nullpointerexception exception will be generated. Note: Logical OR operator (|) and short-circuit or operator (| | The difference is also true.

3. Explain the usage of the in-memory stack (stack), heap, and method area.

A: Usually we define a variable of a basic data type, a reference to an object, and a field save for a function call that uses the stack space in the JVM, while objects created by the new keyword and the constructor are placed in the heap space, and the heap is the main area that the garbage collector manages. Because the garbage collector now uses the Generational collection algorithm, the heap space can also be subdivided into the Cenozoic and Laosheng generation, then the specific point can be divided into Eden, Survivor (also can be divided into from Survivor and to Survivor), tenured Both the method area and the heap are areas of memory shared by each thread to store data such as class information that has been loaded by the JVM, constants, static variables, JIT compiler compiled code, and so on, in which the literal (literal) in the program, such as 100, "Hello" and constants, are placed in a constant pool. Chang is part of the method area. Stack space operation is the fastest but the stack is very small, usually a large number of objects are placed in the heap space, stack and heap size can be adjusted by the JVM's startup parameters, the stack space is exhausted will cause stackoverflowerror, The heap and constant pool space are not sufficient to cause outofmemoryerror.

String str = new String("hello");

In the above statement, the variable str is placed on the stack, the string object created with new is placed on the heap, and the literal "hello" is placed in the method area.

4. How much does math.round (11.5) equal? How much does Math.Round (-11.5) equal?

Answer: The return value of Math.Round (11.5) is the return value of 12,math.round (-11.5) is-11. Rounding is done by adding 0.5 to the parameter and then taking the next rounding.

 5. Does switch work on a byte, can it function on a long, or does it work on a string?

A: Before Java 5, in switch (expr), expr can only be a byte, short, char, Int. Starting with Java 5, the enumeration type is introduced in Java, and expr can be an enum type, starting with Java 7, expr can also be a string (string), but long is not available in all current versions.

Http://blog.csdn.net/jackfrued/article/details/44921941/no.12 to be continue

(i) Java interview easy to forget topic selection (ON)

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.