Saturday, December 31, 2011

Difference between Wait and Sleep , Yield in Java

Difference between wait and sleep or difference between Sleep and yield in Java are popular core Java interview questions and asked on multi-threading interviews. Out of three Sleep () and Yield () methods are defined in thread class while wait() is defined in Object class, which is another interview question. In this Java tutorial we will learn what is sleep in Java, important points of sleep in java and difference between Wait and sleep in Java.

Friday, December 30, 2011

Step By Step guide to Read XML file in Java Using SAX Parser Example

Reading XML file in java using SAX Parser is little different than reading xml file in Java with DOM parser which we had discussed in last article of this series. This tutorial is can be useful for those who are new to the java world and got the requirement for read an xml file in java in their project or assignment, key feature of java is it provides built in class and object to handle everything which makes our task very easy. Basically this process of handling XML file is known as parsing means break down the whole string into small pieces using the special tokens.
Parsing can be done using two ways:

Error java.lang.UnsupportedClassVersionError: Bad version number in .class files Cause and Solution

How to fix Bad version number in .class file

"java.lang.UnsupportedClassVersionError: Bad version number in .class file" is common error in Java programming language which comes when you try to run a Java class file. In our last article we discussed that how to resolve Java.lang.UnSupportedClassVersionError and found that it comes when major and minor version of class is not supported by Java virtual machine or JRE running the program. Though "java.lang.UnsupportedClassVersionError: Bad version number in .class file" is little different than that in its manifestation and Cause. UnsupportedClassVersionError is not as difficult as Java.lang.OutOfMemoryError  and neither its solution is too complex but what is hard is thinking in right direction because cause of different types of UnsupportedClassVersionError is different.

Difference between DOM and SAX Parsers in Java

Difference between SAX and DOM Parser is very popular Java interview and often asked when interviewed on Java and XML. Both DOM and SAX parser are extensively used to read and parse XML file in java and have there own set of advantage and disadvantage which we will cover in this article. Though there is another way of reading xml file using xpath in Java which is more selective approach like SQL statements people tend to stick with XML parsers. DOM Parser vs SAX parsers are also often viewed in terms of speed, memory consumption and there ability to process large xml files.

How to read and write Images in java using ImageIO Utility

Writing an Image file in Java is very common scenario and in this article we will see a new way to write images into file in Java. javax.imageio.ImageIO is a utility class which provides lots of utility method related to images processing in Java. Most common of them is reading form image file and writing images to file in java. You can write any of .jpg, .png, .bmp or .gif images to file in Java. Just like writing, reading is also seamless with ImageIO and you can read BufferedImage directly from URL. Reading Images are little different than reading text or binary file in Java as they they are associated with different format. Though you can still use getClass().getResourceAsStream() approach for loading images.

How to Convert Map to List in Java Example

Map and List are two common data-structures available in Java and in this article we will see how can we convert Map values or Map keys into List in Java. Primary difference between Map (HashMap, ConcurrentHashMap or TreeMap) and List is that Map holds two object key and value while List just holds one object which itself is a value. Key in hashmap is just an addon to find values, so you can just pick the values from Map and create a List out of it. Map in Java allows duplicate value which is fine with List which also allows duplicates but Map doesn't allow duplicate key.

Thursday, December 29, 2011

4 example to Iterate over HashMap, Hashtable or any Map in Java

There are multiple way to iterate, traverse or loop through Map, HashMap or TreeMap in Java and we all familiar of either all of those or some of those. But to my surprise one of my friends was asked in his interview (he has more than 6 years of experience in java programming) to write code for getting values from hashmap or TreeMap in Java with at least 4 ways. Just like me he also surprised on this question but written it. I don't know why exactly some one ask this kind of java interview question to a relatively senior java programmer. Though my closest guess is to verify that whether he is still hands on with coding in java. Anyway that gives me idea to write this Java tutorial and here is multiple ways to traverse, iterate or loop on a Map in Java, so remember this because you may also ask this question J.

What is final in Java? Final variable , Method and Class Example

Final in java is very important keyword and can be applied to class, method, and variables in Java. In this java final tutorial we will see what is final keyword in Java, what does it mean by making final variable, final method and final class in java and what are primary benefits of using final keywords in Java and finally some examples of final in Java. Final is often used along with static keyword in Java to make static final constant and you will see how final in Java can increase performance of Java application.

