Wednesday, August 6, 2014

Effective Java, Chapter-2 Creating and Destroying Objects: Summary

Item 1 (Consider Static factory methods instead of constructors)

  • Static factory methods have names unlike constructors.
  • Unlike constructors they are not required to create a new object every time they are invoked.
  • Unlike constructors they can return an object of any sub type of their return type.
  • Immutable class:-Immutable class is the one whose object can't be modified once created.
  • Any modification in the object results in new immutable object.
  • Immutable objects are thread safe.i.e can be shared without synchronization in concurrent environment.
Service provider framework  of JDBC
  1. service interface:-It provides implement.  ex:Connection
  2. provider registration API:-system uses it to register implementation.   ex.:DriverManager.registerDriver()
  3. service access API:-client use to obtain an instance of the service.  ex.:DriverManager.getConnection()
  4. service provider interface:-providers implement it to create instances of their service implementation.  
ex.:Driver
  • The main disadvantage of providing only static factory methods is that classes without public or protected
  • constructors cannot be sub-classed.
  • For example, it is impossible to subclass any of the convenience implementation classes in the Collections Framework.

Item 2 (Consider builder when faced with many constructor parameters)


  • traditional programmers have used telescoping constructor pattern which does not scale well.
  • A second alternative when you are faced with many constructor parameters is the Java-beans pattern, in which you call a parameter less constructor to create the object and then call setter methods to set each required parameter and each optional parameter of interest
  • A Java-Bean may be in an inconsistent state partway through its construction.
  • build patters are more safer than java beans.
  • Inner class in java are associated with an outer class while nested static class is not associated with any outer class object, so it's instance can be created independently.

Item 3 (Enforce the singleton property with a private constructor or an enum type)


  • Singleton class can be instantiated exactly once. Implemented by making the constructor private.
  • In serialization aspect, to maintain the singleton property of a class you have to declare all instance field transient and provide a readResolve() method. Otherwise, each time a serialized object is deserialized a new instance will be created.
  • A single element enum type is the best way to implement a singleton.

Item 4 (Enforce noninstantiability with a private constructor)


  • A class having private constructor can't be extended or subclassed.
  • Attempting to enforce non-instantiability by making a class absract does not work.

Item 5 (Avoid creating unnecessary objects)


  • String s = new String("Ashish"); //this is not desirable
  • String s = "Ashish"; //this should be adopted instead
  • It is possible to avoid unnecessary initialization by lazy initialization.

Item 6 (Eliminate obsolete object references)


  • If a stack grows and shrinks the popped off objects will not be garbage collected even if stack has no references to it, this is because a stack maintains obsolete references to these objects.
  • An obsolete reference is simply a reference that will never be dereferenced again.
  • Memory leaks in garbage collected languages are harmful. If an object reference is unintentionally retained, not only is that object excluded from garbage collection, but so too are any objects referenced by that object. So, even if few objects references are unintentionally retained, many, many objects references may be prevented from garbage collection , with potentially large impact on performance.
  • Null out the references once they become obsolete.
  • Whenever a class manages its own memory, the programmer should be aware of memory leaks. 
  • Whenever an element is freed, any object reference contained in that element should be manually nulled out.
  • Another sources of memory leaks are caches and callbacks and listeners.

Item 7 (Aviod Finalizers)


  • Finalizers are often unpredictable, often dangerous, and generally unnecessary.
  • You should not do anything time critical in finalizers because it can arbitrarily takes long between the time that an object becomes unreachable and the time that its finalizer is executed.
  • Creating and Destroying an object in Finalizer severely slows down the perfomances.
  • So, instead of finalizers you should provide explicit termination method.
  • Typical example is close method on InputStream, OutputStream, and java.sql.Connection. Another is cancel method on java.util.Timer.
  • Explicit termination methods are typically used in combination with the try-finally construct to ensure termination.
  • Finalizers can act as a 'safety net' in case the owner of an object fails to call its explicit termination method.
  • A second legitimate use of finalizers concerns objects with native peers.

Tuesday, July 22, 2014

Can you dare to answer these questions?

If swimming is a good exercise to stay FIT,
Why are whales FAT ??


Why is the place in a stadium where people SIT,
called a STAND ?


Why is that everyone wants to go to HEAVEN,
but nobody wants to DIE..


Shall I say that there is racial discrimination even in chess...
As the WHITE piece is moved FIRST...


In our country,
We have FREEDOM of SPEECH,
Then why do we have TELEPHONE BILLS ?


