scjp

Java SneakyThrow of exceptions, type erasure

和自甴很熟 提交于 2019-11-27 01:44:06
Can someone explain this code? public class SneakyThrow { public static void sneakyThrow(Throwable ex) { SneakyThrow.<RuntimeException>sneakyThrowInner(ex); } private static <T extends Throwable> T sneakyThrowInner(Throwable ex) throws T { throw (T) ex; } public static void main(String[] args) { SneakyThrow.sneakyThrow(new Exception()); } } It may seems strange, but this doesn't produce a cast exception, and permits to throw a checked exception without having to declare it in the signature, or to wrap it in an unchecked exception. Notice that neither sneakyThrow(...) or the main are declaring

Slight confusion regarding overriding where variables are concerned

六月ゝ 毕业季﹏ 提交于 2019-11-26 20:31:31
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 SubCovariantTest extends CovariantTest { public B getObject() { return new B(); } } The answer is 5 , but I chose 6

Java unreachable catch block compiler error

微笑、不失礼 提交于 2019-11-26 19:01:40
Why in Java can we catch an Exception even if it is not thrown, but we can't catch it's subclass (except for "unchecked" RuntimeException s and it subclasses). Example code: class Test { public static void main(String[] args) { try { // do nothing } catch (Exception e) { // OK } try { // do nothing } catch (IOException e) { // COMPILER ERROR: Unreachable catch block for IOException. //This exception is never thrown from the try statement body } } } Any ideas? A RuntimeException could be thrown by any code. In other words, the compiler can't easily predict what kind of code can throw it. A

Objects eligible for garbage collection

非 Y 不嫁゛ 提交于 2019-11-26 16:46:59
问题 This question was taken from Kathy Sierra SCJP 1.6. How many objects are eligible for garbage collections? According to Kathy Sierra's answer, it is C . That means two objects are eligible for garbage collection. I have given the explanation of the answer. But why is c3 not eligible for garbage collection (GC)? 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

How many String objects will be created

混江龙づ霸主 提交于 2019-11-26 16:42:25
I have the following Java code: public String makinStrings() { String s = "Fred"; s = s + "47"; s = s.substring(2, 5); s = s.toUpperCase(); return s.toString(); } The question is somehow simple: how many String objects will be created when this method is invoked? At the beginning I answered that 5 String objects are created, but the answer from my book says that only 3 objects are created and no explanation was given (this is a SCJP question). From my point of view there are 5 objects: "Fred", "47", "Fred47", "ed4", "ED4". I also found this question on a SCJP simulation exam, with the same

ClassCastException because of classloaders?

依然范特西╮ 提交于 2019-11-26 14:23:55
问题 While playing with classloaders i got the following exception: Exception in thread "main" java.lang.ClassCastException: xxx.Singleton cannot be cast to xxx.Singleton Does this mean that an instance from a classloader is not castable to an class of another classloader? Check my code where i'm able to instanciate 3 singletons thanks to classloaders, even with the "" security. public static void main(String[] args) throws Exception { URL basePath = new URL("file:/myMavenPath/target/classes/");

What are “connecting characters” in Java identifiers?

拈花ヽ惹草 提交于 2019-11-26 14:03:09
I am reading for SCJP and I have a question regarding this line: Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! It states that a valid identifier name can start with a connecting character such as underscore. I thought underscores were the only valid option? What other connecting characters are there? Peter Lawrey Here is a list of connecting characters. These are characters used to connect words. http://www.fileformat.info/info/unicode/category/Pc/list.htm U+005F _ LOW LINE U+203F

Bitwise shift operators. Signed and unsigned

╄→尐↘猪︶ㄣ 提交于 2019-11-26 13:58:51
问题 I'm practising for the SCJP exam using cram notes from the Internet. According to my notes the >> operator is supposed to be signed right shift, with the sign bit being brought in from the left. While the left shift operator << is supposed to preserve the sign bit. Playing around however, I'm able to shift the sign with the << operator (f.e. Integer.MAX_VALUE << 1 evaluates to -2 , while I'm never able to shift the sign with the >> operator. I must be misunderstanding something here, but what

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

白昼怎懂夜的黑 提交于 2019-11-26 11:50:24
public class Java{ public static void main(String[] args){ final byte x = 1; final byte y = 2; byte z = x + y;//ok System.out.println(z); byte a = 1; byte b = 2; byte c = a + b; //Compiler error System.out.println(c); } } If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte. Why does it happen when we add two final bytes that fit in a byte? There is no compiler error. From the JLS 5.2 Assignment Conversion In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int : - A

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

﹥>﹥吖頭↗ 提交于 2019-11-26 11:43:58
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? Adrian Mitev NaN means "Not a Number". Java Language Specification (JLS) Third Edition says : An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no