Java Core Basics (1) Basic syntax

Source: Internet
Author: User

Java Basic syntax

Summary of Interview questions (1)

1. Can I include more than one class (not an inner class) in a ". Java" source file? What's the limit?

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

2. Is there a goto in Java?

?? Yes, but it is used as a reserved word and is not currently used in Java.

3, eight basic data types and packaging classes and sizes.
Eight basic data Types          wrapper class              size byte                    byte             8-bit     -2^7   ~  2^7-1short                   Short            16-bit    -2^15  ~  2^15-1       int                     Integer          32-bit    -2^31  ~  2^31-1long                    Long             64-bit    -2^63  ~  2^63-1double                  Double           64-bit    -2^63  ~  2^63-1char                    Character        16-bit    \u0000 ~  \uffff Boolean                 boolean          1 bit  
4, talk about the difference between & and &&.

?? Both & and && can be used as the logical AND operator, representing logic and (and), when the result of an expression on either side of the operator is true, the entire result 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 an if (str! = null&&!str.equals (")) expression, when STR is NULL, The following expression does not execute, so no nullpointerexception (null pointer exception) will be thrown if && is changed to & The nullpointerexception exception is raised.
?? & can also be used as a bitwise operator, when an expression on either side of the & operator is not a Boolean, & we typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of that integer, for example, 0x31 &
The result of 0x0f is 0x01.

5, the most efficient way to calculate 2 times 8 is equal to a few?

?? 2 << 3,
?? Because moving a number to the left n-bit, is equivalent to multiplying by 2 of the N-square, then, a number multiplied by 8 to move it to the left 3 bits, and the bit operation CPU directly supported, the most efficient, so, 2 times 8 and the most efficient method is 2 << 3.

6. What are the data types that can be switch?

?? In switch (EXPR1), EXPR1 can only be an integer expression or enumeration constant.
So the data types that can be switch are: Byte,short,int,char.
String is also available after jdk1.7.

7. Can I store a Chinese character in char type?

?? Char variables are used to store Unicode encoded characters, the Unicode encoding character set contains Chinese characters, Unicode encoding occupies two bytes, so variables of type char are also two bytes. A char type has a size of 16 bits, which is two bytes, so it can store a Chinese character.

8. Sorting in arrays

Select sort:
?? No. 0 Successive and first, second, third?????? Compare, and with a smaller swap position than his, after round the No. 0 is the minimum, the 1th in and the second, the third?????? Compare, and with a smaller swap position than he did. Loop in turn, at which time the first of the previous round does not compare, and finally gets a small to large sequence.

public   Static  void  selectsort  (int  [] arr) {//1, defines an outer loop that determines the elements participating in the comparison.  for  (int  x =  0 ; x < arr.length-1 ; X + +) {for  (int  y = × + 1 ; Y < arr.length; y++) {//to determine the previous one greater than the next, the position is replaced.  if  (Arr[x] > Arr[y]) {swap (                Arr, x, y); }            }        }    }

Bubble sort:
?? The No. 0 and the first comparison, if the No. 0 large and the first swap position, the first and second comparisons, the large back shift, the second and third comparisons,?????? And so on, the last one after the end of the first round is the maximum value ... At the beginning of the second round of comparison, at this time one does not compare

publicstaticvoidbubbleSort(int[] arr)    {        for (int01; x++)        {            for (int01 - x; y++)            {                if1])                    1);            }        }    }

Java Core Basics (1) Basic syntax

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.