Some Java face questions in-depth analysis

Source: Internet
Author: User

The difference between & and &&?


Both & and && can be used as logical AND operator. Represents the logical and (and) when the result of an expression on either side of an operator is true. The result of the operation is true, otherwise the result is false if only one of the parties is false.

&& also has a short-circuit function, that is, assuming the first expression is false, the second expression is no longer evaluated, for example, for an if (str! = null &&!str.equals ("")) expression, When STR is NULL, subsequent expressions do not run, so the NullPointerException assumes that the && is changed to & The NullPointerException exception is thrown.

if (x==33& ++y>0) y grows, if (x==33 && ++y>0) does not grow

&does not haveShort-circuit nature, for if (str! = null &&!str.equals ("")) expression, when STR is null. The following expressionwill still runSothere will be NullPointerException,In addition,,& can also be used as bit operators.



Whether the switch statement can function on a byte, whether it can function on a long, whether it can function on a string?

在switch(expr1)中。expr1能够是 byte, char, short,int 以及它们的包装类 Byte ,Character,  Short, Integer,还有enum(枚举)、String(JavaSE 7添加的)

The Examiner Net Http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html


Third, when modifying a variable with Finalkeyword. Is the reference immutable, or does the referenced object not change?

When you use Finalkeyword to decorate a variable, the reference variable cannot be changed, and the contents of the object to which the reference variable is pointing can be changed. For instance, for example, the following statement:

Final StringBuffer a=new StringBuffer ("immutable");
Run for example the following statement will report a compile-time error:

A=new StringBuffer ("");
However, running such as the following statement is possible by compiling:

A.append ("broken!");

When someone defines a method's parameters, it may want to use the following form to prevent the internal changes in the method from passing in the Parameter object:

public void Method (final stringbuffer param)

{

}

In fact, this is not possible, within the method can still add such as the following code to modify the parameters of the object:

Param.append ("a");


four, when you run string str=new string ("abc"). , how many string objects did you create?

If we search the answer online, almost all of the answers are as follows:

If the string pool does not have ABC, it creates an ABC object in the string pools, and then creates an ABC object in the heap, returning the address of the ABC object in the heap to STR, which is the case of creating 2 objects, or simply creating an ABC object in the heap.


But. I saw a video of the commentary today. He says that no matter what the situation is, just create a string object. ABC objects are not created in the string pool, regardless of the circumstances. Arguments:

String s1= "HJK"; String S2=s1.intern ();        The function of intern () is to assume that there is a S1 string value "HJK" in the constant pool, and     //returns the String object. Otherwise, add "HJK" to the constant pool and return the string object. System.out.println (S2==S1);   TrueString s3=new String ("abc"); String S4=s3.intern (); System.out.println (S3==S4);  FalseString S5=s3.intern (); System.out.println (S5==S4);  True


Another reference: Java face question video commentary Http://study.163.com/course/courseMain.htm?courseId=519065#/courseMain

Some Java face questions in-depth analysis

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.