最终变量和编译时常量之间有什么区别?
请考虑以下代码
final int a = 5;
final int b;
b=6;
int x=0;
switch(x)
{
case a: //no error
case b: //compiler error
}
Run Code Online (Sandbox Code Playgroud)
这是什么意思?何时以及如何为最终变量赋值?运行时会发生什么以及编译时会发生什么?我们为什么要给switch一个编译时常量?java的其他结构需要编译时间常量?
为什么Java 8支持静态方法?下面代码中main方法中两行的区别是什么?
package sample;
public class A {
public static void doSomething()
{
System.out.println("Make A do something!");
}
}
public interface I {
public static void doSomething()
{
System.out.println("Make I do something!");
}
}
public class B {
public static void main(String[] args) {
A.doSomething(); //difference between this
I.doSomething(); //and this
}
}
Run Code Online (Sandbox Code Playgroud)
正如我们在上面看到的那样,我甚至没有在B中实现.当我们可以在另一个类中编写相同的静态方法并调用它时,在接口中使用静态方法的目的是什么?是否为模块化以外的任何其他目的而引入.通过模块化,我的意思是:
public interface Singable {
public void sing();
public static String getDefaultScale()
{
return "A minor";
}
}
Run Code Online (Sandbox Code Playgroud)
只是把类似的方法放在一起.
什么时候java打印无限,什么时候打印NaN?
为什么1.0/0.0无穷大但((1.0/0.0) - (1.0/0.0))NaN和0.0f/0.0f NaN?
这两者有什么区别?
class SomeClass1 {
void method1() { }
public void method2() { }
private void method3() { }
protected void method4() { }
}
class DemoClass{
public static void main(String[] parameters) {
SomeClass1 sc = new SomeClass1();
sc.method1();
sc.method2();
sc.method4();
}
}
Run Code Online (Sandbox Code Playgroud)
受保护的方法只能由继承超类的类访问.正如我们在这里看到的,DemoClass不会扩展SomeClass.但是,它能够访问受保护的方法.这怎么可能?