我试图使用下面的DbQuery.java类执行简单查询,该类使用DbConnector从DriverManager获取连接.
注意:
你能帮我理解这里的问题吗?
1)DbConnector.java
package com.me.ocpjp.chapter10;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnector{
public static Connection connectToDb() throws SQLException{
String url = "jdbc:mysql//localhost:3306/";
String db = "addressBook";
String username = "root";
String password = "tcial";
return DriverManager.getConnection(url+db, username, password);
}
}
Run Code Online (Sandbox Code Playgroud)
2)DbQuery.java
package com.me.ocpjp.chapter10;
import java.sql.Connection ;
import java.sql.Statement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import com.me.ocpjp.chapter10.DbConnector;
public class DbQuery{ …Run Code Online (Sandbox Code Playgroud) 在我学习OCPJP8期间,我遇到了一个问题,对我来说没有非常明确的答案.考虑以下代码:
public class Animals
{
class Lamb implements Closeable
{
public void close()
{
throw new RuntimeException("a");
}
}
public static void main(String[] args)
{
new Animals().run();
}
public void run()
{
try (Lamb l = new Lamb();)
{
throw new IOException();
}
catch (Exception e)
{
throw new RuntimeException("c");
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据书中正确答案的问题"代码会抛出哪个例外?" 是"没有被抑制的异常的运行时异常c".我在Eclipse中检查了这段代码,system.out表明这本书是对的.但是,我还修改了一些代码并在抛出RuntimeException"c"之前添加了以下system.out
System.out.println(e.getSuppressed().toString());
Run Code Online (Sandbox Code Playgroud)
我从这个system.out得到的输出是:
[Ljava.lang.Throwable; @ 75da931b
很明显有一个被抑制的例外.在调试模式下,我还发现这个被抑制的异常是close()方法中的一个.
两个问题:1.为什么控制台中没有关于close()方法抛出异常的信息?这本书的答案是否正确?
为了准备SCJP(或现在已知的OCPJP)考试,我被一些关于传递(参考)价值和不变性的模拟问题所困扰.
我的理解是,当您将变量传递给方法时,您传递的是表示如何获取该变量的位的副本,而不是实际的对象本身.
您发送的副本指向同一个对象,因此您可以修改该对象(如果它是可变的),例如附加到StringBuilder.但是,如果对不可变对象执行某些操作(例如递增整数),则本地引用变量现在指向新对象,并且原始引用变量仍然无视此对象.
考虑我的例子:
public class PassByValueExperiment
{
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder();
sb.append("hello");
doSomething(sb);
System.out.println(sb);
Integer i = 0;
System.out.println("i before method call : " + i);
doSomethingAgain(i);
System.out.println("i after method call: " + i);
}
private static void doSomethingAgain(Integer localI)
{
// Integer is immutable, so by incrementing it, localI refers to newly created object, not the existing one
localI++;
}
private static void doSomething(StringBuilder localSb)
{
// localSb is a different …Run Code Online (Sandbox Code Playgroud) 是否有人知道AtomicLongFieldUpdate类的任何实际使用?我已经阅读了描述,但我还没有完全理解它的含义.为什么我想知道这个?好奇心和OCPJP准备.
提前致谢.
我是 stackoverflow.com 的新手,但我经常在遇到问题时用它来搜索答案,但现在我找不到任何搜索我的问题的结果,所以我在这里问:) 我正在学习OCPJP SE 7 认证,考试 1Z0-804,我正在使用一本书(只有一个可用的 afaik,Ganesh\Sharma 的一个)在集合章节中,关于 Comparator 接口,这本书提供了使用两个 Comparator 的示例和 Comparable 接口对学生元素数组进行排序,但问题是关于比较器:
import java.util.*;
class Student implements Comparable<Student> {
private String id, name;
private Double cgpa;
public String getName() {
return name;
}
public String getId() {
return id;
}
public Double getCgpa() {
return cgpa;
}
public Student(String studentId, String studentName, double studentCGPA) {
id=studentId;
name=studentName;
cgpa=studentCGPA;
}
public String toString() {
return id+" "+name+" "+cgpa;
}
public int compareTo(Student that) {
return this.id.compareTo(that.id);
} …Run Code Online (Sandbox Code Playgroud) 码:
public class Foo {
static void test(String s){
System.out.println("String called");
}
static void test(int s){
System.out.println("int called");
}
public static void main(String[] args) throws Exception {
test(5>8? 5:8); // Line 1
test(5>8? "he":"ha"); // Line 2
test(5>8? 5:"ha"); // Line 3
System.out.println(5<8? 5:"ha"); //Line 4
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行此代码时,出现以下错误 Line 3
Foo.java:24: error: no suitable method found for test(INT#1)
test(5>8? 5:"ha"); // Line 3
^
Run Code Online (Sandbox Code Playgroud)
在三元运算符中使用类似的类型不会产生错误。但是使用不同的类型只会给方法调用带来错误,test(5>8? 5:"ha");但对调用有效System.out.println(5<8? 5:"ha");
当我添加另一个重载的方法时static void test(Object s){},然后进行//Line 3编译。 …
class A{
A aob;
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new A(); //tricky assignement
c=b; //one object eligible GC
c.aob=null;
System.gc();
}
}
Run Code Online (Sandbox Code Playgroud)
有两个对象有资格进行垃圾收集,但有一个很难理解.
A d=new A().aob=new A();
Run Code Online (Sandbox Code Playgroud)
1)这一行我会做到这一点
A d = new A().aob = new A();
^ ^
O1 O2
O1 --> O2 --> null
^
|
d ----|
Run Code Online (Sandbox Code Playgroud)
2)但真正做的是这个(所以一个符合条件的对象)为什么这样?
A d = new A().aob = new A();
^ ^
O1 …Run Code Online (Sandbox Code Playgroud) 以下代码示例编译但带有编译器警告
class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
class SubArrayList<T> extends ArrayList{}
class ZiggyTest2{
public static void main(String[] args){
ArrayList<Animal> nums = new SubArrayList<Animal>();
}
public static void testMethod(ArrayList<Animal> anim){
System.out.println("In TestMethod");
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译以上时,我得到以下警告
Note: ZiggyTest2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Run Code Online (Sandbox Code Playgroud)
或者,如果我使用-Xlint编译它:未选中我收到以下警告
ZiggyTest2.java:12: warning: [unchecked] unchecked conversion
found : SubArrayList<Animal>
required: java.util.ArrayList<Animal>
ArrayList<Animal> nums = new SubArrayList<Animal>();
^
1 warning
Run Code Online (Sandbox Code Playgroud)
如果我将nums的初始化更改为
List<Animal> nums = new ArrayList<Animal>();
Run Code Online (Sandbox Code Playgroud)
然后我没有得到任何警告.
他们为什么表现不同.ArrayList是List的子类型,SubArrayList是ArrayList的子类型,所以我期望实例化是相同的.
谢谢.
当我练习参加OCJP考试时,我发现我想要练习的问题通常都是技巧问题,试图欺骗我回答错误.这个例子来自Bathes/Sierra的书,一个典型的技巧问题是:

现在我想知道你是否可以告诉我,真实考试中的问题是否经常是这样的技巧问题,或者实际考试是否有另一种风格,或者这是否接近我的期望?
public class Dog
{
int collarID;
String name;
public static void main(String[] args){
Dog d = new Dog();
d.name="hose";
System.out.print(d.hashCode());
}
public boolean equals(Object arg0)
{
if (arg0 instanceof Dog)
{
Dog new_name = (Dog) arg0;
return collarID==new_name.collarID && new_name.name.equals(name);
}
return false;
}
public int hashCode()
{
return toString().length();//StackOverflow
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?是否因为默认的toString()方法而对hashCode()方法进行了循环调用?
java ×10
ocpjp ×10
scjp ×5
arrays ×1
atomic ×1
collections ×1
comparator ×1
exception ×1
generics ×1
immutability ×1
jdbc ×1
nested ×1
return-value ×1
sqlexception ×1
string ×1