Showing posts from December, 2017

What is Dependency Injection?

Dependency injection is a concept valid for any programming language. The general concept behind dependency injection is called Inversion of Control. According to this concept, a class should not configure its dependencies statically but should be configured from the outside.

A Java class has a dependency on another class if it uses an instance of this class. We call this a _class dependency. For example, a class which accesses a logger service has a dependency on this service class.

Ideally, Java classes should be as independent as possible from other Java classes. This increases the possibility of reusing these classes and to be able to test them independently from other classes.

If the Java class creates an instance of another class via the new operator, it cannot be used (and tested) independently from this class and this is called a hard dependency. The following example shows a class which has no hard dependencies.
packagecom.jithin88.example.di; importjava.util.logging.Logger; p…

Thread Safety in Java

Thread safety in java is the process to make our program safe to use in multithreaded environment, there are different ways through which we can make our program thread safe.
Synchronization is the easiest and most widely used tool for thread safety in java.Use of Atomic Wrapper classes from java.util.concurrent.atomic package. For example AtomicIntegerUse of locks from java.util.concurrent.locks package.Using thread safe collection classes, check this post for usage of ConcurrentHashMap for thread safety.Using volatile keyword with variables to make every thread read the data from memory, not read from thread cache.Java synchronized Synchronization is the tool using which we can achieve thread safety, JVM guarantees that synchronized code will be executed by only one thread at a time. java keyword synchronized is used to create synchronized code and internally it uses locks on Object or Class to make sure only one thread is executing the synchronized code.
Java synchronization works on…

Singleton Class in Java

Java Singleton Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine.The singleton class must provide a global access point to get the instance of the class.
To implement Singleton pattern, we have different approaches but all of them have following common concepts. Private constructor to restrict instantiation of the class from other classes.Private static variable of the same class that is the only instance of the class.Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class. Below are the different types of singleton designs, Eager initialization In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a singleton class but it has a drawback that instance is created even though client application might not be using it. package co…

Gradle In Android

What is the use of Gradle in Android? In Android Studio, Gradle is a custom build tool used to build android packages (apk files) by managing dependencies and providing custom build logic. 
Gradle is a build system that takes the best features from other build systems and combines them into one. It is improved based off of their shortcomings. It is a JVM based build system, what that means is that you can write your own script in Java, which Android Studio makes use of.
One cool thing about gradle is that it is a plugin based system. This means if you have your own programming language and you want to automate the task of building some package (output like a JAR for Java) from sources then you can write a complete plugin in Java or Groovy(or Kotlin), and distribute it to rest of world.
Why did Google use it? Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little to no learning curve, and without learning Groovy …

Intern() Method

Java automatically interns String literals. This means that in many cases, the == operator appears to work for Strings in the same way that it does for ints or other primitive values.

Since interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String()

Using your example:String s1 ="Rakesh";String s2 ="Rakesh";String s3 ="Rakesh".intern();String s4 =newString("Rakesh"

Memory Leaks In Android Apps

Memory Leak is a scenario that occurs when objects are no longer being used by the application, but the Garbage Collector is unable to remove them from working memory – because they’re still being referenced. As a result, the application consumes more and more resources – which eventually leads to a fatal OutOfMemoryError.
Keeping a long-lived reference to a Context is the best way to ran into memory leaks issues in Android applications. There are two easy ways to avoid context-related memory leaks. The most obvious one is to avoid escaping the context outside of its own scope. The example above showed the case of a static reference but inner classes and their implicit reference to the outer class can be equally dangerous. The second solution is to use the Application context. This context will live as long as your application is alive and does not depend on the activities life cycle. If you plan on keeping long-lived objects that need a context, remember the application object. You c…