Difference between JRE JVM and JDK in Java Programming language

JRE, JVM and JDK are three terms you often heard in conjunction with Java programming language and most people either confuse between them or think they all are same. In this java article we will what is Java Run-time (JRE), what is Java virtual Machine (JVM) and what is Java development Kit (JDK) along with Just in Time compiler or JIT. Once you know what JRE, JVM or JDK means you can differentiate them easily by yourself.  This article is in continuation of Difference between Comparable and Comparator in Java and Difference between ConcurrentHashMap and Synchronized-map.

Wednesday, December 28, 2011

How to Parse or Read XML File in Java >> XML Tutorial Example

How to parse xml file in Java or how to read xml file in java is one of common need of a Java Developer working with enterprise Java application which uses XML for data representation, messaging and data transfer. Java has good support to handle XML files and XML Documents and you can read XML File in Java, create or write to XML file in Java by using various XML parsers available. Reading XML file is little bit different than reading text or binary file in Java but it uses same concept of File class.

Monday, December 26, 2011

Checked vs Unchecked Exception in Java Example

Checked and Unchecked Exception is two types of Exception exist in Java. Though there is no difference in functionality and you can very achieve same thing with either checked Exception or Unchecked Exception, there is some difference on exception handling part. In this Java tutorial we will see what is checked and Unchecked Exception in Java, Examples of Checked and Unchecked Exception and most importantly we will learn when to use Checked Exception and when to use Unchecked Exception in Java and lastly we will see difference between checked and unchecked exception to understand things better. By the way this article is second in my post on Exception along with difference between throw and throws in Java and improved Exception handling in Java 7 with Automatic resource management ARM and multi-catch block in JDK 7.

Sunday, December 25, 2011

Observer design Pattern in Java with Real world code Example

Observer design pattern in Java is a fundamental core Java pattern where Observe watch for any change in state or property of Subject. For Example Company updates all its shareholders for any decision they make here Company is Subject and Shareholders are Observers, any change in policy of company and Company notifies all its Shareholders or Observer. This was simple real world explanation of Observer pattern. In this article we will in detail what is Observer Design pattern, what is benefit of Observer design Pattern, Example or Observer pattern in Java and few other points. Just like Decorator design Pattern and Factory Pattern in Java, Observer pattern is also used in JDK.

How to change Tomcat default port 8080

Tomcat default port is 8080 but many times other Java application also uses 8080 like any other web-server Resin or Jetty and starting tomcat may result in java.net.BindException:Address already in use: JVM_Bind 8080. In order to avoid this exception you can change default port of tomcat from 8080 to some other port e.g. 8081 or 8082. Though don't change to tomcat port which is likely to be used by tomcat itself e.g. 8443 is used by tomcat https port. Use port which is most likely to be free. In this tomcat tutorial we will see how to change default port 8080 for http protocol in tomcat and port 8443 port for https protocol in tomcat.

Friday, December 23, 2011

What is Method Overloading and Overriding in Java - Example Differences Tutorial

What is method overloading and overriding in Java
Method overloading and method overriding in Java is two important concept in Java which allows Java programmer to declare method with same name but different behavior. Method overloading and method overriding is based on polymorphism in Java. In case of method overloading, method with same name co-exists in same class but they must have different method signature, while in case of method overriding, method with same name is declared in derived class or sub class.Method overloading is resolved using static binding in Java at compile time while method overriding is resolved using dynamic binding in Java at runtime. In short When you overload a method in Java its method signature got changed while in case of overriding method signature remains same but a method can only be overridden in sub class. Since Java supports polymorphism and resolve object at run-time it is capable to call overridden method in Java. By the way difference between method overloading and overriding is also one of the popular Java design question and appear in almost all levels of  Java interviews.

Tuesday, December 20, 2011

Java.net.BindException: Address already in use: JVM_Bind:8080 Solution

java.net.BindException: Address already in use: JVM_Bind is a common exception in Java with application trying to connect on a particular port and some other processes either Java or non Java is already connected on that port. You can get "Address already in use: JVM_Bind" error while doing remote debugging in Java in Eclipse, when Eclipse trying to connect to remote Java application, when you are starting tomcat and another instance of tomcat is listening on port 8080 you will get java.net.BindException: Address already in use: JVM_Bind:8080.

