HTML

How to Find IP address of Localhost, Server or Your Machine

In today's Java programming tutorial, we will learn some networking basics by exploring java.net package. One of the simple Java network programming exercise, yet very useful, is to write a program to find IP address of the local host in Java. Sometimes this question also asked as find IP address of Server on which your Java program is running or find IP address of your machine using Java etc. In short, they all refer to localhost. For those who are entirely new in networking space, there are two things to identify a machine in a network, which could be LAN, WAN or The Internet. First thing is DNS name and the second thing is IP address. In Local Area Network, DNS is more generally replaced by hostname, which could be as simple as ReportingServer to ReportingServer.com or something else.

What is the Actual Use of interface in Java?

An interface in Java has remained a complex topic for many beginners to understand. The first thing which puzzles many programmers is the fact that you cannot define any method inside interface, it a just declaration. By rule, all method inside interface must be abstract (Well, this rule is changing in Java 8 to allow lambda expressions, now interface can have one non-abstract method, also known as a default method). So, if you can't define anything, Why we need an interface?  what's the use of an interface, if we are anyway going to write a class and override them to provide behaviour, Can't we declare those methods inside the class itself without using interface etc. Well, if you are thinking in terms of behaviour then you are really missing the point of interface.

Difference between Static and Dynamic Binding in Java

When you call a method in Java, it is resolved either at compile time or at runtime, depending upon whether it's a virtual method or a static method. When a method call is resolved at compile time, it is known as static binding, while if method invocation is resolved at runtime, it is known as Dynamic binding or Late binding. Since Java is an object-oriented programming language and by virtue of that it supports Polymorphism. Because of polymorphism, a reference variable of type Parent can hold an object of type Child, which extends Parent. Now if you call a virtual method (not private, final or static) on this object, then Compiler can not find actual method, because it could be the one, which is defined in the Parent class, or the one which Child has overridden. This call can only be resolved at runtime when the actual object is available. That's why this is known as runtime or dynamic binding.

How to Convert java.util.Date to java.sql.Date - Example

There are two date classes in Java, one in java.util package and other in the java.sql package. Though both are known as Date class, there is some difference between java.util.Date and java.sql.Date e.g. Former is used whenever a Date is required in Java application while later is used to read and store DATE SQL type from the database. There is one more important difference is, java.util.Date stores both date and time values, while java.sql.date only stores date information, without any time part. As per Javadoc, java.sql.date is a thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated. See SQL date vs Util date for few more differences.

How to Create File and Directory in Java with Example

Many beginners confused with the fact that same class java.io.File is used to create both file and directory in Java. I agree, this is not very intuitive and  junior developers probably start looking for a class called java.io.Directory, which doesn't exists. On the other hand, creating file and directory are simple in Java, as java.io.File provides methods like createNewFile() and mkdir() to create new file and directory in Java. These method returns boolean, which is the result of that operation i.e. createNewFile() returns true if it successfully created file and mkdir() returns true if the directory is created successfully. There is another method called mkdirs(), which you can use if parent directory doesn't exist, it's like mkdir -p option from UNIX mkdir command. In this Java program, we will learn how to create file and directory, only if they don't exist already.