Hi Ranchers, Does serialization work with inner classes? Meaning if I have a class say Outer and there is an inner class called Inner can then we use the writeObject and readObject methods on the outer class to serialize it? Or should we be implementing the serializable interface in the inner class as well? I can post the sample code if anyone needs to have a peek at it
To be honest I am just getting the 50s. Although I am trying hard to cross the 80s. THis is not my first certification I have answered other certifications before but this seems to be a little too intimidating
This is one question that has baffled me. As i read through the topics on thread I came to understand that join() method to some extent can be used to sequence the order in which the threads are executing Now I have one question: In all the code examples provided uptill now the thread spawning another thread calls the join method on the spawned thread. By this i mean If thread A creates thread B then A calls b.wait() where b is an object of type thread B. So as of now we have join() helping us to allow the parent wait for the child. But what if I have two peer threads let's say C and D both spawned by the main thread; but I want thread D to execute only after thread C executes. How do i accomplish that. Please help me out here. I have written a sample code for the same but i dont seem to get that right. Please help me out. I can post the code if needed
Hi Ranchers, I have a question that I am sure many of the others here wanting to take the exam or are performing last minute preparations for the same have.
The question is: Is there a definite sequence of steps that you can take while answering a question in the exam. I am practising here for the exam which i am planning to answer in another two weeks and I find that i get overwhelmed with the syntax details and the logic. Hence in the process of analyzing the code; i loose out on the catch that is present in it. This is making me loose my confidence. Request all the certified ranchers to come to my rescue here
Hi John, Yes you are right. It does print different values and here's is the reason why. First some stuff to remember: Java uses something called as the string pool to manage string objects ( you dont need to know the details abt the string pool for the SCJP. but you should know that there is something calledas the string pool). Whatever string objects are created are stoed in the string pool so that the string can be reused For ex suppose you say String str="Hello". The first time the program runs the str object is placed in the string pool Now next time say you create another object called str1 as follows: String str1="Hello"; What happens at this point is that the Java compiler knows that the string object with the value "Hello" is already present in the string pool. So instead of creating a new String object what the compiler does to optimize memory usage is to make str1 also point to the same string object as str.
Hence the equality test for the string objects str and str1 i.e (str==str1) returns true as the two references are the same.
Now when a method returns a String object it creates a new String object hence if we were to declare another string object say str2 as follows String str2=getString(); where let's assume that getString returns "Hello"; here when the method returns a string a new string object is created in the string pool and the reference variale str2 points to that Hence the test str==str2 fails as the two objects refer to different objects on the pool albeit the objects they are referring to have the same value "Hello". Hope this explaination solves your confusion
Thank you all especially Henry and Marc for your guidance. It now makes abolute sense. Just correct me if what I have got is wrong:
The JLS states that in case of the Boolean equality operators the values are first unboxed and then their values are compared. Hence Boolean true == boolean true yields true. In the same way when we have an Integer object and an int what happens is since int comes in the list of prmitives for which the values are compared only after unboxing; an Integer object with value 10 is the same as an int with an object 10
The above conclusion for Integer objects is based ont the following piece of code that I wrote to test it out public class testintobjects { public static void main(String[] args) { Integer intObject=10; int intPrimitive=10; if(intObject == intPrimitive) System.out.println("The Integer object is equal to the int primitive"); else System.out.println("The Integer object is not equal to the int primitive"); } }
It gives me the follwoing output The Integer object is equal to the int primitive Press any key to continue...
Well here is some code that I have again from a mock exam site public class Boxing6 { public static void main(String[] args) { Boolean b1 = new Boolean(true); Boolean b2 = new Boolean(true); boolean b3 = true; Boolean b4 = true; System.out.println(b1==b2); System.out.println(b1==b3); System.out.println(b3 == b4); System.out.println(b1 == b4); } } What is the output for the above program? 1)false true true false 2)false false false false 3)true true true true 4)false false true true
My answer for this question was 2) but I WAS Wrong!! Well the answer turns out tobe 1) I tried the code out on my compiler and indeed the answer was one! Can someone explain to me how the == operator treats b1( a Boolean object) and b3 (a boolean primitive) as equal?
Here is a small piece of code I got from another mock exam site: public class doublemain { int x;
public static void main(Integer args[]) { System.out.println("in main method with int args"); }
public static void main(String args[]) { System.out.println("In main method with string args"); } }
It was asked whether the code will compile and if yes then which version of main() will run. The answer to this is: Yes the code compiles and the method of main that will run is the main() function taking the string args irrespective of the order in which they are written. I am a newbie to Java can someone explain then why does Java allow for overloading of the main method if it knows that it will always run the String version (or so I suppose ?)