Saturday, April 28, 2012

Difference between java and javaw executable commands

difference between java.exe and javaw.exe commands was the recent java question asked to one of my friend. while running java program on windows you might have noticed that they will appear in task manager as either java.exe or javaw.exe,  also in JAVA_HOME/bin we see these two java commands java.exe and javaw.exe, do you know difference between java.exe and javaw.exe ? surprisingly not many java programmer know answer of this java interview question, may be because java developers has not paid enough attention to these commands. any way here we will see few differences between java and javaw commands:

Thursday, April 19, 2012

Java program to reverse a number - Example tutorial

How to reverse a number in Java without using any API or write a simple Java program to reverse a number is common programming questions asked on fresher level software engineer interviews. Reversing a number is also popular homework questions on many Java programming courses on school, colleges and training institutes. I personally feel java program to reverse number is good programming exercise for some one who is just started learning programming in Java or any other programming language because of its simplicity and little bit of trickiness which shows how to use operators for programming purposes rather than arithmetic purpose. In last couple of Java programming tutorial we have seen some basic programming exercises like how to reverse string in Java using recursion and how to check if a number is prime or not, while in this Java tutorial we will see a simple example of Java program to reverse number by just using basic Java operators like division operator(/) and remainder operator(%). division operator returns quotient while modules or remainder operator % returns remainder.

If you are looking for theoretical java interview question which involves understanding of concept rather than programming then you may like Why multiple inheritance is not supported in Javawhy wait and notify are defined in object class and Why main method is public static in Java. But I would say you better horn your programming skills as well by programming Simple programs than moving to tougher one which involves data-structure and designs like how to find length of linked list using iteration and recursion and  design and code for vending machine in Java which accepts per-defined coin and return per-defined product handling all practical condition like returning change, returning money if product is not there etc

Wednesday, April 18, 2012

How to measure elapsed execution time in Java - StopWatch Example

There are two ways to measure elapsed execution time in Java either by using System.currentTimeinMillis()or by using  System.nanoTime(). These two methods can be used to measure elapsed or execution time between two method calls or event in Java. Calculating elapsed time is one of the first thing Java programmer do to find out how many seconds or millisecond a method is taking to execute or how much time a particular code block is taking. Most of Java programmer are familiar with System.currentTimeInMillis()which is there from beginning while a new version of more precise time measurement utility System.nanoTime is introduced in Java 1.5, along with several new features in language like Generics, Enum types, auto boxing and variable arguments or varargs. You can use any of them to measure execution time of method in Java. Though its better to use System.nanoTime() for more precise measurement of time intervals.

In this Java programming tutorial we will see a simple Java program to measure execution time by using System.nanoTime() and Spring framework’s StopWatch utility class. This article is in continuation of my post on covering fundamental Java concepts like How to compare String in Java, How to write equals method in Java correctly and 4 ways to loop HashMap in Java. If you haven’t read them already you may find them useful.

Tuesday, April 17, 2012

Difference between List and Set in Java Collection

What is difference between List and Set in Java is a very popular Java collection interview questions and an important fundamental concept to remember while using Collections class in Java. Both List and Set are two of most important Collection classes Java Program use along with various Map implementation. Basic feature of List and Set are abstracted in List and Set interface in Java and then various implementation of List and Set adds specific feature on top of that e.g. ArrayList in Java is a List implementation backed by Array while LinkedList is another List implementation which works like linked list data-structure. In this Java tutorial we will see some fundamental difference between List and Set collections. Since List and Set are generified with introduction of Generics in Java5 these difference also application to List and Set.

This article is in continuation of my earlier post on Collection e.g. Difference between HashMap vs HashSet, Difference between HashMap and Hashtable and Difference between Concurrent Collection and Synchronized Collection. If you haven't read them already you may find them useful.

Monday, April 16, 2012

How to invoke method by name in java dynamically using reflection

