Skip to main content

Posts

Showing posts from November, 2017

Difference between Comparable and Comparator in Java

You always been in confusion to differentiate the differences between these two things. Actually speaking Comparable and Comparator are both the Interfaces in Java. Both are since Jdk version 1.2 means quite old still enough to confuse us so far. Basically, the purpose of these two interfaces is to compare two objects. Now you might wondering why we need another class or interface just to compare objects when we already have equals() method in Object class? So, let me give you some background so you better understand the purpose of existence of these interfaces and eventually we will also see the benefits of each one over another plus when to use which interface. Okay! The Background... I hope you might aware about collection framework in java. Collections are objects in java which does the job of container for objects and which theoretically holds unlimited objects. now typed collections holds only the objects which having same type of which collection is.  List...

Data Types in Java

Information that program receives or processed outputs are stored in memory location with some pointer or name to access it, known as Variable. value of variable is prone to change any number of times in lifetime of program. Information can be sequence of character(Strings), numbers, or true or false boolean values. In Java, when you declare a variable you need to declare the type of data or information that it will hold, so Java compiler can understand the what type of information will store in declared variables pointing memory location an in turn JVM allocates that much size of memory,  as every data type has its own limitation or you can say size of data that it can hold. We will see soon below what different types of data types that Java support and their data size limitations. In Java there are two data types available: Primitive data type Reference data type. Primitive Data Type : In Java there are eight primitive data types available to serve different...

Lambda expression in Java 8

Behaviour Parameterisation …pattern which allows you to write such a flexible code which can cope up with any requirement change. Now you may wondering why I am talking about this weird sounding pattern instead lambda expression, right? Well, let me tell you that we will soon get to know about lambda expression in detail but understanding Behaviour Parameterization  pattern will definitely help you thoroughly how lambda expression is useful to write concise and succinct code and you will start feeling power of lambda expression which is one of prime feature of java 8 release. Lets say, we have list of Books and you need to find Books with greater than 200 pages. You could write a method getBooksGreaterThanPages(): Using this method is simple enough, what if you need to find books smaller than 200 pages? Or Books above particular prices? Or Books from particular author or publications? Or even worst book with particular publication with certain pages and below partic...

Reference data type in java

Unlike Primitive data types in java, Reference data type store the address of object in memory which they refer to, than actual value. lets say, Car class is already defined, and its object is created in memory then variable declared with type Car becomes the reference type which will be holding address of car object to which it refers. Car car = new Car(); int noOfDoors = 4; Car myCar = car; // copy the car reference into myCar reference . Here, variable car actually contains the address and variable noOfDoors will contain actual 4 value. See below diagram for more explanation: Reference data type holding address of object in heap memory and primitive data type holds actual primitive values Available reference types in java: Class type Interface type array type e.g. String name = new String("Java"); List listOfNames = new ArrayList(); Students topRankers[] = new Student[10]; Remember though you know now that Reference data type holds ...

this keyword in Java

this  is a one of the keyword amongst the other in java, and java objects includes a data member this  thats actually a reference to the current object. this  keyword is useful if you need to refer to the current object - e.g. when you want to pass the current object to the method or constructor of class. to understand more look at below code snippet in which a class Number  has method printNumber() which prints the value stored in class variable mValue, with the help of NumberPrinter class, which takes the reference of Number class in its constructor. printNumber() method creates the instance of NumberPrinter and pass its own reference in the form of this. Output :

Loop controls in Java

Loop control is one of java's language feature like any other programming language feature, which allows to execute written statements in loop body to execute sequentially those many time until loop condition not get satisfied. Java programming language provides four types of loop controls or constructs. while loop do...while loop for loop for-each loop we will see all those constructs with example one by one. while loop while loop execute the statements or group of statements multiple times unless given loop condition not fail while loop checks for condition first then execute statements in loop body  repeat above step till condition satiesfied see, below code snippet explains while loop: Output: do...while loop do...while loop checks for condition later executing body of loop unlike while loop it does not check condition first, this is one of major difference between do--while and while loop like while loop, it executes loop body until conditi...