我正在阅读从构造函数中抛出异常的主题.在stackflow上研究了一些相同的主题之后.我得出的结论是,我们可以从构造函数中抛出异常.当我们尝试子类化其构造函数抛出异常的父类时,就会出现问题.例如,请参阅下面的以下代码段.
class ParentConstructorException{
public ParentConstructorException() throws IOException {
}
}
public class TestConstructorException extends ParentConstructorException {
public TestConstructorException() throws Exception{
}
//Causes compile time error if i don't throw exception
public TestConstructorException(int x){
}
}
Run Code Online (Sandbox Code Playgroud)
我在子类中提供了一个no-arg构造函数,它抛出了一个更广泛的异常.当我重载构造函数时,它说我没有处理检查的异常.那么这意味着我们不能在不抛出相同或更广泛的已检查异常的情况下启动子类?请有人解释一下.本
我在String转换为UTF-8时遇到问题.基本上,我已经开发了一个DayDescriptionEnum,它实际上会获得星期几并相应地返回描述.请看下面的代码.
public enum DayDescriptionEnum {
SUNDAY(1, "Sunday", "\\u0627\\u0644\\u0623\\u062d\\u062f"),
MONDAY(2, "Monday", "\\u0627\\u0644\\u0625\\u062b\\u0646\\u064a\\u0646"),
TUESDAY(3, "Tuesday", "\\u0627\\u0644\\u062b\\u0644\\u0627\\u062b\\u0627\\u0621"),
WEDNESDAY(4, "Wednesday", "\\u0627\\u0644\\u0623\\u0631\\u0628\\u0639\\u0627\\u0621"),
THURSDAY(5, "Thursday", "\\u0627\\u0644\\u062e\\u0645\\u064a\\u0633"),
FRIDAY(6, "Friday", "\\u0627\\u0644\\u062c\\u0645\\u0639\\u0629"),
SATURDAY(7, "Saturday", "\\u0627\\u0644\\u0633\\u0628\\u062a");
private long dayofWeek;
private String dayDescriptionEnglish;
private String dayDescriptionArabic;
private DayDescriptionEnum(long dayofWeek, String dayDescriptionEnglish, String dayDescriptionArabic) {
this.dayofWeek = dayofWeek;
this.dayDescriptionEnglish = dayDescriptionEnglish;
this.dayDescriptionArabic = dayDescriptionArabic;
}
public long getDayofWeek() {
return dayofWeek;
}
public String getDayDescriptionEnglish() {
return dayDescriptionEnglish;
}
public String getDayDescriptionArabic() {
return dayDescriptionArabic;
}
}
public static DayDescriptionEnum getDescriptionOfDay(long dayOfWeek){ …Run Code Online (Sandbox Code Playgroud) 我有一个例子,我对输出感到困惑.
enum Seasons {
WINTER,
SUMMER,
SPRING,
AUTUMN;
}
public class SeasonTest{
public static void main(String[] args) {
Seasons season1 = Seasons.WINTER;
Seasons season2 = Seasons.SUMMER;
Seasons season3 = Seasons.SPRING;
Seasons season4 = Seasons.AUTUMN;
System.out.println(season1.compareTo(season2));
System.out.println(season3.compareTo(season4));
System.out.println(season4.compareTo(season3));
System.out.println(season2.compareTo(season1));
System.out.println(season3.compareTo(season3));
System.out.println(season1.compareTo(season4));
}
}
Run Code Online (Sandbox Code Playgroud)
最后一行返回-3.这是为什么?它应该返回-1,因为season1小于season4.为什么它会返回-3?
谢谢.
我很困惑Arrays.binarySearch(Object[], Object).
public class SearchObjArray {
public static void main(String[] args){
String[] sa = {"one","two","three","four"};
Arrays.sort(sa);
for(String s : sa ){
System.out.println(s + " ");
}
System.out.println("\n one = " + Arrays.binarySearch(sa,"thro"));
}
}
Run Code Online (Sandbox Code Playgroud)
程序运行时,它返回位置-4.我正在读书,它说,插入点表示为(-(insertionPoint)-1).为什么会这样?我无法理解这一点.
我想知道为什么java中的匿名内部类被标记为final.我已阅读这篇 文章,但无法理解这个概念.甚至JLS指定所有匿名内部类都是隐式最终的.有人可以详细说明这个概念吗?
我知道线程连接方法是如何工作的,但我有一个示例问题.请参阅下面的示例代码
public class RunnableJob implements Runnable{
@Override
public void run(){
Thread currentThread = Thread.currentThread();
System.out.println("Runnable job is run by" + currentThread.getName());
try{
Thread.sleep(1000);
}
catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
public class ThreadExample{
public static void main(String[] args) throws InterruptedException{
RunnableJob runnableJob = new RunnableJob();
Thread thread1 = new Thread(runnableJob,"T1");
Thread thread2 = new Thread(runnableJob,"T2");
Thread thread3 = new Thread(runnableJob,"T3");
Thread thread4 = new Thread(runnableJob,"T4");
thread1.start();
thread1.join();
thread2.start();
thread2.join();
thread3.start();
thread3.join();
thread4.start();
thread4.join();
Thread thread5 = new Thread(runnableJob,"T5");
Thread thread6 …Run Code Online (Sandbox Code Playgroud)