scjp

Java char to byte casting

大兔子大兔子 提交于 2019-11-28 02:28:04
问题 I have been testing the char casting and I went through this: public class Test { public static void main(String a[]) { final byte b1 = 1; byte b2 = 1; char c = 2; c = b1; // 1- Working fine c = b2; // 2 -Compilation error } } Can anyone explain why it's working fine in 1 when I added a final to the byte? 回答1: When the variable is final , the compiler automatically inlines its value which is 1. This value is representable as a char , i.e.: c = b1; is equivalent to c = 1; In fact, according to

Identifer versus keyword

风格不统一 提交于 2019-11-27 22:23:23
问题 I read in the book for OCJP for Java6 the part with assertions. I reached the part where it gives me an overview of how the compiler reacts if the word 'assert' is used as keyword or as an identifier. What is the difference between a Keyword and an identifier ? Can anyone give me a simple explanation and additionally one or more examples for both? 回答1: The terms "keyword" and "identifier" are not Java specific. A keyword is a reserved word from the Java keyword list provide the compiler with

SCJP: can't widen and then box, but you can box and then widen

扶醉桌前 提交于 2019-11-27 18:42:24
问题 I'm studying for the SCJP exam and I ran into an issue I can't really wrap my head around. The book says you can't widen and then box, but you can box and then widen. The example for not being able to box is a method expecting a Long and the method being invoked with a byte. Their explanation is: Think about it…if it tried to box first, the byte would have been converted to a Byte. Now we're back to trying to widen a Byte to a Long, and of course, the IS-A test fails. But that sounds like box

Formatting using printf and format

元气小坏坏 提交于 2019-11-27 17:07:58
问题 In the following program class ZiggyTest2 { public static void main(String[] args){ double x = 123.456; char c = 65; int i = 65; System.out.printf("%s",x); System.out.printf("%b",x); System.out.printf("%c",c); System.out.printf("%5.0f",x); System.out.printf("%d",i); } } The output is 123.456trueA 12365 Can someone please explain how a double value (i.e. 123.456 ) is converted to a boolean (ie. true ) The reason I ask is because I know java does not allow numbers to be used for boolean values.

Objects eligible for garbage collection

亡梦爱人 提交于 2019-11-27 14:31:07
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 CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // Do stuff } } When // Do stuff is reached, how many

ClassCastException because of classloaders?

只谈情不闲聊 提交于 2019-11-27 08:57:38
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/"); Object instance = getClassInstance(Singleton.class); System.out.println(instance); // Object instance2 =

Bitwise shift operators. Signed and unsigned

血红的双手。 提交于 2019-11-27 08:03:58
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? ">>" is signed because it keeps the sign. It uses the most left digit in binary representation of a

why this synchronized method is not working as expected?

那年仲夏 提交于 2019-11-27 07:12:23
问题 Could someone explain two me why these to codes dont output the same results (the only difference between two codes is in the run() method) ? NB: the first code seems not doing any lock! First Code: class LetterThread extends Thread { private StringBuffer letter; public static void main(String[] args) { StringBuffer sbltr = new StringBuffer("A"); LetterThread one = new LetterThread(sbltr); LetterThread two = new LetterThread(sbltr); LetterThread three = new LetterThread(sbltr); one.setName(

Why do we use final keyword with anonymous inner classes?

走远了吗. 提交于 2019-11-27 02:06:26
问题 I'm currently preparing the S(O)CJP, with the Sierra & Bates book. About inner classes (method local or anonymous), they say that we can't access the local variables because they live on the stack while the class lives on the heap and could get returned by the method and then try to have access to these variables that are on the stack but do not exist anymore since the method has ended... As we all know, we can bypass this by using the final keyword. This is what they say in the book but they

How to call a specific parent constructor from anonymous inner class?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 02:05:00
问题 Ok, So I know that an anonymous inner class is either implicitly extending a parent class or implementing an interface, and therefore a constructor of the superclass will need to be called. However, I'm not sure how to create a constructor for the anonymous class (if this is possible) and without defining a constructor I'm not sure how to make calls to super()! Here is my practice code: public class AnonymousConstructor { public static void main(String[] args) { //I'm not sure how to