Thursday, January 24, 2013

1/24/2013



1. Primitives and collections

Why Java Collections can not directly store Primitives types?

It was a Java design decision (mistake). Containers want Objects and primitives don't derive from Object.
Generics allow you to pretend there is no wrapper, but you still pay the performance price of boxing. This is IMPORTANT to many programmers.

what is so hard technically to include primitive types when talking about Generics ?
In Java's case, it's because of the way generics work. In Java, generics are a compile-time trick, that prevents you from putting an Image object into an ArrayList<String>. However, Java's generics are implemented with type erasure: the generic type information is lost during run-time. This was for compatibility reasons, because generics were added fairly late in Java's life. This means that, run-time, an ArrayList<String> is effectively an ArrayList<Object> (or better: just ArrayList that expects and returns Object in all of its methods) that automatically casts to String when you retrieve a value.

But since int doesn't derive from Object, you can't put it in an ArrayList that expects (at runtime) Object and you can't cast an Object to int either. This means that the primitive int must be wrapped into a type that does inherit from Object, like Integer.


2. A constructor cannot be abstract, static, final, native, or synchronized
It can be public, protected, default, and private.

But, what’s the point of having a private constructor?



2.1 The first that you only want objects to be created internally – as in only created in your class.

Private constructors can be used in the singleton design pattern

2.2 The second is that you don’t want any objects of your class to be created at all.
when creating an object doesn’t make sense – and this occurs when the class only contains static members.


3. Static method can only use static variables.


4. The concept is : || and && are short circuiting operation i.e. if the value of the expression can be known by just seeing the first part then the remaining part is not evaluated while | and & will always let all the parts evaluates.

robust design
if ( (str == null) || (i == str.length() )
(i == str.length()) will only be evaluated if (str == null) is false, and (str == null) will be false if 'str' is NOT null. So it will NEVER throw a NullPointerException.


5. Unlike a class, an interface can extend from multiple interfaces.

6. invalid Identifier

My Variable // Contains a space
9pins // Begins with a digit
a+c // The plus sign is not an alphanumeric character
testing1-2-3 // The hyphen is not an alphanumeric character
O'Reilly // Apostrophe is not an alphanumeric character
OReilly_&_Associates // ampersand is not an alphanumeric characterIdentifiers must comply with these rules:

7. key word:  native?  friendly?

No comments:

Post a Comment