Address already use: JVM Bind ExceptionIn this post we will analyze java.net.BindException and trying to figure out cause of "Address already in use: JVM_Bind" before fixing it. This article is in continuation of my earlier tutorial, How to Solve OutOfMemoryError in Java and How to fix ClassNotFoundException in Java.

Monday, December 19, 2011

Why main method is public static in Java

Main method in Java is the first programming method a Java programmer knows when he starts learning Java programming language.have you ever thought about why main method in Java is public, static and void, of-course Yes, since most of us first learn C and C++ than we move to Java in our programming path we familiar with main method but in Java main method is slightly different it doesn't return any value like in C it returns int, main method is public static and void Why? In this post we will try to find answer of these questions and have an idea of one of the most popular questions in Java why main method is declared Static.

Friday, December 16, 2011

What is load-on-startup servlet element in web.xml with Example?

load-on-startup is an element which appears inside <servlet> tag in web.xml.4 years back load-on-startup was a very popular servlet interview question because not many Java J2EE developer was familiar with this element and how load-on-startup works inside servlet container like tomcat or webshere. In this J2EE Tutorial we will see what is load on start up, how to use load-on-startup element and what are different values we can configure for loadOnStartup inside web.xml.

Is Apple IPhone4S Siri going to replace the Google Search Engine

I am sure this is one of those most optimistic title you can think of but that is what my first thought after seeing apple introductory video of Siri, there new voice recognition and task completion service integrated on Apple Iphone4s. I was amazed with smoothness and accuracy of Siri as shown on that video. You just say siri what's whether, find me new restaurant, traffic status and you get a response you are looking for, this looks to me a new way of searching web and technology.  Asking questions is more natural than typing on Google and believe if Siri kicks of like that you will suddenly have more search volume via siri than google, though only limiting factor is not everybody has Apple Iphone4s with them :). People are also thinking Siri as Google Killer let's see.

Wednesday, December 14, 2011

5 Example of kill command in UNIX and Linux

Kill command in UNIX and Linux is normally used to kill a suspended or hanged process or process group. Though kill is mainly associated with kill operation its mere a signal transporter and can send specified signal to specified process in UNIX or UNIX like systems e.g. Linux, Solaris or FreeBSD. Like in windows when we see a particular process hung the system we go to task-manager find the process and kill it, similarly in UNIX and Linux we first find the process ID (PID) of offending process and then kill it. Though we have killAll command also which doesn't require PID instead it can kill the process with just process name. Kill commands is often a wrapper around kill () system call but some Linux systems also has built-in kill in place. In this article we will see some examples of kill command in UNIX and how we can use kill command to kill the locked process.

Monday, December 12, 2011

Java TreeMap Tutorial: 10 Example of TreeMap in Java

TreeMap in Java is a SortedMap and it maintains Sorting order when you insert object on it.
You can specify Sorting order while Creating TreeMap by providing an explicit Comparator to
TreeMap. Basically you can create TreeMap in Java by different ways, a TreeMap with natural sorting order, and TreeMap with custom Sorting Order by providing Comparator, Copying Sorting order from other SortedMap etc. TreeMap has specific constructor for these conditions. We will see these in section of creating instance of TreeMap in Java.  We will also see how to put element, get element, iterate over TreeMap, clearing and reusing TreeMap in this Java TreeMap tutorial. This article is in continuation of my collection series e.g. HashMap vs HashSet , SynchronziedHashMap vs ConcurrentHashMap and Iterator vs Enumeration in Java

Friday, December 9, 2011

How to parse String to Enum in Java | Convert Enum to String with Example

Converting Enum into String and parsing String to Enum in Java is becoming a common task with growing use of Enum. Enum is very versatile in Java and preferred choice to represent bounded data and since is almost used everywhere to carry literal value its important to know how to convert Enum to String in Java. In this article we will see both first converting Strings to Enum in Java and than Changing an Enum to String in Java with Example. I thought about this Enum tutorial when I wrote 10 Examples of Enum in Java. I missed String to Enum conversion and one of reader pointed out that. So here we have now.

Enum to String to Enum in Java

This article is in continuation of other conversion related post e.g. how to convert Date to String in Java and How to Convert String to Integer in Java. As these are common needs and having best way to do things in mind saves lot of time while coding.

Wednesday, December 7, 2011

Top 5 FIX Protocol Data Dictionaries and Online FIX References

