关于在Java中重新抛出异常,我有一个非常简单的问题.
这是代码片段:
public static void main(String[] args) throws FileNotFoundException {
try {
FileReader reader = new FileReader("java.pdf");
} catch (FileNotFoundException ex) {
throw ex;
}
}
public static void main(String[] args) throws FileNotFoundException {
FileReader reader = new FileReader("java.pdf");
}
Run Code Online (Sandbox Code Playgroud)
为什么我们需要重新抛出ex
第一个版本,而第二个版本看起来更优雅?什么可能是好处,哪个版本比另一个更受欢迎?
我已阅读内部课程教程并且不了解一件事.据说内部类隐藏了对外类的引用,所以我通过这个普通类提出了几个问题:
public class OuterClass {
public void doSomething() {
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
所以我们有一个本地内部类,它位于方法内部doSomething()
,我有一些问题.
这个本地内部类是否持有对OuterClass的引用,因为它是本地的?
在方法doSomething()
终止后,此本地内部类是否保留内存?
是否有任何OuterClass符合GC条件的情况,但本地内部类仍然被其他类引用?会发生什么?
你能告诉我以下的调用是否可以重入?
public class Foo {
public synchronized void doSomething() {}
public synchronized void doAnotherSomething() {}
}
public class Too {
private Foo foo;
public synchronized void doToo() {
foo.doSomething();
//...other thread interfere here....
foo.doAnotherSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
方法doToo()
可重入的2个连续调用?我不确定这种情况,因为foo.doSomething()
方法获取并释放内部锁,两次调用之间没有嵌套同步.是否存在其他线程可能在两次调用之间发生干扰的情况?
我是spring框架的新手今天我遇到了web.xml文件中的调度程序servlet配置,我想出了一个关于url模式的问题,比如这个语法/.那么,如果我在tomcat服务器中部署web应用程序,实际上"/"符号适用的是:host:port /或host:port/myWeb /
尝试notifyAll()
在synchronized语句中执行调用时出现以下错误:在同步上下文外调用Object.notify().
例:
final List list = new ArrayList();
synchronized(list) {..... invoked notifyAll() here};
Run Code Online (Sandbox Code Playgroud) 据说当函数在C/C++中结束时,将自动分配和释放局部变量.
根据我的理解,当被解除分配时,局部变量所持有的值也会被破坏!如果我错了,请纠正我
考虑以下代码:
void doSomething(int** num)
{
int a = 10;
*num = &a;
} // end of function and a will be destroyed
void main()
{
int* number;
doSomething(&number);
cout << *number << endl; // print 10 ???
}
Run Code Online (Sandbox Code Playgroud)
有人可以为我澄清一下吗?
我有从Myanel扩展的类MyPanel.MyPanel类具有JLabel组件,其中包含一个图标.
我的问题是如何在MyPanel类中绘制/渲染此JLabel组件以获得半透明效果(请参阅图标)(不创建xxxJLabel扩展JLabel类并覆盖paintComponents方法).
谢谢
我已阅读有关Java中原子操作的文章,但仍有一些疑问需要澄清:
int volatile num;
public void doSomething() {
num = 10; // write operation
System.out.println(num) // read
num = 20; // write
System.out.println(num); // read
}
Run Code Online (Sandbox Code Playgroud)
所以我在1方法上做了4次操作,它们是原子操作吗?如果多个线程同时调用doSomething()方法会发生什么?
最近,我一直面临着在JDBC主题中抛出异常的问题.
我使用2个同步方法同步Connection对象:getConnection()和releaseConnection().然后另一个从数据库中删除行的方法如下:
public void removeItem(int itemID) throws ItemNotFound {
PreparedStatement ps = null;
String query = "DELETE * FROM Student.Book WHERE id = ?";
getConnection();
try {
ps = con.prepareStatement(query);
ps.setInt(1, bookID);
ps.executeUpdate();
} catch (SQLException sqle) {
this.close(null, null, ps); // method to close PreparedStatement and ResultSet
releaseConnection();
throw new BookNotFoundException("Book not found!");
} finally {
this.close(null, null, ps);
releaseConnection();
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有异常发生,一切都很好.如果发生异常,在releaseConnection()方法之后的catch块中,抛出新的BookNotFoundException("找不到书!")挂起!! 如果我评论releaseConnection()方法然后它正常抛出?
我已经挣扎了近一周的任务而没有成功找到解决方案,所以这个网站是我最后的希望.
我有0-1背包问题,有20个项目具有不同的值和重量,最大重量为524.现在我需要实施蛮力找到20个项目的最佳解决方案子集,使总权重<= 524和最大值选择的项目.
能否请您指出或更好地详细实施以分析它是如何工作的!非常感谢你
我仍然可以通过使用构造函数实例化,虽然在类定义中它已被声明为私有构造函数??? 这是代码片段:
public class Singleton {
private static Singleton instance;
private String name;
/* Private constructor prevents other classes from instantiating */
private Singleton(String name) {
this.name = name;
// Optional code
}
/*
* Static factory method that creates instance instead of constructor.
* Synchronization helps to block any attempt to instantiate from simultaneous
* thread hence break concept of singleton.
* This method uses a technique known as lazy instantiation.
*/
public static synchronized Singleton getInstance(String name) { …
Run Code Online (Sandbox Code Playgroud)