Java Interview--java Basic section (i)

Source: Internet
Author: User
Tags bitwise

One ". Java" source files can include multiple classes (not internal classes)? What are the restrictions?

There can be multiple classes, but only one public class , and the class name of public must match the file.

Second, does Java have goto?

reserved words in Java are not currently used in Java.

What is the difference between & and &&?

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

&& also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated, for example, for if (Str!=null &&!str.equals ("")) expression, when STR is NULL, subsequent expressions do not execute, so the nullpointerexception will not appear if you change && to & a NullPointerException exception is thrown.

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

& can also be used as a bitwise operator , when an expression on either side of the & operator is not a Boolean,,& represents bitwise AND operation, and we typically use 0x0f to perform a & operation with an integer. To get the lowest 4 bit bits of the integer, for example, the result of 0x31&0x0f is 0x01.

Note: This topic first said the similarities between the two, and the special points of && and &, and cited some classic examples to show that they understand thoroughly deep, practical experience.

Iv. How do I jump out of the current multiple nested loops in Java?

in Java, to jump out of multiple loops, you can define a label in front of the outer loop statement, and then use a labeled break statement in the code of the inner loop body to jump out of the outer loop. For example:

        ok:         for (int i=0;i<10;i++) {             for (int j=0;j<10;j++) {                 system.out.println ("i="  + i +  ", j="  +  j);                 if (j==5) {                      break ok;                 }            }         } 

     , for example, to find a number in a two-dimensional array.    

        int arr[][]={{1,2,3},{4,5,6,7},{9}};         boolean found = false;         for (int i=0;i<arr.length&&!found;i++) {             for (int j=0;j<arr[i].length;j++) {                 system.out.println ("i="  + i +  ", j=" &NBSP;+&NBSP;J);                 if (arr[i][j] == 5) {                     found = true;                     break;                 }             }        }

If the switch statement can function on a byte, can it function on a long 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 package type, since Byte,short,char can be implicitly converted to int, These types, as well as these types of wrapper types, are available. Obviously, long and string types do not conform to the syntax of switch and cannot be implicitly converted to type int , so they cannot be used in a switch statement.

After jdk1.7: EXPR1 supports the string type, but long is still not supported.

Six, short s1=1;s1=s1+1; what's wrong? Short s1=1;s1+=1; what's wrong?

for short s1=1;s1=s1+1, because the type of the expression is automatically promoted by the s1+1 operation, the result is of type int, and when assigned to the short type S1, the compiler will report an error that requires a cast type .

for short s1=1;s1+=1; since + = is a Java-language-defined operator , the Java compiler will treat it specially, so it can compile correctly.

Can I store a Chinese character in char type? Why?

char-type variables are used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so the char type can of course store Chinese characters. However, if a particular Chinese character is not included in the Unicode encoding character set, then this char variable cannot store this particular character. Supplemental Note: Unicode encoding consumes two bytes, so variables of type char are also two bytes. Note: Although the latter part is not a positive answer, however, in order to show their knowledge and performance of their own understanding of the depth of the problem, can answer some relevant knowledge, do sparing, liker.

Eight, the most effective method to calculate 2 times 8 is equal to a few?

2<<3,

Because moving a number to the left N-bit, is the equivalent of multiplying by 2 of the N -square, then, a number multiplied by 8 only to move it to the left 3 bits, and bit computing CPU directly support , the most efficient, so, The most efficient way to multiply 2 times 8 equals several is 2<<3.

Nine, please design a 10 billion calculator?

     First of all to understand the question of the test point is what, first of all, we have to clear the bottom of the computer, to know the addition and subtraction of and know the computer's arithmetic operations will occur out of bounds , and the other is to have a certain face object design ideas. First of all, the computer with a fixed number of bytes to store numeric values, so the computer can be expressed in a certain range of values, in order to facilitate interpretation and understanding, we first to byte type As an example, it uses 1 bytes for storage, indicating the maximum range of values is 128 to +127. -1 The corresponding binary data in memory is 11111111, if two-1 is added, regardless of the Java operation when the type promotion, the operation will produce a carry, the binary result is 1,11111110,, that is, the final result is 11111110, that is, 2, which is exactly . 128 the corresponding binary data in memory is 10000000, if two-128 add, do not consider the Java operation when the type promotion, the operation will produce a carry, the binary result is 1, 00000000, because the carry after more than the byte type of storage space, so the carry part is discarded, The final result is 00000000, which is 0, which is obviously not what we expected, indicating

int a = Integer.max_value;
int b = Integer.max_value;
int sum = a+b;

Regardless of the long type, since int has a positive range of 2 to 31 square, indicating that the largest value is approximately equal to 2*1000*1000, that is, the size of 2 billion, so to implement a 10 billion calculator, we have to design a class can represent a large number of integers, and provides the function of subtraction with another integer, the approximate function is as follows:

(1) There are two member variables within this class, one representing the symbol and the other using a byte array to represent the binary number of the value

(2) There is a construction method that converts a string containing a multi-digit value into an internal symbol and byte array

        public class BigInteger{             int sign;             byte[] val;             public biginteger (String val) {                 sign = ;                 val = ;             }            public  Biginteger add (Biginteger other) {                             }             public biginteger substract (Biginteger other) {                          }            public  Biginteger multiply (Biginteger other) {                         }             public biginteger divide (BigInteger other) {                          }        }

Note: To write this class of complete code, is very complex, if interested, you can see the JDK in the Java.math.BigInteger class source. The interviewer also knows no one can write this kind of complete code in a short time, he wants to be you have this aspect of concept and consciousness, his most important is to examine your ability, so, you do not because you cannot write the complete final result to give up this problem, you have to do is you write more than others, Prove that you are stronger than others, you have this aspect of the ideological consciousness can be, after all, others may even the meaning of the topic do not understand, what has not written, you have to dare to answer this question, even if only answer part, that also with those who do not understand the difference out of the person, opened the distance, is the dwarf in the tall, In addition, the framework code of the answer is also very important, reflects some of the design of the face object, especially the method name is very professional, with the English word is very accurate, this is the ability, experience, professionalism, English level and other aspects of the embodiment, will give a good impression, in the programming ability and other aspects of the condition of the same situation , English is good except that you can get more opportunities, salary can be 1000 yuan higher.

When you use the final keyword to decorate a variable, is the reference immutable or does the referenced object not change?

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

Final StringBuffer a = new StringBuffer ("immutable");

Executing the following statement will report a compiler error:

A = new StringBuffer ("");

However, the following statements can be executed by compiling:

A.append ("broken!");

When someone defines a method's parameters, you might want to block the method from modifying the passed-in Parameter object in the following form:

public void method (final stringbuffer param) {

}

In fact, this cannot be done, and the following code can still be added inside the method to modify the Parameter object:

Param.append ("a");

Java Interview--java Basic section (i)

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.