Skip to main content

Some Important Facts About Core Java

  1. There are 4 important OOPS concepts in Java : Inheritance, Abstraction, Encapsulation and Polymorphism.
  2. There are two types of variables in Java : Primitive types and Object Types
  3. The default value of Boolean is false (the same applies for the Wrapper class too).
  4. Marker interface is the one which doesn't contains any methods, it is like empty interface.
  5. You cannot override the 'main' method of the Java, even-though if you try to do so it will not give any exception, program compiles and executes fine. The reason is : 'main' method is of type static. Static methods are class level methods. Even-though it looks like method overriding, it is not overriding. These are two different method with respect to the corresponding class.
  6. You can Overload the 'main' method of Java, it is valid in Java.
  7. You can have only one 'Public' class and any number of non-public classes in any java source file.
  8. Inheritance is an example for IS-A relationship.
  9. Composition is an example for HAS-A relationship.
  10. You can have abstract class without any abstract methods.
  11. The use of abstract class without any abstract method : a) Whenever you  don't want to direct instantiation of the class, b) Whenever you want to add abstract future at later point of time which you are not aware currently, Otherwise I don't see any usage of such class.
  12. String class is immutable and thread-safe. The reason is , whenever we create String object mentioning some string literal either inside the constructor or direct literal, first it checks in the String constant pool whether that string literal is available or not. If available then it will not create the new instance inside the constant pool instead it points reference to the same string constant. In this case this string might be referenced by many different reference variables. Suppose you make changes to this string value it will alter all the referenced string variables by breaking the actual behavior. Because of this reason String class implemented as Final & Immutable one.
  13. If you want to update your string frequently and dynamically, in case you need to modify your string based on some scenario then you can either go With StringBuffer or StringBuilder based on either for Multithreaded application or Single threaded application. StringBuffer and StringBuilder both classes are same in behavior and functionality except the fact that, StringBuffer class is Synchronized and StringBuilder class non-synchronized.
  14. The efficient way of creating String object is : String str="abcd";, the reason is it creates instance only String Constant Pool and refer it through the reference variable 'str'. On other case, whenever you create String object using 'new' operator as : String str=new String("abcd");, in this case instance will be created in the heap and as well as in the String constant pool if the same string doesn't exist already, else it refers the existing string instance from the pool.

Still There are many to add, I will be updating this post in coming days....

Popular posts from this blog

Resolve OutOfMemoryError With ExcelExport : Export Excel Utility with Apache POI Stream API (SXSSF)

Whenever we try to export excel of huge data (for ex: around 200000-300000 records), most of the time we end up with OutOfMemoryError:JavaHeapSpace. And also consuming more time to process or export that much of data. Main reason to this kind of problem is that, the prior version of Apache POI (prior to 3.8) will not provide proper solution for this kind of situations and also we have other issues with the API designed with those versions. Even I had faced issues of not supporting more than 65000 rows of data during exporting excel with prior versions of POI. But with the version 3.8 and higher they come with solutions for all these problems. To resolve Memory issue and performance issue of Excel Export they have utilized stream API to design their API to support huge data export and performance issues. With stream API we can flush only few rows of data into the Memory and reamining we can flush to the hard memory (permanent Memory). In this example y…

Consider static factory methods instead of constructors

The normal way for a class to allow a client to obtain an instance of itself is to provide a public constructor. There is another technique that should be a part of every programmer’s toolkit. A class can provide a public static factory method, which is simply a static method that returns an instance of the class. Here’s a simple example from Boolean (the boxed primitive class for the primitive type boolean). This method translates a boolean primitive value into a Boolean object reference:

public static Boolean valueOf(boolean b) {
return b ? Boolean.TRUE : Boolean.FALSE;
}

          Note that a static factory method is not the same as the Factory Method pattern from Design Patterns . The static factory method described in this item has no direct equivalent in Design Patterns. A class can provide its clients with static factory methods instead of, or in addition to, constructors. Providing a static factory method instead of a public constructor has both advantages and disadvant…

Java Performance Checklist

Specify the required performance. Ensure performance objectives are clear.Specify target response times for as much of the system as possible.Specify all variations in benchmarks, including expected response ranges (e.g., 80% of responses for X must fall within 3 seconds).Include benchmarks for the full range of scaling expected (e.g., low to high numbers of users, data, files, file sizes, objects, etc.).Specify and use a benchmark suite based on real user behavior. This is particularly important for multi-user benchmarks.Agree on all target times with users, customers, managers, etc., before tuning.

Make your benchmarks long enough: over five seconds is a good target. Use elapsed time (wall-clock time) for the primary time measurements.Ensure the benchmark harness does not interfere with the performance of the application.Run benchmarks before starting tuning, and again after each tuning exercise.Take care that you are not measuring artificial situations, such as full caches contain…