One of the
key difference between a static and a non-static method is that static method
belongs to a class while non-static method belongs to the instance. This means
you can call a static method without creating any instance of the class by just
using the name of the class e.g. Math.random() for creating random numbers in Java. Often utility methods which
don't use the member variables of the class are declared static. On the other
hand, you need an instance of the class to call a non-static method in Java.
You cannot call it without creating an object because they are dependent upon
the member variables which has different values for different instances.
One more important difference
between the static and non-static method is
that you cannot use a non-static
member variable inside
a static method, you cannot even call a non-static method from the static
method, but the opposite is true e.g. you can call a static function from a
non-static method in Java.
Friday, 21 April 2017
Thursday, 20 April 2017
Difference between static and nonstatic member variables in Java
The concept of static remains same, that doesn't change
with method or member variables but there are still some subtle details, which
every Java programmer should know and understand. As with static methods, a static member variable belongs to a
class and a non-static member variable belongs to an instance. This means, the value of a static variable will be same for all instances, but the value of a non-static variable
will be different for different objects. That is also referred as the state of
objects. The value of nonstatic member variable actually defines the state of
objects.
Wednesday, 19 April 2017
Difference between BufferedReader and Scanner class in Java - Example
Difference between BufferedReader and Scanner
class in Java - Example
Even though both BufferedReader and Scanner can read a file or user input from command
prompt in Java, there some significant differences between them. One of the
main difference between BufferedReader and Scanner class is that former is meant to just read
String while later is meant to both read and parse text data into Java
primitive type e.g. int, short, float, double, and long.
In other words, BufferedRedaer can only read String but Scanner can read both String and other data types like int,
float, long, double, float etc. This functional difference drives several other
differences on their usage. Another difference is Scanner is newer than BufferedReader, only introduced in Java 5, while BufferedReader is present in Java from JDK 1.1 version. This
means, you have access to BufferedReader in almost all JDK version mainly Java 1.4 but Scanner is only
available after Java 5. This is also a popular core Java questions from interviews. Since many developer lack Java IO skill,
questions like this test their knowledge about API and how to do some practical
task.
Tuesday, 18 April 2017
Difference between final vs finally and finalize in Java
Difference between final vs finally and
finalize in Java
The final, finally, and finalize are one of the three confusing keywords, modifiers and methods in
Java. They sounds similar but they are for different purpose. For example, final keyword is a modifier in Java. When you use
final keyword with a class it becomes a final class and no one can extend this
e.g. String is final in Java. When you use the final modifier with
method than it cannot be overridden in subclass, and when you use the final keyword with variable, it become a constant i.e. its
value cannot be changed once assigned. On the other hand, finally is a keyword
related to exception handling in Java. It's often used with try block and it's part of try, catch and finally
trio. A finally block can be used with or without catch block
in Java and its guaranteed to be always executed, irrespective of what happens
inside try block. This is the reason finally block is used to do cleanup and
free resources.
Monday, 17 April 2017
Difference between HashMap vs IdentityHashMap in Java
Difference between HashMap
vs IdentityHashMap in Java
The IdentityHashMap is
one of the lesser known Map implementation
from JDK. Unlike general purposes Map implementations like HashMap and LinkedHashMap, it is very special and it's internal
working is quite different than HashMap. The main difference between IdentityHashMap and HashMap in Java is that former uses equality operator
(==) instead of equals()method to
compare keys. Which means you need the same key object to retrieve the value
from IdentityHashMap, you cannot
retrieve values by using another key which is logically equal to previous key.
Another important difference between HashMap and IdentityHashMap is
that IdentityHashMap doesn't
use hashCode() method instead it uses System.identityHashCode() method. This is a significant difference because
now you can use mutable objects as key in Map whose hash code are
likely to change when the mapping is stored inside IdentityHashMap.
Subscribe to:
Posts (Atom)