为什么我不能在包外面使用受保护的构造函数来获取这段代码:
package code;
public class Example{
protected Example(){}
...
}
Run Code Online (Sandbox Code Playgroud)
Check.java
package test;
public class Check extends Example {
void m1() {
Example ex=new Example(); //compilation error
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
编译错误:
构造函数Example()不可见
字符串类是不可变的,其背后的原因之一是该类被声明为final,尽管还有其他原因.但是为什么StringBuffer或StringBuilder最终仍然是可变的?那么还有哪些因素决定String是不可变的?
我遇到了这两种对字符串对象进行空检查的方法.
给定一个字符串对象String str ="example";
If(str.someMethod() != null ) 要么If (null != str.someMethod())为什么我们更喜欢第二个?这背后的确切原因是什么,它与性能有关吗?
这背后的原因是什么:
为什么不允许这样做 StringBuffer sb=(String)"Java";
这是允许的 StringBuffer sb=new StringBuffer("Java");
原始数据类型是否扩展了Object类?如果没有,那么这段代码怎么可能
long l=4567;
Object o=l;
System.out.println(o);
Run Code Online (Sandbox Code Playgroud)
为什么我们不会得到任何编译错误?
给出下面的代码:
//Super class
class A {
void m1() {
System.out.println("A");
}
}
//Extending the super class
class B extends A {
void m1() {
System.out.println("B");
}
}
// Class with the main method
public class C extends B {
void m1() {
System.out.println("C");
}
}
//in some main method
B b1 = new B(); //creating B object
System.out.println(b1 instanceof A); //gives true
B b = (B) new A();
b.m1();
Run Code Online (Sandbox Code Playgroud)
instanceof运营商提供了真正所以B是类的一个对象A.A到B …