If money doesn't grow on TREES,
then why do banks have BRANCHES ?


Why doesn't GLUE
stick to its BOTTLE ?


Why do you still call it a BUILDING,
when its already BUILT ?


If its true that we are here to HELP others,
What are others HERE for ?
 

If you aren't supposed to DRINK and DRIVE...
Why do bars have PARKING lots ?


If All The Nations In The World Are In Debt,
Where Did All The Money Go..?


When Dog Food Is New With Improved Taste,
Who Tests It..?


If The "Black Box" Flight Recorder Is Never Damaged During A Plane Crash,
Why Isn't The Whole Airplane Made Out Of That Stuff..?


Who Copyrighted
The Copyright Symbol..?


Can You Cry Under Water.?


Why Do People Say "You've Been Working Like A Dog",
When Dogs Just Sit Around All Day..??


We all are Living in a seriously funny world....
So Enjoy 

Wednesday, July 16, 2014

Google and Udacity introducing a course in Android Development

Here is an influencing program for those who aspire about developing android apps. This is an opportunity to learn about from basic, for instance, application layer, application framework, C/C++ libs, Android runtime, Linux kernel. The name of the course is Developing Android Apps: Android Fundamentals.

The course will be demonstrated by Google developers namely, Reto Meier, Dan Galpin and Katherine Kuan. Udacity is an educational organization that offers massive open online courses. By the end of the course you will have created your first real android app.

So undoubtedly, by this step Google wants more developers for its platform. Android is so far prevailing in more than 75 percentage of the smartphones. For more information about this course get in touch with https://www.udacity.com/lp/google.

You can watch the trailer of the course below

















Monday, July 14, 2014

What are static factory methods in java

In java we often have to deal with creating and destroying the objects. Public constructors have been widely used for creating an instance of the class. But for writing programs that are clear, correct, usable, robust, flexible and maintainable static factory method is another technique that every java programmer should know.
Having static factory method does not mean eliminating the constructor. A programmer can use static factory method in addition to public constructors.

Advantages of static factory method

  • Static factory methods have well chosen names.
A static factory method with a name is comfortable for use compared to many public constructors that have variable parameters. If such multiple constructors exist in class then we may end up calling a wrong one by mistake so static factory method eliminates this restriction.
For example, myClass(int a, int b, int c) constructor returns date, month and year then instead of that we could have a static factory method as myClass.retriveDate().

  • Static factory methods improves performance.
If equivalent objects are requested often to the class then a pubic constructor produces a new duplicate object on every request. Static factory methods comes over this drawback. It relieves immutable class from creating unnecessary objects.
It improves performance if repeatedly object creation requests are expensive. So in this way static factory method open up a way for instance-controlled classes (classes that maintain control over what instance exist at any time).


  • Unlike Constructors static factory method returns an object any subtype of their return type.
This flexibility allows classes to return objects without being public. So in this way it hides implementation.

  • Another Advantage is that they decrease the verbosity of creating parameterized type constructors.
 For example, following declaration requires you to provide type parameters two times in quick succession.
Map<String, List<String>> m = new HashMap<String, List<String>>();

 Now using static factory methods we can deploy it easily as follows
 private static <K, V> HashMap<K, V> newInstance()
{
            return new HashMap<K, V>();
}
and u can replace above declaration in a compact way like this
Map<String, List<String>> m = HashMap.newInstance();

Disadvantages of  static factory methods

  • Classes without public or protected constructors cannot be subclassed.
  • Static factory methods are easily distinguishable from other static factory methods.
     In order to overcome this disadvantage you should adhere to some common and unique static factory method naming. For instance, valueOf(), of(), getInstance(), newInstane(), getType(), newType().

Monday, July 7, 2014

The Chronicle of Computer Languages

Whether you are from IT sector or not, but one question must have popped up into your mind, what is a computer language? Why it was requited? Actually, I am too young to narrate all the past events and founder of computer languages. But, still I feel crucial to write this post. An answer to the question, why computer languages are requires lies in another question i.e. Why do we require English.? Why don't we speak binary language? Well, because binary language is well specified for computers having no common sense. Computer languages consists of instructions and these instructions participate in communication with computers. The computer languages were set in motion when Charles Babbage proposed the first analytical machine. And the major dominating languages are: BCPL led B, B led to C, C evolved into C++ and C++ set the platform for java and in turn C#. The famous hierarchy of computer languages is as follows.


BCPL

