scjp

Literal string creation vs String object creation

浪尽此生 提交于 2021-02-04 13:36:22
问题 How many String object are created I am studying for the SCJP I cant seem to get my head round this String problem. I seem to see several possible answers depending on how i look at a question. In the following initialization, how many string objects are created? String s1 = "A" + "B" + "C" + "D"; System.out.println(s1) Initially i thought 5 objects, i.e. "A" "B" "C" "D" "ABCD" But then thinking about it i am not really sure because for example will the compiler concatenate "A" + "B" as one

What's this generics usage in Java? X.<Y>method()

蓝咒 提交于 2020-01-22 10:37:08
问题 I've read the whole SCJP6 book Sierra and Bates book, scored 88% the exam. But still, i never heard of how this kind of code works as it's not explained in the generics chapter: Collections.<TimeUnit>reverseOrder() What is this kind of generics usage? I discovered it in some code but never read anything about it. It seems to me it permits to give some help to type inference. I've tried to search about that but it's not so easy to find (and it's not even in the SCJP book/exam!) So can someone

char and byte with final access modifier - java

戏子无情 提交于 2020-01-02 04:36:07
问题 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 回答1:

Java Method Overloading with Boxing/Widening

ε祈祈猫儿з 提交于 2019-12-31 04:54:12
问题 I am working on Java Se 7 OCA and could not figure out why below code does not compile. aMethod call in main method gives compile error stating ambiguous method. Precedence rules between widening and boxing seems to clash in this overloading method sample. public class Overloading { public static void main(String[] args) { Byte i = 5; byte k = 5; aMethod(i, k); } static void aMethod(byte i, Byte k) { System.out.println("Inside 1"); } static void aMethod(byte i, int k) { System.out.println(

java garbage collection and null reference

半世苍凉 提交于 2019-12-29 08:18:08
问题 In my studying for OCJP I came across the following question: 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? The correct answer is 2, meaning c1 and its story object. When line //doStuff is reached, c3 is also null. Why isn't it

Java - When is it a compiler error and when is it a runtime exception?

风流意气都作罢 提交于 2019-12-28 18:44:40
问题 I am currently studying for the SCJP certification using the Sierra and Bates Study Guide and in many of the self tests (mock exam questions) I keep running into the same problem - I can't tell whether a particular error will be at runtime (an exception) or at compile (compile error). I know this is a bit of a vague question and that it might not be possible to answer but, how can I tell if an error will be found at compile or at runtime? Would you be able to send me some website links that

Why can't we pass a instance variable to the super class constructor? [duplicate]

百般思念 提交于 2019-12-24 09:39:37
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Cannot refer to a instance method while explicitly invoking a constructor I have been trying to do this for long time. public class bb extends test { int t = 23; public bb() { super(t); //**This is the place that error comes** // TODO Auto-generated constructor stub } public bb(int v) { } } public class test { public test() { // TODO Auto-generated constructor stub } public test(int v) { // TODO Auto-generated

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

What benefit do method-local inner classes provide in Java?

不羁的心 提交于 2019-12-22 04:22:34
问题 I've just read through the chapter on method-local inner classes in the SCJP book, and I'm really struggling to think of any practical use for them. I've always been under the impression, that methods should be as small and specific to their task as possible (Orthogonality IIRC), so introducing even the simplest inner class would create heft and unwieldy methods. Can anyone suggest a good practical usage for method local inner classes? So far it feels as if I might have to understand them

Assigning result of an expression to a primitive

混江龙づ霸主 提交于 2019-12-21 21:27:26
问题 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