Posts

Showing posts from May, 2022

Java Interview Questions and Programs

1) Transient Variable in java? 2) Vector vs ArrayList in java? 3) Which version in java? 4) Concurrent hashmap in java? 5) Volatile keyword in java? 6) Comparable and toCompare() method in java? 1) Write syntax of Coroutines, Sealed Classes, Extension Function, Generics in kotlin fun main() = runBlocking { // this: CoroutineScope     launch { // launch a new coroutine and continue         delay(1000L) // non-blocking delay for 1 second (default time unit is ms)         println("World!") // print after delay     }     println("Hello") // main coroutine continues while a previous one is delayed } Generic in Kotlin class  Person<T>(age: T){   var age: T = age   init {   this .age= age   println(age)       }   }   fun  main(args: Array<String>){   var ageInt: Person<Int> = Person<Int>( 30 )   var ageString: Person<String> = Person<String>( "40" )   }   2) Write code for implementing constructor in kotlin class  myClass(name:

SingleTon in Java

  Singleton is a design pattern that ensures that a class can only have one object .  Define a class that has only one instance and provides a global point of access to it . Static member: It gets memory only once because of static, it contains the instance of the Singleton class. Private constructor: It will prevent instantiating the Singleton class from outside the class. Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller. Its used in Logging, Caching, thread pools, configuration settings etc.. Advantages: Saves memory because the object is not created at each request. An only a single instance is reused again and again. Thread Safe class Playground {     public static void main ( String [ ] args ) {         SingleTon singleTonClass = SingleTon . getInstance ();         System . out . println ( "Hello Java" );     } } class SingleTonClass {     private SingleTonClass (){}     private s