"JAVA" Basics _ FAQ 1

Source: Internet
Author: User
Tags bitwise wrapper

1. Whether a ". Java" source file can contain more than one class

Of course, there can be more than one class, but a class that is decorated with public can only have one, and the name of the class must match the name of the file.

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

Many people understand this problem is not very thorough.

First:& and && are both logical and operators, and when the expression on either side of the operator is true, the entire operation evaluates to true, and vice versa to false. But & and && are also slightly different,&& for short-circuiting and, what do you mean, for example: if (null! = str && "". Equals. STR), when STR is NULL, the second condition is not executed. If & is used, the second condition is still executed.

The second:& can also represent the bitwise operator, when the expression on both sides of & is not a Boolean,,& represents bitwise AND operation, 0x111 & 0x01 results in 0x01, and in this way to determine the parity of a number is not good ah, of course, this is just an example , there are other uses, according to the actual situation.

3, whether the switch statement can function on a byte, can function on a long, can function on a string

In switch (EXPR1), EXPR1 can only be an integer expression or enumeration constant, an integer expression can be an int primitive type or an integer wrapper type, since Byte, short, char can be implicitly convertible to int, So these types, as well as these types of wrapper types, are also available. It is obvious that long, string types do not conform to the syntax of switch, and cannot be implicitly converted to an int type and therefore cannot be used in a switch statement.

4, the char type variable can be stored in a Chinese Han

Char type variables are used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so char type variables can of course be used to store Chinese characters. However, if a special Chinese character is not included in the Unicode encoding character set, then this char cannot save the special kanji.

Note: The Unicode encoding occupies two bytes, so the char type variable also occupies two bytes.

5. When you use the final keyword to decorate a variable, the reference cannot be changed or the referenced object cannot be changed.

When you use the final keyword to decorate a variable, it means that the reference variable cannot be changed, and that the reference variable points to the contents of the object or can be changed.

6. What is the difference between "= =" and Equals method?

The = = operator is the equality of values for two variables, that is, whether the values in the corresponding memory of the comparison variable are equal. Compare the base data type or two reference variables for equality, and you should use the = = operator.

If the data that a variable points to is an object type, then it involves two memory, the object itself occupies a piece of memory (heap memory), the variable also occupies a piece of memory, the first address of the heap memory. For two reference-type variables, the = = comparison Indicates whether two variables point to the same heap memory, compared to whether the addresses stored by the two variables are equal.

The Equals method is used to compare the contents of two true independent objects, as follows:

String a = new string ("abc");

String b = new String ("abc");

To compare the strings in A and B, use: A.equals (b) Returns the result to true;

At this point, if you compare with A==b, the returned result will be false, because A and B are two different objects, the address is completely different.

7. What is the difference between a static variable and an instance variable?

Syntax definition differences: Static changes are preceded by the static keyword, which does not precede the instance variable.

Program Run differences: the instance variable is essentially a property of an object, an instance object that must be created, where the instance variable is allocated space before the instance variable can be used. Static variables do not belong to a specific instance object, but belong to a class, so also called class variables, as long as the program loads the class's bytecode, without creating any instance object, static variables will be allocated space, static variables can be used.

In summary, instance variables must be created before they can be used by this object, while static variables can be referenced directly using the class name.

8. Whether a call to a non-static method can be emitted inside a static method

No.

A non-static method is associated with an object, and a concrete object must be created before a method call can be made on the object, and the static method call does not need to create the object, which can be called directly. When a static method is issued inside a non-static method call, which object is the non-static method associated with? I don't know, theoretically I can't get through. Therefore, inside a static method, a call to a non-static method cannot be emitted.

9, the difference between integer and int

int is one of the 8-clock basic data types provided by Java. Java provides a wrapper class for each base data type, integer is the wrapper class provided by Java for Int, and the default value for int is null for 0,integer.

10, talk about public, private, protected scope and do not write the difference

The specific scope is shown in the following table. If you do not add a modifier, it represents friendly.

Scope

Current class

Same package

Descendant class

Other Packages

Public

Ok

Ok

Ok

Ok

Protected

Ok

Ok

Ok

NO

Friendly

Ok

Ok

NO

NO

Private

Ok

NO

NO

NO

"JAVA" Basics _ FAQ 1

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.