Boris Mechkov

Ranch Hand
+ Follow
since May 13, 2011
Boris likes ...
Netbeans IDE Tomcat Server Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
10
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Boris Mechkov

Deepa aroon wrote:Hi,
Am Deepa. I am very new to this forum. I am giving my ocpjp exam on august 1... so can anyone tell me whether serialization topic is there for exam or not?


No serialization, no wait(), notify(), notifyAll() and no Drag and Drop questions.
Good Luck!

Tere Luna wrote:Thank you Boris

Your explanation is very clear! Now I'm testing by using only primitive not wrapper, like in the code below



And I get the "Type mismatch: cannot convert from int to short" compiler error in line 2. I thought this could be implicit casting :S

Thanks for your help !
Tere


Hi Tere,

You have to remember this: Any numerical operation results in an INT! Consider this:



If you want to make this work you have two choices:

1. Add an explicit cast - byte b3 = (byte) b1 +b2;
2. Use a compound assignment - b3 += b1+b2;

Compound assignments (+=, -=, *=, /=) add an IMPLICIT cast, so you are safe!.
Hope this helps!
Boris

Rohit Sidana wrote:Hi...everyone....Today i was reading my book where i got one statement that " Static methods can't be overridden but can be redefined."

Can anyone please explain me this statement that what is the difference between redefine a code and override the code?



and the output of this code is :

a a a



The difference between redefinition and overriding is simple. When you override a method, at runtime the object determines which method will execute. Redefining a static method means that the reference variable determines which method will execute.

Here is some code that will make it easier to grasp:


In the above example class Dog has two methods do() and eat(). They are neither overridden nor redefined. Now, imagine you want the static do() method to print DOG. You would want to re-define the method (un-comment Line 1 above). Here is tricky part: Consider this...



What happens when the compiler sees Line 2 and Line 3 above? It substitutes the reference variable "a" and the reference variable "d" with the CLASS NAME! So the compiler sees Animal.do(); and prints ANIMAL and also the compiler sees Dog.do(); and prints DOG.

Let me get back to LINE 2 above. if this method was not static the object would determine the invoked method, so a.do() would have printed DOG. But with static methods the compiler substitutes the reference variable with the class name (via ClassLoader) and invokes that same CLASS-specific method.
This is why you can only redefine static methods, for example, if you wanted the static animal do() to print something else rather than "ANIMAL"
Hope this answered your question!

Ash Gill wrote:cheers Boris, got it. very informative reply, never knew about the iterator thing in PQ. thanks a lot.



No problem man!

ai dan wrote:How realistic is the oracle sample exam compared to the real thing?


It is pretty close to what you will get on the real exam.
Hope this helps!

sid noob wrote:Boris, thanks for the beautiful and lucid explanation. You actually cleared some earlier concepts too that was foggy. Great explanation, i understood multi-dimensional arrays as well as how the init block actually works and not to forget the post decrement operator [the chapter am studying].



No problem! Good luck!

sid noob wrote:class Twisty {
{ index = 1; }
int index;
public static void main(String[] args) {
new Twisty().go();
}
void go() {
int [][] dd = {{9,8,7}, {6,5,4}, {3,2,1,0}};
System.out.println(dd[index++][index++]);
}
}


Please help me to understand this program, i am unable to understand as to how the output shows as 4 without any errors on running the program.

Regards,

Sid.



Here are the explanations:
You have a class called Twisty. This class has one instance variable "index" which is initialized to ZERO. You start with the "main" method, which creates an instance (or an object) of type "Twisty" at which point the "initialization block ( { index =1 } ) assigns the value of ONE to index. Right after we are done with the object creation and initialization, we invoke method go();

Method "go()" creates a two-dimensional array (array of arrays) and initializes with the following:{{9,8,7}, {6,5,4}, {3,2,1,0}}
The first sequence of integers (9,8,7) would be index 0, the second one (6,5,4) would be at index 1 and so on.
So, consider you want to get the integer "7" from the first array (9,8,7). You would do that by getting the element at index [0][2]. The ZERO obtains the whole array at index 0 which is {9,8,7}. The TWO obtains the third element from that same array, hence we get integer of value 7.

The System.out.println(); code prints the element, but you have to be careful with the post-incrementing the index instance variable.

This gives you the answer of 4. Remember post-increment works as follows: use the variable first and then increment.

Ash Gill wrote:Hi Boris, thanks a lot for the reply. can you please explain a bit more on:

remember there is no order in the PQ



ques: priority queues are sorted and the order representes priorities of the elements. then arn't PQ ordered?

C

an you get two elements from a PriorityQueue without removing any? The answer is no. "Peek", "poll", "remove" and all other methods in the PriorityQueue API work on the element AT THE HEAD of the queue. This means you cant get to the second element (whatever that is, remember there is no order in the PQ) without removing the first one.
LinkedList has indexed access, meaning you can get a specific element at a specific index.



yes, i follow the above points but we can always traverse a PQ, without removing any element, using an Iterator and the Iterator can be used in the same way to traverse a LinkedList.
for eg:



ques: i understand that this is not same as getting to a particular element in a linkedlist using get(int index), but it is still a non-destructive traversal, isnt it?. please correct me.

