Posts

AWS Notes of training

Listing some notes from my learning. I hope it is useful for everybody. AWS is a secure cloud service platform offering computation power, database storage, content delivery and other useful functionalities that help the business to scale. We have different domains in AWS: Compute (EC2, Light Sails, Lamda) Storage (Disk, SSD) : S3 low cost storage Database (SQL/NoSQL): RDS Network and Content delivery: Route53 (DNS system) Management Tools: Manage AWS services (Cloud Watch) Security and Identity Compliance: "Iam" accounts (authenticate user access to AWS ) Messaging: Email Service, PubSub,  Migration (Transfer GB of data to AWS. Snowball (sending physical data to AWS)  ) AWS Compute Services: EC2: Raw server configured to anything Lamda: To host background tasks and not hosting. Listen to triggers and execute the code. Elastic Beanstalk: Advanced of EC2. Here we don't need to set up the env manually. You select what you need and AWS installs every s/w

How generics can be used in Class Factories

Lets see how generics can be used to create generic interface for class factories. public class GenericFactory <E> { Class<E> thisClass=null; public GenericFactory (Class iclass) { this.thisClass = iclass; } public E createInstance() throws IllegalAccessException,InstantiationException{ return(E)this.thisClass.newInstance(); } } The <E> is a type token that signals that this class has a type set when instantiated. public class Myclass { void print() { System.out.println("Hellooo"); } } public class Main { public static void main(String[] args) throws IllegalAccessException, InstantiationException { GenericFactory<Myclass> factory = new GenericFactory<Myclass>(Myclass.class); Myclass myclass= factory.createInstance(); myclass.print(); } } Output:-   Hellooo We can see that, it is not necessary to cast the object returned from the factory.createInstance().

When to use getClass() and instanceOf in Java

getClass() only gives equality if two objects belong to same class whereas instanceOf works for subtype also. If two objects of different classes are checked for equal, then we use instanceOf . But once you use instanceOf at some level in the class hierarchy everything below it must use instanceOf or you will violatate the symmetry clause of equals contract.  e.g. any list can be equal to any list as long they have same elements iun the same order. So any List implementation equals() method must do instanceOf and not getclass() and then compare. On the other hand, it only make sense for na instance of class X to be equal to another instance of class X ( and not any subclass ) , then use getClass().

Java Interview Points

Java Interview Points Uses of finalizer :-  It acts as a safety net. It may be better to free the resources than never e.g. FileInputStream, Connection etc.  Object with native peers, the garbage collector doesn't know about it when it reclaimed the java peer. If native peer hold some critical resources, we can use this method to free the resources.  Clone will not work if the array fields are final because clone would be prohibited from assigning a new value to the field.  A method in an interface can’t be native, synchronized, strictfp but in the implementation class it can be since these keywords describe implementation properties rather than interface properties. Same is true for final.  Only data associated with specific instances of a class is serialized, because static data is associated with a class as opposite to an instance, so it’s not serialized automatically. To serialize static data, one must provide class specific serialization.  Array implementation in Ja

Why we have Runtime Exceptioons in Java

Image
Oracle  defines Exception as  "An  exception  is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions." We have three categories of exception as:- Checked Exceptions , which are subject to catch . All exceptions are checked exceptions, except for those indicated by  Error ,  RuntimeException , and their subclasses Runtime Exceptions are internal to the application and system does not expect to recover from. NullPointerException is type of RunTime Exception Error are exceptions which are external to applications like OutOfMemory Exception, StackOverFlow Exception.    The class diagram of Exception is as follows:- This discussion is regarding why we have Runtime type of exceptions in Java. When exception was first designed, there was worry about conditions that occur because of problems with the machine on which the program being run bugs in VM  security violations. These sort of thin

How class.forName loads the Database Driver in JDBC

When Class.forName() is called with an argument like this: Class.forName("org.gjt.mm.mysql.Driver"); the classloader attempts to load and link the Driver class in the “org.gjt.mm.mysql” package and if successful, the static initializer is run. The MySQL Driver static initializer looks like this: static {   try {     java.sql.DriverManager.registerDriver(new Driver());   } catch (SQLException E) {     throw new RuntimeException("Can't register driver!");   } } So it calls a static method in the java.sql.DriverManager class which apparently registers a copy of itself when it loads.   To understand the why you have to look at the next line in the initial code example: Connection con = DriverManager.getConnection(url,   "myLogin", "myPassword"); The DriverManager class returns a database connection given a JDBC URL string, a username and a password. In order to create that connection, the DriverManager class has to know

Problems on Bit Manipulations

Check if two numbers have opposite sign We can use the operation if    (x ^ y) < 0     where x and y are non zero numbers {  True   --- if numbers of opposite sign {  False  --- if numbers of same sign int   x=-10,y=2; System.out .println(((x ^ y) < 0)); Output :- False Check if number is power of 2 We can use    x & (x - 1)) == 0     True   --- if numbers is a power of 2                                  False  --- if numbers is not a power of 2 int  x=16; System.out.println((x & (x - 1))==0); Output :- True More bitwise tricks x&(x-1)           :---------------- Returns number x with the lowest bit set off x ^ ( x & (x-1 )   :----------------  Returns the lowest bit of a number x x & 1<<n           :----------------   Returns 1<<n if the n-th bit is set in x x | 1<<n           :----------------   Returns the number x with the n-th bit set x ^ 1<<n           :----------------   Toggles the state