BCPL was developed by Martin Richard in 1966. And it is well known to have influenced B. Few have heard about BCPL because nowadays it seems to be vanished. Actually BCPL is successor to CPL programming language. BCPL system library provided I/O support and very simple memory management. BCPL included integers, reals, bit patterns, I/O streams, various kinds of references, and vectors. BCPL strongly influenced B which in turn influenced C.


B was developed at bell labs with the efforts from Ken Thompson and Dennis Ritchie primarily developed for system programming. It included processing of integers, characters and bit strings.  Code efficiency of this language was good compared to assembly level languages.
B is also extinct and take over by C.

C

C is a programmer's language. Invented and first implemented by Dennis Ritchie. It shook the computer world because it changes the programming approach. Earlier failures of assembly level languages gave C more popularity. The power of C is its structured principles. C is one of the widely used programming language of all the time. It was believed to be a language of modern age. Because of increasing the complexity in C programs led to invention of C++.

C++

C++ is a response to the increasing complexity of programs in C++. However even with a structured programming once a program reaches certain size its complexity exceeds what a programmer can manage. To solve this problem Bjarne Stroustrup invented this object oriented programming. It is a methodology that organize complex programs through the use of inheritance, encapsulation and polymorphism. It led to use of classes. C++ includes all of C's features, attributes, and benefits. so it is an enhancement to an already highly successful one.

JAVA

JAVA was created by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sheridan at Sun Microsystems, Inc. in 1991. The language was initially called 'oak' but was later renamed to 'JAVA' in 1995. It is an architecturally neutral language. It is portable and platform independent language that could be used to produce code that would run on a variety of CPUs under differing environments. Bytecode is JAVA's magic.

C#

Most important example of JAVA's influence is C#. Recently created by microsoft to support the .NET framework. .Net Framework, C# is closely related to JAVA. for example, both share the same general C++ style, support distributed programming and utilize the same object model. There are of course many difference between JAVA and C#, but the overall look and feel of these languages is very similar. 
This "cross-pollination" from JAVA to C# is the strongest testimonial to date that java redefined the way we think about and use a computer language

Sunday, July 6, 2014

Frequently asked HR Interview Questions

Are you going to appear for HR interview? Then I recommend some sort of questions (capability to be asked) to be noticed before appearing. No doubt, the technical interview questions changes from job to job, but some selected HR interview questions are potentially found be same in all types of job.
Remember, being interviewed is a great opportunity so here I am posting some questions likely to be asked. Generally, HR interview is scheduled after technical interview so it will be improper to judge it as a casual discussion with. Indeed, you should not underestimate the HR interview. The interview typically consists of interviewer asking questions, checking the confidence, determination and productivity of the candidate.

Before interview go through your resume, know about the company, requirement of the company, check how your skills are  matching with the job requirement.

On the day of the interview have proper attire, feel freedom, carry appropriate copies of resume.
The answers I posted here, some of them i found on other websites and some of them are my opinions.

Introduce yourself

This question is sure to be asked, i will exotic if it is not asked.
The pretty good answer for the question would be
My name is XYZ.
I live in ABC.
I have completed my B-Tech from EXAMPLE college with the percentage of 76.
I had completed my schooling from CBSE board with the percentage of 93 in intermediate and 92 in high school. My short term goal is to take job a reputed company like yours. My long term goal is give full satisfaction from my side like discipline, work hard and as per company requirements.
My strength is I am very motivated person and easily adapting new environment.

Why do you want in our company?

As I am fresher. I need a good platform to start up my career where I can use my skills properly to get better experience and enhance my knowledge. The company's requirement are matching with my profile that will provide me environment where I am interested if I will work in my interested Field I would give best.

What is your goal?

My short term goal is to be placed in a good company and I would like to prove myself to that company.
My long term goal is to improve myself step by step thoroughly and I want to see myself as a well developed and skilled person in future.

How do you define your ideal company?

An ideal company is one where I can utilize my skills and abilities and can gain more knowledge.

Why should we hire you?

Being a fresher I don't have any working experience. But I can assure you that I am a hard worker, confident and can adapt to any environment easily. Under your training and guidance I will enhance my practical knowledge and skills and will utilize them to their full extent for the growth of your organization.

Are you comfortable to work at night?

I think its more important to have a complete rest to give my best so its hard to work in the weekends and nights but I ensure that I will complete my work in time. Still, if my company need my presence I will do it with pleasure.

What are your outside interests?