thanks and regards



Sorry, i should have been more specific. Using the Iterator to traverse elements in a PQ does not guarantee order. Take a look at the PriorityQueue API

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Because the original question was whether it was easier to implement a non-destructive traversal with PQ rather than LinkedLists, the answer would have been - LinkedList since you dont have to implement an Iterator.

Ash Gill wrote:hello everyone, can anyone please clarify this:



then does Harry also implement Tommable indirectly?

thanks and regards



Absolutely not.
class Tom implements Tommable means Tom IS A Tommable.
But class Harry DOES NOT implement nor extend anything. Big difference between HAS-A and IS-A.

Ash Gill wrote:Hello guys, in one of the questions in K & B, i found this

It’s programmatically easier to perform a non-destructive traversal of a PriorityQueue
than a LinkedList. correct or incorrect?


and the answer was: is incorrect because the PriorityQueue class itself provides
no non-destructive traversal methods.


what do they mean by non-destructive traversal? if they mean traversing without removing objects then PriorityQueue does inherit peek() method which returns the highest priority element.
is the opposite of the statement correct i.e. "It’s programmatically easier to perform a non-destructive traversal of a linked list than a PriorityQueue"

many thanks and regards


Ash,
Think about it this way. Can you get two elements from a PriorityQueue without removing any? The answer is no. "Peek", "poll", "remove" and all other methods in the PriorityQueue API work on the element AT THE HEAD of the queue. This means you cant get to the second element (whatever that is, remember there is no order in the PQ) without removing the first one.
LinkedList has indexed access, meaning you can get a specific element at a specific index.

"It’s programmatically easier to perform a non-destructive traversal of a linked list than a PriorityQueue" - this statement is correct.
Hope all this answered your questions.
Regards!

Tere Luna wrote:Hi All

I'm a little confused about implicit and explicit casting in Java. I'm preparing for OCPJP, but I have the below code, and I wonder why when you perform an arithmetic operation using literals like 3/2, that can be assigned to a short type attribute, but if you try to perform the same operation but using a variable like short1/4, you get a compiler error. Also the same in the line 5, where you want to assign a byte to a short, I would assume it would compile since byte is lower precision than short, and I would think it would perform an implicit casting

1. Short short1 = 3;
2. Byte byte1 = 4;
3. short1 = 3/2;
4. Short short2 = short1 / 4;
5. short1 = byte1;

Thanks for your help
Tere



First of all, welcome to JavaRanch! Further down the road, please use the "code" tags when you are posting code, so it is easier to read. Now off to the questions...Let's consider this:



The reason you can assign a division of "3 devided by 2" is because it returns an integer of 1, which can fit in a short (integer division). By the way this code: compiles and assigns a zero to short2, so not sure why it did not compile for you.
The reason you cannot assign "byte1" to "short1" is because you cannot assign one wrapper class to another i.e. you cannot assign a Byte object to a Short reference. Why? Because the IS-A test fails i.e. Byte IS NOT a Short. You can assign an Integer to a Number reference like this:

Number n = new Integer();

But this is possible because Integer extends Number or Integer IS-A Number.
Hope this helps!

ai dan wrote:Has Serialization not been removed?


It has, so i was surprised when you mentioned this. But, serialization is not that hard to grasp so i would learn it anyway.
Good luck!

Gursewak Singh wrote:i have a code:

subClass obj=new superClass();
obj.eat();
Here eat() method is overridden in the sub class.

as we know that in case of method overriding Object type(not reference type determine) ,determine which overridden method is used at run time.so in this case this code should work according to statement.
But this show compilation error for incompatible type.
so i want to know ,why?



To make it a little easier to spot consider this code:



....and consider this code:



Explanations are in the comments. let me know if you have more questions.

Gordon Keenan wrote:Hi,
I am sitting the OCPJP exam this week and I just came across this question in Oracle's list of sample questions for the exam.



If, on line 7, t is successfully serialized and then deserialized, what is the result?

The answer they give is "0 7 0" as correct, stating correctly that static or transient variables are not serialized.

However x1 should still be 7, the same as it is set two lines above as it is static.

So the answer should be 7 7 0.

This does not inspire me with confidence for the exam

The question can be found on the link below, labeled objective 3.3

// You might need to select your region
http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=303&p_certName=SQ1Z0-851



If this is indeed the answer they give you - it is wrong. It should be 7 7 0 as you pointed out. I should not worry about it too much. Just follow the principles and you will be fine.

Sam Hazim wrote:Since the move to Oracle the exam has been shortened to 2.5 hours (down from 3) and raised the passing score somewhat. This concerns me slightly as 2.5 mins per question seems a lot tighter than I'd like (whilst I think I could complete the exam I don't think I'd have much time spare).

Seems like it's harder than ever to pass the exam, how does everyone feel about that?



No, i would not say it is harder. It has gotten a little easier, since they removed the time-consuming drag & drop questions, hence they shortened the time. There is plenty of time for doing the exam, IF and only IF, you know the insides and outsides of the Java programming language. The exam is pretty straight forward if you are prepared enough.