Posts

Showing posts from 2015

The Curious Case Of Strings In Java

Image
img src The most important thing to note about String objects in java is that they are immutable. Immutability means that once the state of an object is defined, it cannot be changed. To make String immutable, all the attributes in String class are declared as final. The attribute 'hash' holding the hashcode is an exception though. Following extract from the String class shows the declaration of the attribute which holds the String's value 1 2 /** The value is used for character storage. */ private final char value []; Because this char array is declared as final, once a String object is assigned a value, it can never be changed. However, the references to a String object are mutable, which means , we can point to another String object using the same reference. Let's look at the following code which explains the above concept : 1 2 3 4 5 6 7 8 9 10 11 12 13 public class TestStrings { public static void ...

What is ConcurrentModificationException , how is it caused and how can it be prevented ? (Part 2- ArrayLists)

In the previous article, we saw with an example of HashMap  how a ConcurrentModificationException is caused. In this article ,we will see how it happens in case of Lists. The basics are the same as in the example of a HashMap. When the structure of a list is modified by adding or removing an element from the list while an iterator is  traversing over it, a ConcurrentModificationException is caused. This is because just like HashMap , an  ArrayList also maintains a count of the times the structure of the list is modified (modCount). When an  iterator is obtained on that list, it creates its own expectedCount which is set to the modCount value . Any  addition or removal from the list will change the modCount.When the iterator compares the modCount with  expectedCount whenever iterator's next() method is called , it throws the  ConcurrentModification exception  if it finds a difference .Such an iterator is said to be fail-fast. When we use Cop...

What is ConcurrentModificationException , how is it caused and how can it be prevented ? (Part 1- HashMaps)

ConcurrentModificationException occurs when a Collection is modified structurally while an iterator is already traversing the Collection. Taking the example of a HashMap,while iterating through the entries of a HashMap, if the internal structure of the HashMap is changed by either adding an element to it or removing an element from it, a ConcurrentModificationException is caused. What happens internally? HashMap maintains an integer variable called modCount. This modCount is nothing but the count of times the HashMap has been modified structurally. If we add an element to the HashMap, the modCount increases. Similarly, if we remove an element from the HashMap, the modCount increases. Interestingly, when we just update an entry, there isn't any change to the modCount. This is because update to an element does not change the internal structure of the HashMap. When we create an iterator for the Entry table of the HashMap, an expectedCount variable is created which is s...

Writing a Web Service Client in Java using Eclipse IDE

Image
The steps involved in writing the Web Service Client will be as follows: a) Locate the service description in the form of WSDL for the web service in question. (Actually the first step is to search UDDI for the services. I am excluding this step here to simplify the process ) b) Once we have the WSDL document, most of the client code is auto-generated using the wsimport tool. c)   Run the client and invoke the web service. Now I will demonstrate the steps to write a Web Service client in Java using Eclipse IDE. This web service client is invoking the web services available at the url  "http://www.webservicex.net ”         1)       Go to the url : http://www.webservicex.net/ws/default.aspx              2)       Choose the web service you want to write the client for. I have chosen Computer Unit converter, which gi...

Web Services In Java- An Introduction

Image
As the name implies, a web service is a "service available on web". The main difference between a web application (which may also be called as a service available on web) and a Web service is that the website/web application is meant for humans as end users whereas a web service is meant for another application. A web service basically services the needs of an application running on another server which is network accessible.  The main advantage of Web Service is Interoperability, which means it is not any technology or platform specific. This means that a C# application running on  an Application Server A on a Windows machine can request for a web service written in Java and running on an Application Server B on a Linux machine. It may also happen that the web service host is an Application Server whereas the web service client is a mobile device having a network access to the host. Web Services are mainly of two types: SOAP based (JAX-WS specifi...