Java basic interview questions and reference analysis ------------ 02

What are the basic data types 1.Java there? String is the basic data type? Whether the String class can inherit?


A: 4 1.java class defines eight basic types:

                       Int: byte, short, int, long
                       float: float, double
                       Boolean: boolean
                       character: char
                    2.String not the basic data types, String belongs to the reference type.
             3.String class is a final class can not be inherited.

 

2. Description of ++ i and i ++ difference?


 

 3.short s = 1; s = s + 1; there anything wrong? short s = 1; s + = 1; there anything wrong?


 A: 1 for short s = 1; s = s + 1; int Since the short data type and the data type represents a different range, it is necessary for the type of conversion.

    Solution: either to the short int, or cast (short) s + 1.

  2. For short s = 1; s + = 1; = + Because Java is a predetermined operator, it will Java compiler special treatment, it is possible to correct translation.

 

  For short s1 = 1; s1 = s1 + 1; int Since the type 1 is, therefore s1 + 1 calculation result is an int, need to be assigned to the type of cast short type.
  And short s1 = 1; s1 + = 1; + = operator may be an implicit type conversion automatically , is defined in the Java language operators ;
  Java compiler will it special treatment, it can be built correctly. Since s1 + = 1; corresponds to s1 = (short) (s1 + 1).
 
4, access modifiers public, private, protected, and when the difference is not write (default default)?

 

The default is default when members of the class do not write access modifications. The default for the same package disclosed in other classes corresponding to (public), a packet is not the same for the other classes as a private (private). Protected (protected) subclass equivalent public , not the same kind of parent-child relationship is not the package as a private. Java, the outer class modifiers can only be public or default modifier member of the class (including inner classes) can be more than four.
 
 
5.int and Integer What is the difference?

Java is a nearly pure object-oriented programming language, but in order to facilitate the programming or the introduction of the basic data types, but in order to be able to these basic data types as object operation, Java for each elementary data types introduced corresponding type of packaging ( wrapper class), int wrapper class is Integer, since Java 5 introduced automatic boxing / unboxing mechanism, so that the two can be interchangeable.
Java provides a package type for each primitive type:

  • Primitive types: boolean, char, byte, short, int, long, float, double
  • Package Type: Boolean, Character, Byte, Short, Integer, Long, Float, Double

 

6. & and && difference?


& Operator in two ways: (1) bit and; (2) a logical AND.

&&运算符是短路与运算。逻辑与跟短路与的差别是非常巨大的,虽然二者都要求运算符左右两端的布尔值都是true整个表达式的值才是true。&&之所以称为短路运算是因为,如果&&左边的表达式的值是false,右边的表达式会被直接短路掉,不会进行运算。很多时候我们可能都需要用&&而不是&,例如在验证用户登录时判定用户名不是null而且不是空字符串,应当写为:username != null &&!username.equals(""),二者的顺序不能交换,更不能用&运算符,因为第一个条件如果不成立,根本不能进行字符串的equals比较,否则会产生NullPointerException异常。注意:逻辑或运算符(|)和短路或运算符(||)的差别也是如此。

 

7.用最有效率的方法计算2乘以8?


2 << 3(左移3位相当于乘以2的3次方,右移3位相当于除以2的3次方)

 

8.Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?


Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在参数上加0.5然后进行下取整。

 

9.数组有没有length()方法?String有没有length()方法?


 

数组没有length()方法,有length 的属性。String 有length()方法。JavaScript中,获得字符串的长度是通过length属性得到的,这一点容易和Java混淆。

 

10.列出一些你常见的运行时异常?


答:

  • ArithmeticException(算术异常)
  • ClassCastException (类转换异常)
  • IllegalArgumentException (非法参数异常)
  • IndexOutOfBoundsException (下标越界异常)
  • NullPointerException (空指针异常)
  • SecurityException (安全异常)

 

 

11.用Java写一个折半查找。
 1 import java.util.Comparator;
 2 
 3 public class MyUtil {
 4 
 5    public static <T extends Comparable<T>> int binarySearch(T[] x, T key) {
 6       return binarySearch(x, 0, x.length- 1, key);
 7    }
 8 
 9    // 使用循环实现的二分查找
10    public static <T> int binarySearch(T[] x, T key, Comparator<T> comp) {
11       int low = 0;
12       int high = x.length - 1;
13       while (low <= high) {
14           int mid = (low + high) >>> 1;
15           int cmp = comp.compare(x[mid], key);
16           if (cmp < 0) {
17             low= mid + 1;
18           }
19           else if (cmp > 0) {
20             high= mid - 1;
21           }
22           else {
23             return mid;
24           }
25       }
26       return -1;
27    }
28 
29    // 使用递归实现的二分查找
30    private static<T extends Comparable<T>> int binarySearch(T[] x, int low, int high, T key) {
31       if(low <= high) {
32         int mid = low + ((high -low) >> 1);
33         if(key.compareTo(x[mid])== 0) {
34            return mid;
35         }
36         else if(key.compareTo(x[mid])< 0) {
37            return binarySearch(x,low, mid - 1, key);
38         }
39         else {
40            return binarySearch(x,mid + 1, high, key);
41         }
42       }
43       return -1;
44    }
45 }
View Code

答:折半查找,也称二分查找、二分搜索,是一种在有序数组中查找某一特定元素的搜索算法。搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组已经为空,则表示找不到指定的元素。这种搜索算法每一次比较都使搜索范围缩小一半,其时间复杂度是O(logN)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
 
 

 

 

Guess you like

Origin www.cnblogs.com/hhl686/p/11208342.html