任何人都可以解释这段代码的输出吗?
public class Main
{
int temp()
{
return(true ? null : 0);
}
public static void main(String[] args)
{
Main m=new Main();
System.out.println(m.temp());
}
}
Run Code Online (Sandbox Code Playgroud) 我写了一段代码:
public class Child{
int y ;
private static final int z = getZ();
static {
System.out.println("The value of z is "+z);
}
public int getX(){
System.out.println("get x");
return 10;
}
public int getY(){
Child ch = new Child();
System.out.println("get y");
ch.y = getX();
return y;
}
public static int getZ(){
System.out.println("get z");
return new Child().getY();
}
public Child(){
System.out.println("Child constructor");
}
public static void main(String...args){
Child ch = new Child();
System.out.println("the value of z in main is "+z);
} …Run Code Online (Sandbox Code Playgroud) 这是对我之前的问题的跟进:
集合 - Iterator.remove() 与 Collection.remove()
下面的两段代码,显然只有一行不同,但一个抛出异常,另一个不抛出。你能解释一下区别吗?
List<String> list = new ArrayList<String>
(Arrays.asList("noob1","noob2","noob3"));
System.out.println(list);
for (String str : list) {
if (str.equals("noob2")) {
list.remove(str);
}
}
Run Code Online (Sandbox Code Playgroud)
运行良好,但如果我将条件更改为
if (!str.equals("noob2"))
Run Code Online (Sandbox Code Playgroud)
代码抛出异常!
how to split text or csv file (100 thousand) into csv files (each one contains 10k lines ) based on the lines count ? Using UNIX script.
我想创建一个组合框,它在运行时从数据库中获取名称.所以我创建了一个空字符串数组,但它抛出了一个异常,即arrayindexoutofbound.我认为初始化有一个错误.....
String s[]=new String[0];
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:project","sa","123456");
Statement stmt= con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT Name FROM company");
i=0;
while(rs.next()) {
s[i]=rs.getString(1);
i++;
}
}
catch(Exception ex)
{
JOptionPane.showConfirmDialog(f,ex);
}
cb=new JComboBox(s);
}
Run Code Online (Sandbox Code Playgroud) 编译以下代码时,编译器显示错误:
InterfaceTest.java:19:错误:找不到符号knightObj.dispBK();
public class InterfaceTest{
public static interface Knight{
public void embark();
}
public static class BraveKnight implements Knight{
private int id;
public BraveKnight(int id){
this.id = id;
}
public void dispBK(){
System.out.println("ID: "+id);
}
public void embark(){
System.out.println("ID: "+id);
}
}
public static void main(String[] args){
Knight knightObj = new BraveKnight(101);
knightObj.dispBK();
}
}
Run Code Online (Sandbox Code Playgroud)
可能的原因是什么?
为什么方法调用System.gc()不能保证垃圾收集器算法会在那一刻运行?为什么它在调用时无法回收所有未使用的对象的内存?
java ×6
arrays ×1
autoboxing ×1
collections ×1
interface ×1
iterator ×1
shell ×1
static ×1
string ×1
unix ×1