scjp

Getting confused with == and = in “if” statement

空扰寡人 提交于 2019-11-29 09:26:32
I know that we cant use assignment operator in if statements in java as we use in any other few languages. that is int a; if(a = 1) { } will give a compilation error. but the following code works fine, how? boolean b; if(b = true) { } EDIT : Is this the exception to rule that assignment cant be used in if statement. Because the "result" of an assignment is the value assigned... so it's still a boolean expression in the second case. if expressions require the condition to be a boolean expression, which is satisfied by the second but not the first. Effectively, your two snippets are: int a; a =

Java char to byte casting

时间秒杀一切 提交于 2019-11-29 09:06:11
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? 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 this section on final variables , b1 is treated as a constant: A variable of primitive type or type String

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

走远了吗. 提交于 2019-11-29 04:48:32
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 and then widen and not widen and then box to me. Could anyone clarify the whole box and widen vs widen

Formatting using printf and format

青春壹個敷衍的年華 提交于 2019-11-29 02:50:49
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. For example, the following is not allowed in Java if (5) { //do something } Thanks for "%b" : If the

why this synchronized method is not working as expected?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 12:44:30
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("Thread ONE"); two.setName("Thread TWO"); three.setName("Thread THREE"); one.start(); two.start(); three

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

青春壹個敷衍的年華 提交于 2019-11-28 11:35:53
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 might be able to help me? Andreas_D Compile time error - the java compiler can't compile the code, often

Why do we use final keyword with anonymous inner classes?

巧了我就是萌 提交于 2019-11-28 08:59:36
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 don't really explain what's the effect of that final keyword... As far as i know, using the final

Priority queue ordering of elements

最后都变了- 提交于 2019-11-28 08:54:57
问题 How come the elements of priority queue are ordered according to natural order by default as it doesn't implement comparable interface. From the docs, it says elements are ordered based on natural ordering but I can't find anywhere it talks about equals method nor comparable. Hows it happening internally? All Implemented Interfaces: Serializable, Iterable, Collection, Queue. If it implements comparable then why doesn't it say in the above line Example: public static void main(String[] args) {

What does a bitwise exclusive OR do in Java?

≯℡__Kan透↙ 提交于 2019-11-28 08:14:17
问题 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? 回答1: 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

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

折月煮酒 提交于 2019-11-28 08:08:36
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 explicitly call one of the arg super constructors MyBob my = new MyBob() { //I would like to do something like