ocpjp

Real life use and explanation of the AtomicLongFieldUpdate class

烂漫一生 提交于 2020-01-23 07:21:49
问题 Is anybody aware of any real life use of the class AtomicLongFieldUpdate? I have read the description but I have not quite grasped the meaning of it. Why do I want to know that? Curiosity and for OCPJP preparation. Thanks in advance. 回答1: You can think of a cost ladder for the following: ordinary long : cheap, but unsafe for multi-threaded access volatile long : more expensive, safe for multi-threaded access, atomic operations not possible AtomicLong : most expensive, safe for multi-threaded

Instantiating a Generic Class of Type <?>

烈酒焚心 提交于 2019-12-24 03:29:46
问题 I'm studying for the SCJP/OCPJP and I came across a sample question that seams strange to me. The sample code instantiated two generic collections: List<?> list = new ArrayList<?>(); List<? extends Object> list2 = new ArrayList<? extends Object>(); The "correct" answer to the question was that this code would compile but adding to either collection would produce a runtime error. When I try to compile code like this I just get errors. The Java tutorial does not even show this type of code, it

Slight confusion regarding overriding where variables are concerned

只愿长相守 提交于 2019-12-17 06:12:17
问题 I'm preparing for the SCJP (recently rebranded as OCPJP by Oracle) and one particular question that I got wrong on a mock exam has confused me, the answer description doesn't explain things clear enough. This is the question : class A { int x = 5; } class B extends A { int x = 6; } public class CovariantTest { public A getObject() { return new A(); } public static void main(String[]args) { CovariantTest c1 = new SubCovariantTest(); System.out.println(c1.getObject().x); } } class

Why does Double.NaN==Double.NaN return false?

匆匆过客 提交于 2019-12-17 02:28:30
问题 I was just studying OCPJP questions and I found this strange code: public static void main(String a[]) { System.out.println(Double.NaN==Double.NaN); System.out.println(Double.NaN!=Double.NaN); } When I ran the code, I got: false true How is the output false when we're comparing two things that look the same as each other? What does NaN mean? 回答1: NaN means "Not a Number". Java Language Specification (JLS) Third Edition says: An operation that overflows produces a signed infinity, an operation

java.sql.SQLException: No suitable driver found

谁都会走 提交于 2019-12-10 13:12:02
问题 I am trying to execute simple query using below DbQuery.java class which uses DbConnector to get a Connection from DriverManager. note : I have already included "mysql-connector-java-5.1.25-bin.jar" on my classpath via: export CLASSPATH=$CLASSPATH:/home/me/ocpjp/chapter-10/mysql-connector-java-5.1.25/mysql-connector-java-5.1.25-bin.jar I am able to connect to mysql with "mysql -uroot -ptcial addressBook", if it matters. have also tried running with '-cp' argument with no avail. I am able to

Eligible variables for garbage collection in Java

社会主义新天地 提交于 2019-12-06 00:53:06
问题 I am preparing for OCPJP, and I got stuck at the following mock exam question: Given: 3. interface Animal { void makeNoise(); } 4. class Horse implements Animal { 5. Long weight = 1200L; 6. public void makeNoise() { System.out.println("whinny"); } 7. } 8. public class Icelandic extends Horse { 9. public void makeNoise() { System.out.println("vinny"); } 10. public static void main(String[] args) { 11. Icelandic i1 = new Icelandic(); 12. Icelandic i2 = new Icelandic(); 12. Icelandic i3 = new

Eligible variables for garbage collection in Java

早过忘川 提交于 2019-12-04 05:54:02
I am preparing for OCPJP , and I got stuck at the following mock exam question: Given: 3. interface Animal { void makeNoise(); } 4. class Horse implements Animal { 5. Long weight = 1200L; 6. public void makeNoise() { System.out.println("whinny"); } 7. } 8. public class Icelandic extends Horse { 9. public void makeNoise() { System.out.println("vinny"); } 10. public static void main(String[] args) { 11. Icelandic i1 = new Icelandic(); 12. Icelandic i2 = new Icelandic(); 12. Icelandic i3 = new Icelandic(); 13. i3 = i1; i1 = i2; i2 = null; i3 = i1; 14. } 15. } When line 14 is reached, how many

Post and Pre increment operators

自闭症网瘾萝莉.ら 提交于 2019-12-04 03:15:47
问题 When i run the following example i get the output 0,2,1 class ZiggyTest2{ static int f1(int i) { System.out.print(i + ","); return 0; } public static void main(String[] args) { int i = 0; int j = 0; j = i++; //After this statement j=0 i=1 j = j + f1(j); //After this statement j=0 i=1 i = i++ + f1(i); //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0 System.out.println(i); //prints 2? } } I don't understand why the output is 0,2,1 and not 0,2,2 回答1: i = i++ + f1(i);

Post and Pre increment operators

随声附和 提交于 2019-12-01 16:14:10
When i run the following example i get the output 0,2,1 class ZiggyTest2{ static int f1(int i) { System.out.print(i + ","); return 0; } public static void main(String[] args) { int i = 0; int j = 0; j = i++; //After this statement j=0 i=1 j = j + f1(j); //After this statement j=0 i=1 i = i++ + f1(i); //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0 System.out.println(i); //prints 2? } } I don't understand why the output is 0,2,1 and not 0,2,2 i = i++ + f1(i); i++ means i is now 2 . The call f1(i) prints 2 but returns 0 so i=2 and j=0 before this i = 1 , now imagine

How to programmatically test if assertions are enabled?

隐身守侯 提交于 2019-12-01 02:37:13
One of the correct answers from OCP Java SE 6 Programmer Practice Exams is: You can programmatically test wheather assertions have been enabled without throwing an AssertionError . How can I do that? I use this boolean assertOn = false; // *assigns* true if assertions are on. assert assertOn = true; I am not sure this is the "official" way. I guess you should use Class.desiredAssertionStatus() http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#desiredAssertionStatus() Joe The Oracle Java Tutorial provides information about how to do it... http://docs.oracle.com/javase/7/docs