My outside interests are watching movie and listening music. I want spent time with my parents. I spend some time for net surfing with my laptop to know the new things.

How will you be a good resource to our organization?

My dedication and firm determination will help me to become an asset of the organization.
Sir, the discipline, dedication and determination are the asset that the employee must have to grow himself as well as his company & I'm one of them how have these qualities.

Give me an example of your creativity.

At the time if interview if you don't remember any creativity. Then you can say like following
There is no certain example right now. But everyone has creativity in some field or in other. But he/she needs to apply at right time and at right place.

Tell us something about our company.

It is one of the reputed and fast growing company in IT Industry, It has a so many branches in India as well as throughout the world.This Company was founded in ______(year), The CEO of the Company is______(Name), This Company having the Project in Banking Sector, Govt Sector and So on. The growth rate of this company is_____(%)in the ______(year). These all I heard about this company.

Where do you see yourself five years from now?

I see myself as an important resource to your organization 5 years from now where my position reflects my hard work and dedication and where my position proves my skill and receiving best employee award for the same.

Expected Salary.

More than money my preferences are job satisfaction and career development. I am sure that, reputed company like this won't disappoint me.

Do want to ask any question to me?

Sir I would like to have feedback on my performance so that I could analyze and improve my strengths and rectify the shortcomings.
During Interview it would be disappointing to you if you could not answer any of question. So, I encourage you to go across these questions before you come out for interview. Explain the interviewer in a humble way how you aim to advance in your career. Show confident exhibition of your thoughts. Be succinct. The examples above posted are general and you should pour healthy ingredients by being specific to the position the company offers. Read my whole interview experience here.

Saturday, July 5, 2014

Cognizant Technology Solutions Interview Experience

Dear Friends, hereby, I am sharing my success story. A story of happiness, which advanced me to step into IT corporate world. Before I begin I would prefer to tell you that I had appeared for 10 IT industries earlier, but was unsuccessful in impressing the interviewers. In spite of that i never lost hopefulness. I never stopped endeavoring.

I urge you not to read before interview is about to start. It's a kind of dreadful. Instead, the sort of thing that is required is arousal of complete freedom inside you.

L.D. College of Engineering(Gujarat) organized a pool campus drive for 2014 students from GUJARAT region.

Selection process:

  1. Online Aptitude Test
  2. Technical interviews for Test Shortlists
  3. HR discussion for Technical Shortlists

Designation :

The students who get shortlisted were offered the role of Programmer Analyst Trainee
26 colleges of GUJARAT participated in this recruitment process. So the number of students appeared was difficult to guess, but roughly 1000 students had arrived. So there was  nothing improper. More the number of students, more the number of requirements of company, greater is the opportunity.

Online Aptitude Test

23 March, 2014 It was a day for Online Aptitude test. The written exam consisted of three sections namely English, Aptitude and Reasoning. A huge mass of students passed this test. The shortlisted candidates were informed by SMS and the admit card for interview was sent via E-mail.

Technical interview

The interview was held in a big training and placement hall of L.D. Engineering College. Roughly 7-8 panels were simultaneously active. 
The following questions I was asked in the interview 
  1. Tell me about yourself.
  2. Why Normalization is required?
  3. What are the types of joins in DBMS?
  4. Explain natural join with example.
  5. Which programming language you are comfortable with?
  6. Explain your final year project.
  7.  What are your outside hobby?
  8. Further questions on that hobby you mentioned
  9. Do you have any questions for me?
After completion of technical interview I was asked wait outside the hall. After sometime a placement coordinator came outside and told me that i was selected in the technical test. Past interview experience helped me to be acquainted with the interviewer. Then It was announced to wait in the waiting room for HR interview call.

HR Interview

Before interview started a thorough verification of all my certificate was done. See most probably asked HR interview questions here.
  1. Tell me about yourself.
  2. What type of people are expected IT industry? (Hard working or smart working?)
  3. Have you done any hard work? Explain with example.
  4. Have you done any smart work? Explain with example.
  5. What does cognizant do?
  6. What is the difference IT product company and IT solution company? with example.
  7. Do you want to ask me any question?
Both the interviewers were friendly. On 1st April 2014, final result of selected students was declared.
I was happy because I was selected. 118 students from 26 colleges were selected. Well, I didn't answered all the questions correctly but some of  them I answered were strong and full of confidence.
Read the interview experiences of others. Be confident, little nervousness is good. Prepare well. All the best. 
BE OPTIMIST, BE OPPORTUNIST.