Posts

Showing posts from February, 2019

Why toString() ?

toString() in java ------------------------------------------------------------------------- There is a superclass of all classes in java that is Object class .There are some methods which are present in the Object class. One of the methods is toString( ) . Do you ever know what if I print an object? When I print an object, internally it calls the toString() method of Object class And the "classname@hashcode" of the class is printed. So if we want to print something that we want by toString(), Then we have to override it and to give it our own implementation. Following is the default implementation of the toString() in java @Override public String toString() { return "ConnectionClass [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ",                                                                 ...

Wrapper Class In Java

Wraper classes in java ----------------------------------------- There is a w r apper class for every primitive in Java. The wrapper class for int is Integer and the class for float i s Float and so on. Wrapper classes also provide many utility functions for primitives like Integer.parseInt(). Ex: Write a me thod which will accept anything. We can write the method as   public method(Object obj) { } Now as all classes are children of Object clas s, their objects can be passed in the above program. However primitive types(like int,float...etc) ar e not children of Object and hence cannot be passed. We can wrap the primitives inside corre sponding wrapper classes(like Integer,Float....etc) and then can pass to the function.