In Java you can invoke any method by its string name dynamically using reflection API. java.lang.reflect API provides powerful reflection mechanism which can load classes by its name even if classes are not available at compile time, Can get all methods including private and public from class and allow you to invoke any method dynamically using reflection. For those who are new in Java this sound pretty strange that at runtime you provide a method name using string and Java can run that method without any code for calling the method during compilation, but Reflection is such a powerful mechanism it allows to do lot of stuff dynamically and if you been using IDE like Netbeans or Eclipse , J2EE framework like Spring and Struts, these all used reflection to provide powerful configuration module and several other useful feature like code assist etc. Reflection is very comprehensive topic and there is a lot to learn but we will start with simple Java program example to invoke a method using reflection in  by providing name of method as String value.  

This Java article is continuation of my post on covering basic concepts like static and dynamic binding in Javawhen to use Interface in Java and why use PreparedStaement in Java. If you are new here or haven’t read them already then you may find them useful.

Sunday, April 15, 2012

Difference between java.util.Date and java.sql.Date in Java - JDBC Question


Difference between java.util.Date and java.sql.Date in Java or why does Java has java.sql.Date if there is already a Date class in java util package is a common java interview question. There is always confusion among many Java developer familiar with both java.util.Date and java.sql.Date. Though many of them already using java.sql.Date to interface with DATE type of any relational database like mysql, oracle or any other database. In this Java tutorial we will see some differences between java.sql.Date and java.util.Date which will help to understand both of them better.

This Java tutorial is in continuation of our earlier post on java.util.Date like How to convert String to Date in Java and how to get current Date and Time value in GMT timezone.  if you haven't read them already you may find them interesting and useful. If you like JDBC articles then checkout some JDBC Performance tips and  Benefits of PreparedStatement in Java.

Saturday, April 14, 2012

How to convert local time to GMT in Java Program Example tutorial code

Converting local time into GMT or any other timezone in Java is easy as Java has support for time zones. JDK has a class called java.util.Timezone which represents timezone and Java also has classes like SimpleDateFormat which can use Time zone while parsing or formatting dates. By using java.uti.TimeZone and java.text.SimpleDateFormat we can write simple Java program to convert local time to GMT or any other time zone in Java. We have already seen example of How to get current date and time in GMT and this Java program is extension of that in order to convert time form one timezone to other timezone.

This article is in continuation of my previous basic Java tutorials like How to convert Double to String in Java , How to convert char to String in Java and how to write hashcode in Java. If you haven’t read them already you may find them useful.

Friday, April 13, 2012

Java Program to connect Oracle Database with Example - JDBC Tutorial Sample Code

How to connect to Oracle database from Java Program using JDBC API is common need for many Java programmer, though there are lot of framework available which has simplified JDBC development e.g hibernate, Spring JdbcTempate and many more, but creating Java program to connect to oracle database from plain old Java is still most easy and quickest method for testing and debugging database connectivity. Database connection program is also a common Java programming exercise in many Java programming courses on school, colleges and various training institutes. We have been exploring  some advanced concepts and best practices on JDBC in my previous articles like  Why should you use PreparedStatement in Java and 4 JDBC performance tips for Java application , which you may like if you are on more advanced level. This simple java program is intended for beginners in Java which have just started learning JDBC API.

If you like to read tutorials on database then you may find difference between truncate and delete10 example of SQL SELECT queries and how to manage database transaction useful and interesting.

Java PropertyUtils Example - getting and setting properties by name

PropertyUtils class of Apache commons beanutils library is very useful and provides you ability to modify properties of Java object at runtime. PropertyUtils enables you to write highly configurable code where you can provide name of bean properties and there values from configuration rather than coded in Java program and Apache PropertyUtils can set those properties on Java object at runtime. One popular example of how powerful PropertyUtils can be is display tag which provides rich tabular display for JSP pages, it uses PropertyUtils class to get values of object at runtime and than display it. You can setup properties as column and only selected columns will be displayed. Even larger web framework like Struts and Spring also uses Apache commons beanutils library for getting and setting java properties by name.

