Self Test Java -2

December 9, 2009 Leave a comment

In the same pattern as of last post, I have a new question. Just the difference this time is the question was asked by my brother.

    Ques:Why is clone method declared protected in Java (object class)

I thought hard and thought process led me to the basic difference between public and protected method. For One. And for second thing, I thought deeply about the Clonable interface may be for the first time. Obviously if a method is defined protected, it is supposed to be used in the class hierarchy only. It can not be called from outside.
1.But why it is kept protected?
2.And doesn’t Object class provide any implementation of clone(). Does it return null? Does nothing? Throw an exception?
3. What’s the use of Clonebale interface?

Answers in that order:
1. It’s kept protected because of the reason so that in your class hierarchy you can provide “deep copy” or “shallow copy” and you have to take a stand.
Let’s say with example ( Reason)
2. for second take straight from Sun javadoc:
“The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned.”
3.Brings us back to the debate of marker interface. Leave that. Its more debatable than that because of the way clone() has been defined in Object class. Because of its access modifier. This is what I have to say (echo-ing somebody else views):
“You can’t clone anything that doesn’t implement the Cloneable interface, but just because something implements the Cloneable interface doesn’t mean you can clone it”

Comments (in fact calrity) invited.

Self Test Java -1

December 9, 2009 Leave a comment

Yesterday, one of my colleague casually mentioned something while having lunch together. This is what he said “I am having a class in jar” On which I can do

Class.foName("fully qualified class name");

But, I am not able to do

clazz.newInstance();

The question stuck in my mind. But instead of going ususal root of opening a google box and tamly looking for answer I thought to self test me too. It should be easy pick and I got it bang on target except for the Exception names and exact jargon of the words. One should not be so hard on himself.

Quotuing from javadoc

Returns:
a newly allocated instance of the class represented by this object.
Throws:
IllegalAccessException - if the class or its nullary constructor is not accessible.
InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
ExceptionInInitializerError - if the initialization provoked by this method fails.
SecurityException - if there is no permission to create a new instance.

 

Now this gonna be my new questions in the interviews I take. M gonna have some fun.

Categories: Java Tags:

DataStructure quick pick

December 9, 2009 Leave a comment

