scjp

Why do static and instance init blocks in Enums behave differently from those in Classes

可紊 提交于 2019-12-10 14:21:29
问题 In studying for the Java certification test I learned that static initialization blocks run once when the class is loaded, in order of appearance within the source code, that instance initialization blocks run each time an instance is created, and that code in constructors runs each time an instance is created after that. To test that I created a class with some static and instance init blocks and a contructor with print stuff. Everything worked as expected -- except I thought "loaded" meant

What class does the target object take on after casting?

孤人 提交于 2019-12-08 08:17:53
问题 OK, noob question. I'm studying for the SCJP and got 3 questions on object reference casting wrong which all seem to point to the same misunderstanding. Just wanted to confirm what the right insight should be. Right, here are the questions: 1. 1. class CodeWalkFour { 2. public static void main(String[] args){ 3. Car c = new Lexus(); 4. System.out.print(c.speedUp(30) + " "); 5. Lexus l = new Lexus(); 6. System.out.print(l.speedUp(30, 40, 50)); 7. } 8. } 9. class Car { 10. private int i=0; 11.

Garbage collection - why is c3 not eligible for collection in this example (SCJP 6) [duplicate]

旧时模样 提交于 2019-12-06 06:37:01
问题 This question already has answers here : Objects eligible for garbage collection (5 answers) Closed 6 years ago . Taken from SCJP 6 prep book - Given: class CardBoard { Short story = 200; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // do Stuff } } When // doStuff is reached, how many objects are eligible for GC? A. 0 B. 1 C. 2 D. Compilation

when an object is eligible for a garbage collector?

耗尽温柔 提交于 2019-12-06 06:27:22
问题 consider this sample code: 1. public class GC { 2. private Object o; 3. private void doSomethingElse(Object obj) { o = obj; } 4. public void doSomething() { 5. Object o = new Object(); 6. doSomethingElse(o); 7. o = new Object(); 8. doSomethingElse(null); 9. o = null; 10. } 11. } When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection? A. Line 5 B. Line 6 C. Line 7 D. Line 8 E. Line 9 F. Line 10 Answer: D why D? it's

char and byte with final access modifier - java

狂风中的少年 提交于 2019-12-05 10:14:40
Please take a look at below example i cant understand the relation between char and byte byte b = 1; char c = 2; c = b; // line 1 Give me compilation Error because c is type of char and b is type of byte so casting is must in such condition but now the tweest here is when i run below code final byte b = 1; char c = 2; c = b; // line 2 line 2 compile successfully it doesn't need any casting at all so my question is why char c behave different when i use final access modifier with byte Because qualifying it with final makes the variable a constant variable, which is a constant expression . So

Assigning result of an expression to a primitive

泄露秘密 提交于 2019-12-04 15:20:22
K.Sierra in her book "SCJP Study Guide" mentions "We know that a literal integer is always an int, but more importantly, the result of an expression involving anything int-sized or smaller is always an int." I've started experimenting and I'm a little bit confused with the below results: byte a = 1; // correct byte b = 1 + a; // incorrect (needs explicit casting) byte c = 1 + 1; // correct (I expected it to be incorrect) Could anyone explain to me why the last example does not require casting? Why does the Java compiler put the implicit cast? Is it because there are 2 int literals?

when an object is eligible for a garbage collector?

試著忘記壹切 提交于 2019-12-04 10:22:30
consider this sample code: 1. public class GC { 2. private Object o; 3. private void doSomethingElse(Object obj) { o = obj; } 4. public void doSomething() { 5. Object o = new Object(); 6. doSomethingElse(o); 7. o = new Object(); 8. doSomethingElse(null); 9. o = null; 10. } 11. } When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection? A. Line 5 B. Line 6 C. Line 7 D. Line 8 E. Line 9 F. Line 10 Answer: D why D? it's true that when Line 6 is executed the object created in Line 5 is now referenced by the instance var o and

Protected member behavior once it was inherited.

痴心易碎 提交于 2019-12-04 05:46:43
I've got some doubts regarding protected identifier. In the first chapter of Sun Certified Java Programmer Study Guide by K.Sierra I found the following information: "Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass." I provided sample code which reflects the above statement and it is absolutely clear to me. // Parent class package package1; import package2.Child; public class Parent { protected int i = 5; } // Child class package

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);

Understanding join()

坚强是说给别人听的谎言 提交于 2019-12-03 17:23:39
Suppose a thread A is running. I have another thread, B , who's not. B has been started, is on runnable state. What happens if I call: B.join() ? Will it suspend the execution of A or will it wait for A's run() method to complete? brabster join() will make the currently executing thread to wait for the the thread it is called on to die. So - If A is running, and you call B.join(), A will stop executing until B ends/dies. Join waits till the thread is dead. If you call it on a dead thread, it should return immediately. Here's a demo: public class Foo extends Thread { /** * @param args */ public