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 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 or something else.

How Google Search words, Contents, Images and Videos on Internet

Though we all use Google almost more than 100 times in a day, how many of you know that How Google works? How Google find the content you are looking for? How does it even find all the website in the BIG world of Internet? How does it index URLs? There is a lot of things to know about how Google actually works. Though I know a couple of them before, I found this useful Infographic, which tells you how exactly Google Works. It all starts from Crawling and Indexing. Google has lots of programs known as Spider, which keeps crawling the web, day, and night. This is how Google picks up every new information added into the internet. As long as your website is reachable by The Internet and your Content is crawlable i.e. not prevented or secured by robot.txt, Google will crawl then and index them appropriately.

Can You Run Java Program Without Main Method?

First thing Java programmers learns is that they need a main method to run, but when they go to any Interview or college viva and asked can you run a Java program without a main method, they surprised like hell. Well, there are actually different types of execution model available in Java, for example, Applets which run on browser doesn't have main method, instead they have life-cycle methods like init(), start() and stop(), which controls their execution. Since Applet is a Java program, you can answer this question in Yes. Similarly, we have Servlet, which runs in a Servlet container, comes as bundled in a web server like Tomcat, or Jetty. Servlet also works on callback mechanism, it has methods like init(), service() and destroy(). Container calls init() if Servlet is first time loaded into memory, calls service() if there is a request to process and calls destroy() before removing it from memory.

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 find largest and smallest number from integer array - Java Solution

Good Understanding of array data structure is very important for any software developer, and to develop this understanding there are lots of programming exercise beginners can do. One of them is writing a program to find smallest and largest number in an integer array. Java programmers are no different than others, so they can do this program in Java, not just to understand array but also relational operators available in Java.  In this program, you need to write a method, yes we call the function a method in Java, which will accept an integer array and then print largest and smallest number from that array. Use of any third-party library or API method is not allowed, which means you need to do this exercise by using basic tools of Java programming language, which includes operators, control statements,  keyword and some classes from java.lang package.

This problem is also known as finding maximum and minimum numbers in an array, and technique mentioned here can be used in any other programming language as well. As a bonus point, you can also write JUnit test cases to test your method, I have not done so and relied on simple main method to test my code to show the output and keep it short, essential for any example or demo.

Btw, if you preparing for programming job interview, then don't forget to check the Cracking the Coding Interview book. It contains 150 Programming Questions and Solutions, which is more than enough for many coding interviews.

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 only stores date information, without any time part. As per Javadoc, 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 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, which doesn't exists. On the other hand, creating file and directory are simple in Java, as 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.