Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, 5 April 2020

Singleton - Java vs Kotlin

Singleton is a widely used design pattern in our programming, because the single instance or object reference has been used to accessing their properties and that instance remains through out the  application session.

Now we can see how it will be implemented in Java and Kotlin.

Singleton Using Java:

public final class Singleton {
    private static final Singleton INSTANCE = new Singleton();
    public String property1;

    private Singleton() {
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }

    public String getInfo() {
        return "SingleInfo";
    }
}

Points to be remembered on creating the Singleton class using Java.
  1. Class need to be final, in order to avoid this class to be inheritance by other.
  2. Constructors are need to be private
  3. private, static and final instance have to instantiated.
Accessing the properties and methods will be as follows.

String property1 = Singleton.getInstance().property1;
String info = Singleton.getInstance().getInfo();

Singleton in Kotlin: Here it is very crazy in kotlin, the below things does the job.

object Singleton {
    var property1: String? = null
    val info: String
        get() = "SingleInfo"
}


val property = Singleton.property1
val info = Singleton.info

Happy Coding :-)

Sunday, 25 September 2016

Java - String, StringBuilder & String Buffer


Below is the main difference between these three most commonly used classes.


  • String class objects are immutable whereas StringBuffer and StringBuilder objects are mutable.
  • StringBuffer is synchronized while StringBuilder is not synchronized.
  • Concatenation operator "+" is internal implemented using either StringBuffer or StringBuilder.
Criteria to choose among String, StringBuffer and StringBuilder
  • If the Object value is not going to change use String Class because a String object is immutable.
  • If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is not synchronized one.
  • In case the Object value can change, and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.
The main idea is that String is immutable. So when you are trying to modify it, new object created. StringBuffer and StringBuilder are mutable. And the difference is the first one is thread-safe.
The common approach to using StringBuilder is to parse something when you iteratively create a String object in a NON thread safe environment.
For example, you have a numbers array [1, 2, 3]. To create object "String1String2String3" you can use a StringBuilder
StringBuilder builder = new StringBuilder();
foreach(Integer num : array) {
    builder.append("String").append(num);
}
builder.toString();
It's better than just using String string = string + ("String" + num);. AFAIK, compiler will optimize using string concatenation in the loop to StringBuilder, but better to use it manually.
StringBuffer is used when you have shared states, that are modified by concurrent threads.
Courtesy & Source:
http://stackoverflow.com/a/11273836
http://stackoverflow.com/a/11273893

Sunday, 24 January 2016

Java Quick Reference - Tyro

This post will briefly gives you an idea about important Java syntax on one page to get you up to speed quickly, including:

Class Implementation:

public class MyClass {                                              
public String mString; 
private int mInt; 
// More member variables 
// Constructor for Class 
public MyClass() {
 mString = “Foo”; mInt = 10; }
 // More meth
 }                                                               
}

Declaring Variables:
double doubleVar = 1.0 
doubleVar = 2.0f
int intVar = 1; 
String stringVar = “Hey”; 
Boolean truth = true;

Sample Values:
int: 1, 2, 500, 10000 
double: 1.5, 3.14, 578.234 
boolean: true, false 
String: “Kermit”, “Gonzo”, “Ms. Piggy”                              
ClassName: Activity, TextView, etc