Justin Fleming

+ Follow
since Dec 30, 2009
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Justin Fleming

Vinay Dinakar wrote:what i can see from this example

is that, line1 creates one object in heap ("abc") and in line 2, "def" goes to string constant pool and sb (line 1 's object) value gets modified. so total 2 objects. line 3 does not have any effect.

My understanding is that "abc" creates a String Literal (Obj1) , and it's value is transfered to the StringBuffer (object 2) , "def" creates a 3rd object (another String Literal)(Obj3), but is appended to the sb object rather than creating a new "abcdef" String. Line 3 does not create a new object.

So thats 3 objects. If the code was

Then we would have 4 String objects. The String sb is a String reference (OBJ1), "abc" is a literal that is copied to the reference(OBJ2). When we create a 3rd string "def"(OBJ3) and a 4th string "abcdef"(OBJ4) which is assigned to the String Reference variable sb. In this example you would have 4 object and 3 String Literals in the String Constant Pool.

Someone feel free to jump in if I am not correct.

Vinay Dinakar wrote:Arent these string literal also objects ? then how 3 objects gets created, it should be only one right. if it is 3 objects, then whats the difference between string and stringbuffer !!! ?

If you were using String instead of String Buffer then even more objects would be created. This isn't the best example of a StringBuffer or evena StringBuilder in use though. Repeat the append about 20 times and it becomes more apparent.

One thing I wonder though, when you append a non string literal to a StringBuffer / Builder and it appends the string representation, does it still create a String literal that gets added to the pool? I would imagine it would but I don't know the answer.
Ok, not sure if this qualifies. Table 7-5 states that the * allows you to specify if the endpoints in a subset are exclusive, while they actually let you specify whether the endpoints are inclusive. I mean, in a way it lets you specify if they are exclusive, because thats another way to say the same thing by passing the opposite boolean value in.

Stating this the way it is in the book (that your specifying whether or not the endpoints are exclusive) I would expect that if a boolean value true is passed the endpoint should be exclusive, but instead they are inclusive. This was identified in test problem 11 at the end of chapter where I got the incorrect answer because of the confusion since I study from these tables. But now that I made this post I'm sure I will remember it correctly.
haha, rub it in. I would add it to my resume even if I just barley passed. certified is better than not certified IMO. Of course I'm also wanting to get in the 90% when I take mine. Just be ready to answer tricky java questions at interviews.

curve karve wrote:

hey i understood....its like when Base Class constructor is called..it gives a call to derived class constructor ...and again when derived calss constructor is called the same method is executed so..values when methods are not static will be 20+20=40;
when static methods are present since static methods are not overriden..when Base constructor is called it calls method from its class itself which prints value=10 first ..when Derived constructor is called static method of this class is executed ..then value becomes 10+20=30...
m i right???

yes, if the methods were not static the answer would be 40.
When new Derived1() is called we enter the constructor. The Superclass Constructor is called first because the call to it is implicit. The value of the static variable is now 10, then the super constructor returns, and the derived classes constructor finishes running incrementing the static variable by 20 to give you 30.

A harder question would be what is the total if the add value methods are NOT static. ;)
Grats! It's good to hear that it was easier than you expected. I'm still too scared to take the exam. It took me 8 months just to finish reading the book. I'm pretty sure I have forgotten too much since the I first started. I don't know where everyone finds the time to study so much. There are so many tables to memorize!
13 years ago
I'm having trouble with this one too.

The other thread linked above says "GetJar refers to "Foo.d" but does not either import myApp.* or refer to Foo.d by its fully qualified name. There is no way to get this code to compile." That is not the case in my book, or in the OP's (Original Poster). My book also has the following which uses the fully qualified name.

Furthermore, option "A" compiles the code fine on my machine, so long as the directory structure is preserved in the Jar file.

The jar can be created with after compiling the the java file using
javac -classpath MyJar.jar GetJar.java
jar -cf myJar.jar myApp/Foo.class

However my issue is that the explanation for option C seems incorrect.

1st) there is no "MyJar.java" in option C. It should be "MyJar.jar"
2nd) even if it was written correctly I would take this to mean

java -classpath test/Myjar.jar GetJar

which is not the reason it would not work.

3rd) I think what it is trying to say is that the Jar file (MyJar.jar) does not include the GetJar.class file (which is in the test directory).

A jar that contains this file can be created using the following command. This will allow option C to run and be correct as well. But it was not specifically said that these files were or were not in the Jar file.

jar -cf MyJar.jar GetJar.class myApp/Foo.class


jar -cf MyJar.jar *

So really C could also be correct, unless we assume that the only class in the Jar is myApp/Foo.class
perhaps giving the jar file creation command (and location executed) in place of the statement that "myApp/Foo.class is placed in a JAR file called MyJar.jar" would be much more clear.