While you work on FIX Protocol you need a handy FIX Data dictionaries or what we called fixionary in order to refer for exact details related to particular message type, particular fix tag and what does a particular value of tag means. Without FIX data dictionary or fix wiki it’s very hard to work especially when you are dealing with different fix protocol versions like FIX 4.0, FIX 4.1, FIX 4.2 and FIX 4.4. In this FIX Protocol article we will explore some of free available fixionaries or fix protocol reference guides. FIX data dictionary is a must have development tool for FIX protocol developers.

Tuesday, December 6, 2011

How to read and write in text file in Java

Java has excellent support for reading from file and writing to file in Java. In last post we have seen how to create file and directory in Java and now we will see how to read content from file in java and how we will write text into file in Java. File provides persistent solution to Java developer; in almost every Java application you need to store some of the data in persistent may be its user configuration, system configuration or related to state of application but without persistence no enterprise Java application can buildup. Normally Database is preferred choice for storing data persistently but in high frequency trading one doesn’t have liberty to afford network latency to travel database and latency introduced by database itself, though database still offer best choice for historical data , transactional details related to orders and trades are commonly stored in In Memory files provided by Java.nio API.

Monday, December 5, 2011

How to deal with Java.rmi.MarshalException: CORBA MARSHAL in Java

Java has some mysterious RMI exception one of them is java.rmi.MarshalException: CORBA MARSHAL, which was bothering me from last couple of days while working. I thought to put this solution post similar to my earlier post How to solve OutofMEmroyError in Java and Fixing ClassNotFoundException in Java. RMI is still used in many places and many times it create too much problem and chew-up your lot of time to find exact cause and solution.

Sunday, December 4, 2011

Matt Cutt Revealing Google's Latest Algorithmic Changes

Inside Search: Search quality highlights: new monthly series on a...: Today we’re publishing another list of search improvements, beginning a monthly series where we’ll be sharing even more details about the al...

Saturday, December 3, 2011

How to Create File and Directory in Java Example - Java IO Tutorial

How to create File and directory in Java is probably the first things come in mind when we exposed to file system from Java. Java provides rich IO API to access contents of File and Directory in Java and also provides lots of utility method to create file, delete file, read from file, and write to file or directory. Anybody who wants to develop application in Java should have a solid understanding of IO and Networking package. In this Java File Tutorial we will basics of File and Directory in Java, How to Create File and Directory in Java, Utility methods provided by File API and Common Exception or Error you will face during File and Directory Creation or access time. Creating File is different than creating Thread in java as you don’t have to implement any interface for making a Class as File in Java.


File in Java is also getting its place on various core java interviews questions specially after introduction of java.nio package and concepts like In Memory Files, we will discuss about those in probably another blog post but what it confirms is importance of knowledge of File IO for java programmer.

Friday, December 2, 2011

Java String Replace Example Tutorial

This String replace example in Java will show you how to replace String in Java both at character level and by using regular expression. Since String is final in Java every time you replace String you will get a new String object only if your actually replace anything on original String otherwise replace methods of String return same String object. String Class in Java provides 4 methods to replace String in Java. Those methods allow you to replace character from String, replace CharacterSequence from String, replace all occurrence of pattern in String or just first occurrence of any pattern in Java. We will also see important points on String replace method and how to make best use of regular expression while replace string in Java.

This article is on series of my other String article like 2 ways to Split String in Java and how to convert String to Date in Java. String is one of the most important classes in Java and having a good knowledge of String class is mandatory for any Java developer.

What is Factory method Design Pattern in Java with Example - Tutorial

Factory design pattern in Java one of the core design pattern which is used heavily not only in JDK but also in various Open Source framework such as Spring, Struts and Apache along with decorator design pattern in Java. Factory Design pattern is based on Encapsulation object oriented concept. Factory method is used to create different object from factory often refereed as Item and it encapsulate the creation code. So instead of having object creation code on client side we encapsulate inside Factory method in Java. One of the best examples of factory pattern in Java is BorderFactory Class of Swing API. In this Design pattern tutorial we will see What is Factory method design pattern in Java, What are main advantages of factory pattern in Java , Code example of Factory design pattern and What problem Factory pattern solves in Java or when to use Factory design pattern.  This article is in continuation of my design pattern article as 10 OOPS and SOLID design principles java programmer should know and How to use Observer pattern in Java