面试题(1-10)

面向对象的特征有哪些方面?

  • 抽象

    抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象行为抽象两方面。

    抽象只关注对象有哪些属性和行为,并不关注这些行为的细节是什么。

  • 继承

    继承是从已有类得到继承信息创建新类的过程。

    提供继承信息的类被称为父类(超类、基类),得到继承信息的类被称为子类(派生类)。

  • 封装

    通常认为封装是把数据和操作数据的方法绑定起来,对数据的访问只能通过已定义的接口。

  • 多态

    多态是是指允许不同子类型的对象对同一消息做出不同的响应。

    多态分为编译时的多态性运行时的多态性

    方法重载(overload)实现的是编译时的多态性(也称为前绑定);方法重写(override)实现的是运行时的多态性(也称为后绑定)。

    运行时的多态是面向对象最精髓的东西,要实现多态需要两件事:

    ​ 方法重写(子类继承父类并重写父类中已有的或抽象的方法);

    ​ 对象构造(用父类型引用子类型对象,这样同样的引用调用同样的方法就会根据子类对象的不同而表现出不同的行为);

访问修饰符publicprivateprotected以及不写(默认)时的区别?

修饰符当前类同包子类其他包
public
protected
default
private
  • 类的成员不写访问修饰符默认为default。默认对于同一个包中的其他类相当于公开(public),对于不是同一个包中的其他类相当于私有(private
  • 受保护(protected)对子类相当于公开,对不是同一个包中的没有父子关系的类相当于私有(private
  • Java中,外部类的修饰符只能是public或默认(default),类的成员(包括内部类)的修饰符可以是以上四种。

String是最基本的数据类型吗?

​ 不是。

​ Java中的基本数据类型只有8个:byteshortintlongfloatdoublecharboolean;除了基本类型(primitive type),剩下的都是引用类型reference type);Java5以后引入的枚举类型也算是以在比较特殊的引用类型。

float f = 3.4;是否正确?

​ 不正确。

​ 3.4是双精度数,将双精度型(double)赋值给浮点型(float)属于向下转型(down-casting,也成为窄化),会造成精度损失,因此需要强制类型转换float f = (float)3.4;或者写成float=3.4F

short s1 = 1; s1 = s1 + 1;有错吗?short s1 = 1; s1 += 1;有错吗?

​ 对于short s1 = 1; s1 = s1 + 1;,由于1是int类型,因此s1+1运算结果也是int类型,需要强制类型转换才能赋值给short型。

​ 对于short s1 = 1; s1 += 1;,可以正确编译,因为s1+=1相当于s1=(short)(s1+1),其中有隐含的强制类型转换。

Java有没有goto

goto是Java中的保留字,在目前版本的Java中没有使用。

intInteger有什么区别?

​ 为了编程方便,Java引入了基本数据类型,到那时为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper class),int的包装类就是Integer。从Java5开始引入了自动装箱/拆箱操作,使得二者可以相互转换。

  • 基本数据类型:booleancharbyteshortintlongfloatdouble
  • 包装类型:BooleanCharacterByteShortIntegerLongFloatDouble
@Test
  public void test1() {
    Integer a = new Integer(3);
    //将3自动装箱成Integer类型
    Integer b = 3;
    int c = 3;

    System.out.println(a == b);
    //false:两个引用没有引用同一对象
    System.out.println(a == c);
    //true:自动拆箱成int类型再和c比较
  }
 @Test
  public void test2() {
    Integer f1 = 100;
    Integer f2 = 100;

    Integer f3 = 150;
    Integer f4 = 150;

    System.out.println(f1 == f2);
    System.out.println(f3 == f4);
  }

当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf()

/**
  * Returns an {@code Integer} instance representing the specified
  * {@code int} value.  If a new {@code Integer} instance is not
  * required, this method should generally be used in preference to
  * the constructor {@link #Integer(int)}, as this method is likely
  * to yield significantly better space and time performance by
  * caching frequently requested values.
  *
  * This method will always cache values in the range -128 to 127,
  * inclusive, and may cache other values outside of this range.
  *
  * @param  i an {@code int} value.
  * @return an {@code Integer} instance representing {@code i}.
  * @since  1.5
  */
public static Integer valueOf(int i) {
  if (i >= IntegerCache.low && i <= IntegerCache.high)
    return IntegerCache.cache[i + (-IntegerCache.low)];
  return new Integer(i);
}

IntegerCache是Integer的内部类,其代码如下:

/**
	* Cache to support the object identity semantics of autoboxing for values between
  * -128 and 127 (inclusive) as required by JLS.
  *
  * The cache is initialized on first usage.  The size of the cache
  * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
  * During VM initialization, java.lang.Integer.IntegerCache.high property
  * may be set and saved in the private system properties in the
  * sun.misc.VM class.
  */
private static class IntegerCache {
  static final int low = -128;
  static final int high;
  static final Integer cache[];

  static {
    // high value may be configured by property
    int h = 127;
    String integerCacheHighPropValue =
      sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
    if (integerCacheHighPropValue != null) {
      try {
        int i = parseInt(integerCacheHighPropValue);
        i = Math.max(i, 127);
        // Maximum array size is Integer.MAX_VALUE
        h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
      } catch( NumberFormatException nfe) {
        // If the property cannot be parsed into an int, ignore it.
      }
    }
    high = h;

    cache = new Integer[(high - low) + 1];
    int j = low;
    for(int k = 0; k < cache.length; k++)
      cache[k] = new Integer(j++);

    // range [-128, 127] must be interned (JLS7 5.1.7)
    assert IntegerCache.high >= 127;
  }

  private IntegerCache() {}
}

​ 简单的说,如果整型字面量的值在-128到127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,所以f1 == f2的结果是true,而f3 == f4的结果是false。

&和&&的区别?

  • &运算符有两种用法:(1)按位与;(2)逻辑与。&&运算符是短路与运算。
  • 如果&&左边的表达式的值是false,右边的表达式会被直接短路,不会进行运算。
  • 例如验证用户登录时判定用户名不是null也不是空字符串时,应当写成null != username && ("").equals(username)

解释内存中的栈(stack)、堆(heap)和方法区(method area)的用法

  • 通常我们定义一个基本数据类型的变量,一个对象的引用,还有就是函数调用的现场保存都使用JVM中的栈空间;
  • 通过new关键字和构造器创建的对象则放在堆空间,堆是垃圾收集器管理的主要区域,由于现在的垃圾收集器都采用分代收集算法,所以堆空间还可以细分为新生代和老年代。再具体一点可以分为Eden、Survivor(又可以分为From Survivor 和 To Survivor)、Tenured;
  • 方法区和堆都是各个线程共享的内存区域,用于存储已经被JVM加载的类信息、常量、静态变量、JIT编译器编译后的代码等数据;
  • 程序中的字面量()如直接书写的100、“hello”和常量都是放在常量池中,常量池是方法区的一部分。
  • 栈空间操作起来最快但是栈很小,通常大量的对象都是放在堆空间,栈和堆的大小都可以通过JVM的启动参数来进行调整,栈空间用光了会引发StackOverflowError,而堆和常量池空间不足则会引发OutofMemoryError。
  • String str = new String("hello");:变量str放在栈上,用new创建出来的字符串对象放在堆上,而"hello"这个字面量是放在方法区的。
  • 运行时常量池相当于Class文件常量池具有动态性,Java语言并不要求常量一定只有编译期间才能产生,运行期间也可以将新的常量放入池中,String类的intern()方法就是这样的。

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

  • Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。
  • 四舍五入的原理是在参数上加0.5然后进行向下取整。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值