scjp

Singletons, Enums and anonymous inner classes

扶醉桌前 提交于 2019-11-30 19:57:08
As you may know, some people are declaring singletons with an Enum of 1 instance, because the JVM guarantees that there will always be a single instance with no concurrency problems to handle... Thus what about an Enum with multiple instances? Can we say something like an Enum is a kind of ordered set of singletons sharing a common interface? Why? public enum EnumPriceType { WITH_TAXES { @Override public float getPrice(float input) { return input*1.20f; } public String getFormattedPrice(float input) { return input*1.20f + " €"; } }, WITHOUT_TAXES { @Override public float getPrice(float input)

Generics super vs. extends

故事扮演 提交于 2019-11-30 07:52:49
Just when I thought I finally understood generics, I came across the following example: public class Organic<E> { void react(E e) { } static void main(String[] args) { //1: Organic<? extends Organic> compound = new Aliphatic<Organic>(); //2: Organic<? super Aliphatic> compound = new Aliphatic<Organic>(); compound.react(new Organic()); compound.react(new Aliphatic()); compound.react(new Hexane()); } } class Aliphatic<F> extends Organic<F> { } class Hexane<G> extends Aliphatic<G> { } It says, if line 1 is uncommented, the following will not compile: compound.react(new Organic()); compound.react

Operator precedence in Java

大憨熊 提交于 2019-11-30 07:39:02
问题 In one example from http://leepoint.net/notes-java/data/expressions/precedence.html The following expression 1 + 2 - 3 * 4 / 5 Is evaluated as 1 + 2 - 3 * 4 / 5 = (1 + 2) - ((3 * 4) / 5) = 3 - (12/5) = 3 - 2 The result of the integer division, 12/5, is 2 . = 1 Then i saw another example from http://www.roseindia.net/java/master-java/operator-precedence.shtml The following expression 4 + 5 * 6 / 3 is evaluated as 4 + (5 * (6 / 3)) I am slightly confused as to how it is decided which will be

How does Java handle String objects in memory?

寵の児 提交于 2019-11-30 05:10:24
问题 I was asked this question: String s = "abc"; // creates one String object and one // reference variable In this simple case, "abc" will go in the pool and s will refer to it. String s = new String("abc"); // creates two objects, // and one reference variable* Based on above details how many String objects and how many reference variables were created prior to the println statement of below code? String s1 = "spring "; String s2 = s1 + "summer "; s1.concat("fall "); s2.concat(s1); s1 +=

Singletons, Enums and anonymous inner classes

社会主义新天地 提交于 2019-11-30 04:17:06
问题 As you may know, some people are declaring singletons with an Enum of 1 instance, because the JVM guarantees that there will always be a single instance with no concurrency problems to handle... Thus what about an Enum with multiple instances? Can we say something like an Enum is a kind of ordered set of singletons sharing a common interface? Why? public enum EnumPriceType { WITH_TAXES { @Override public float getPrice(float input) { return input*1.20f; } public String getFormattedPrice(float

What does a bitwise exclusive OR do in Java?

六眼飞鱼酱① 提交于 2019-11-29 13:56:12
Given: public class Spock { public static void main(String[] args) { Long tail = 2000L; Long distance = 1999L; Long story = 1000L; if ((tail > distance) ^ ((story * 2) == tail)) { System.out.print("1"); } if ((distance + 1 != tail) ^ ((story * 2) == distance)) { System.out.print("2"); } } } Why this sample code doesn't output anything? In first if you get true ^ true = false In second if you get false ^ false = false becouse ^ - is OR exclusive opeartor, it's means true ^ true = false true ^ false = true false ^ true = true false ^ false = false You are using boolean exclusive OR and this is

java garbage collection and null reference

╄→尐↘猪︶ㄣ 提交于 2019-11-29 12:27:20
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 eligible for GC too? onon15 c3 is a local handle with a null reference, it does not point (and hever has

Which run first? default values for instance variables or Super Constructors?

淺唱寂寞╮ 提交于 2019-11-29 11:28:07
According to the SCJP6 (Page 507) i found that instance variables are assigned default values before the superclass constructors complete, i tried an example in Debugg mode but i saw that the super contractor runs before instance variables get their default values, could any one explain that to me ? Example i used in case someone want to try it: package courseExercise; class test { test() { System.out.println("Super Constructor run"); } } public class Init extends test { private Integer i = 6; private int j = 8; Init(int x) { super(); System.out.println("1-arg const"); } Init() { System.out

Generics super vs. extends

情到浓时终转凉″ 提交于 2019-11-29 10:16:09
问题 Just when I thought I finally understood generics, I came across the following example: public class Organic<E> { void react(E e) { } static void main(String[] args) { //1: Organic<? extends Organic> compound = new Aliphatic<Organic>(); //2: Organic<? super Aliphatic> compound = new Aliphatic<Organic>(); compound.react(new Organic()); compound.react(new Aliphatic()); compound.react(new Hexane()); } } class Aliphatic<F> extends Organic<F> { } class Hexane<G> extends Aliphatic<G> { } It says,

Hidden fields though inheritance

[亡魂溺海] 提交于 2019-11-29 09:42:15
In the following code example: class Parent { int x =5; public Integer aMethod(){ System.out.print("Parent.aMthod "); return x; } } class Child extends Parent { int x =6; public Integer aMethod(){ System.out.print("Child.aMthod "); return x; } } class ZiggyTest2{ public static void main(String[] args){ Parent p = new Child(); Child c = new Child(); System.out.println(p.x + " " + c.x); System.out.println(p.aMethod() + " \n"); System.out.println(c.aMethod() + " \n"); } } And the output: 5 6 Child.aMthod 6 Child.aMthod 6 Why does p.aMethod() not print 6 when p.x prints 6? Thanks Edit Oops a