Got this in the mail from my brother. Feel its handy. It might be from some source, which I don’t know, hence unable to quote otherwise would have.
Nonethelss worth saving in this post too.

  • Data Structure: Array
    Advantages: Quick Insertion, Very fast access if index known
    Disadvantage: Slow search, slow deletion, fised size
  • Data Structure: Ordered array
    Advantages: Quicker search than unsorted array
    Disadvantage: Slow insertion and deletion, fixed size
  • Data Structure: Stack
    Advantages: Provides LIFO
    Disadvantage: Slow access to other items
  • Data Structure: Queue
    Advantages: Provides FIFO
    Disadvantage: Slow access to other items
  • Data Structure: Linked List
    Advantage: Quick insertion, quick deletion
    Disadvantage: Slow search
  • Data Structure: Binary Tree
    Advantage: Quick Search, insertion, deletion (if tree remains balanced)
    Disadvantage: Deletion Algorithm is complex
  • Data Structure: Red-Black tree
    Advantage: Quick search, insertion, deletion. Tree always balanced
    Disadvantage: Complex
  • Data Structure: 2-3-4 tree
    Advantage: Quick search, insertion, deletion. Tree always balanced. Similer trees good for disk storage.
    Disadvantage: Complex
  • Data Structure: Hash table
    Advantage: Very fast access if key known. Fast insertion.
    Disadvantage: Slow deletion, access slow if key not known, inefficient memory usage.
  • Data Structure: Heap
    Advantage: Fast insertion, deletion, access to largest item.
    Disadvantage: Slow access to other items.
  • Data Structure: Graph
    Advantage: Model real world situation
    Disadvantage: Some algorithms are slow and complex.
  • SQL SQL everywhere

    December 8, 2009 Leave a comment

    this link was working yesterday, today thought of sending it to my bro and found this ;-

    http://dvdstore.erosentertainment.com/product/specialoffer.asp


    Microsoft OLE DB Provider for SQL Server error '80040e14'

    Incorrect syntax near the keyword 'and'.

    /product/specialoffer.asp, line 20

    See the snapshot attached.
    Should happen on prod at least?
    Some observations:
    1. SQL does have error name? Of what use man? Is there any consensus among different RDBMS.
    2. It should be a simple testing, I guess.

    Guys, share your WTF …. preferably from Indian sites.

    Categories: Uncategorized

    First tryst with .Net

    December 7, 2009 Leave a comment

    Disclaimer in the beginning itself : I am a java professional basically and Java J2EE and RDBMS is my bread and butter.

    Still, in the due course of time and out of passion for different programming languages, to gain versatility, and because of mood swings I get rather frequently than rarely, I have a fair bit of flair for PHP, Ruby, Python et al…

    C and C++ was my introduction to programming language world, some 10 years back, but never got chance to work extensively on those. Had C# basic knowledge as such, but nothing concrete again. That sums up .Net.

    But recently got into the project which has its backend in Java and client written in C#. Want to take full use of technologies.

    Intsalled .Net framework 1.0/2.0 and 3.5 which comes bundled with VS2008. First thing first, being a Eclipse/Idea guy all along, spent my hours making VS too similar to Eclipse for me. Second, I always start a project with “debug”. Now I am not aware of the theories regarding whether its right or wrong to do so, but yet this is my modus operandi, so I set sail on my debug journey.

    Got my debug working. Found out two-three things in first hour, these are:

    1. Just like jars, here the things resolve around dlls.

    2. the interface to include those dependencies though are pretty bad.

    3. VS is much much slow than Eclipse in my humble opinion. You can say I knew it by birth, but now I can definitely write so too.

    4. Strange things happen in .Net world (Pardon me, I have heard that too many times that it has definitely affected my viewpoint). My project was supposed to work from the word “go”, but it didn’t. Got some System.DLLexception: and it was not able to load some DLL we are creating in the project through NAnt.  (It was my first experience with NAnt too, something, a very little tiny thing about that in some other post too.) so, DLL was there. But why the f***, it was not able to load then. Got to know it might be because of some other DLLs which this DLL is dependent on. Fine. I thought I will check that. got to know about dependecnychecker, run it and to my surprise and irritation found that whatever dlls it require are present in my system, their path is already included. What the hack! Then, just it happens, sometimes some idea strikes by lightning, I thought to give VS a restart and viola! (Questions for experts here first: I have seen these kind of things sometime happening with Eclipse too, not with jars, is it VS issue or some crap of .Net?)

    5. Shortcuts with VS are so bad, I say.

    6. Good learning,  and my learning starts with the DLLimport sort of keywords, already a stuff of intermediate level I guess(other experts can confirm or take me out of my fairy tales)

    7. Did learn about setting arguments, setting output directories, Assembly Infos in the first go. first two much like Java, not a big deal!

    Hope I will get more insight with already running (although crappy)  code at my disposal.

    One last question: What’s the best framework to embed MVC in window Form applications?

    And what the hack is Resharper?

    Categories: .Net, C#, VS2008, WinForm

    rowcount and variable value on unsuccessful select.

    November 25, 2009 Leave a comment

    Learn a new thing today, as always. That’s a classical hierarchical tree problem reprsented in relational database.

    A table having nodeId and parentNodeId and thus the tree is set up. How do you get the org tree startting from anode going up till the root node level? A stored proc.

    But two problems in the implementation.

    1. If you want base your loop termination logic in a PL/SQL stored proc on the basis of rowcount on the select statement having where clause.  Then it doesn’t work. It is not considered unsuccessful, hence doesn’t result ZERO as rowcount as per PL/SQL rule.

    2. If you want to have your loop termination logic on the basis of variable’s value obtained from the select. That too doesnt gonna work as it doesnt update the value with null on the basis of no row returend.

    THE SOLUTION: have a prevParentNodeId and parentNodeId. Andrest you know. Check for the inequality/equality of those.

    Categories: PL/SQL, RDBMS, Stored Procedure

    Javadoc says about registering a SASL Mechanism ….

    November 25, 2009 Leave a comment

    So, what you do if that’s not working? That’s it. All the tutorials say on the vast internet.

    You register all the possible SASLMechanism those are provided by the api/javadoc. Still, its throwing the SASLauthentication failed exception saying authentication failed by “PLAIN” mechanism.

    So, what you do?

    Easy. You go and un-register that mechanism. Think beyond tutorials and javadoc. Think common sense.

     

    Categories: Java, smack

    Overloaded method invocation in java reflection api

    November 24, 2009 Leave a comment

    Bad 3 hours at work.

    Was trying to invoke some public function on a class through reflection. Now, just to make that interesting, the method in limelight is overloaded (some 8 times). So poor soul me was thinking that this part needs to be taken care of in Method.invoke(Object arg, Object… args) only.

    xxxMethod.invoke(class, array, arg2);

    Got bit less informative exception too. IllegalArgumentException: wrong number of arguments

    But at invoke line.

    Up above in the code, I was creating that method something like

    Method xxxMethod = Class.gethMethod(“xxx”, array.getClass());

    Now, shouldn’t the error somehow point to above line also instead of invoke.

    Or I guess, it shouldn’t.  Pretty lame of me, but wasted two precious hours.

     

     

    Categories: Java, Reflection
    Follow

    Get every new post delivered to your Inbox.