PropertyUtils is based on Java reflection but provides convenient method to operate on Java object at runtime. By using PropertyUtils you can get map of all Object properties which enables you to change them at runtime by values coming from all the places like web request, database or configuration files. In this Java tutorial we will example of how to get and set properties of java object using PropertyUtils class at runtime.

This article is in continuation of my earlier post on open source library like How to limit number of user session in web application using Spring Security and How to perform LDAP authentication on windows Active directory using Spring Security. If you haven’t read them already you may find them useful and interesting.

Java Program to find factorial of number in Java - Example Tutorial

How to find factorial of a number in Java on both recursive and iterative way is a common Java interview question mostly asked at fresher level. It’s not just popular on Java interview but also on other programming language like C or C++. Its also famous  In our last article we have seen how to check if a number is prime or not and in this Java programming tutorial we will see a simple Java program to find factorial of a number in Java by using recursion and iteration. Same program can also be used to print factorial of any number or printing a range of factorial as well. Let’s talk about logic, factorial of a number is calculated by formula number*(number -1) till zero and since value of factorial zero is 1, it acts as a base case in recursive version of factorial method. logic to find factorial of number is encapsulated inside factorial(int number) and fact(int number) method.

This Java programming tutorial is in next with my earlier tutorial for beginners like Java Program to reverse String in Java with recursion , Java program to free memory in java and recently  how to read and write on text file in Java etc, if you haven’t read them you may find them interesting and useful.

Java Program to print Prime numbers in Java - Example Tutorial and Code

How to print Prime numbers in Java or how to check if a number is prime or not is classical Java programming questions, mostly taught in Java programming courses. A number is called prime number if its not divisible by any number other than 1 or itself and you can use this logic to check whether a number is prime or not. This program is slightly difficult than printing even or odd number which are relatively easier Java exercises. This Simple Java program print prime number starting from 1 to 100 or any specified number. It also has a method which checks if a number is prime or not.

This Java tutorial is in conjunction with my earlier tutorial for beginners like How to set Path in Java on windows and Unix , Java Program to reverse String in Java with recursion and recently  how to read file in Java etc, if you haven’t read them you may find them useful.

Thursday, April 5, 2012

What is bounded and unbounded wildcards in Generics Java

Bounded and un-bounded wildcards in Generics are two types of wildcard available on Java. Any Type can be bounded either upper or lower of class hierarchy in Generics by using bounded wildcards. In short <? extends T> and <? super T> represent bounded wildcards while <?> represent an unbounded wildcard in generics . In our last article How Generics works in Java , we have seen some basic details about bounded and unbounded wildcards in generics and In this Java tutorial we will see bounded and unbounded generics wildcards in details. We will start from basics like what is bounded wild card in generics and what is unbounded wildcard  and than will some popular java interview questions on generics like Difference between ArrayList<? extends T>  and ArrayList<T super ?>.

What is interface in Java with Example - Tutorial

What is interface in Java
Interface in java is core part of Java programming language and one of the way to achieve abstraction in Java along with abstract class. Even though interface is fundamental object oriented concept ; Many Java programmers thinks Interface in Java as advanced concept and refrain using interface from early in programming career. At very basic level  interface  in java is a keyword  but same time it is an object oriented term to define contracts and abstraction , This contract is followed by any implementation of Interface in Java. Since multiple inheritance is not allowed in Javainterface is only way to implement multiple inheritance at Type level. In this Java tutorial we will see What is an interface in Java, How to use interface in Java and where to use interface in Java and some important points related to Java interface. What is an interface in Java is also a common core Java question which people asked on various programming exams and interviews.

Sunday, April 1, 2012

How to find current directory in Java with Example

How to find current directory in Java with ExampleIts easy to get current directory in Java by using built-in system property provided by Java environment. Current directory represent here the directory from where "java" command has launched. you can use "user.dir" system property to find current working directory in Java. This article is in continuation of my earlier post on Java e.g. How to load properties file in Java on XML format or How to parse XML files in Java using DOM parser and Why Java doesn’t support multiple inheritance . If you haven’t read them already, You may find them useful and worth reading. By the way here is a quick example of finding current directory in Java: