Friday, September 18, 2015

Initialize byte array in Java and converting byte array to String

Just like any other Array, Byte array also have  the same syntax.

Below is  just an example to initialize a byte array.

byte[] bytes = [69, 121, 101, 45, 62, 118, 101, 114, 61, 101, 98];

But when you try to initialize your byte array, you will get compile time errors some times for bigger values.

For ex :

byte[] bytes = [69, 121, 101, 45, 62, 118, 101, 114, 196, 195, 61, 101, 98];

That code won't compile and you'll see a compile time error at the numbers, 196,195. The reason is that, Byte can hold up to the values  -128 to 127 only since it is 8 bits. Values greater or lesser than that should explicitly cast to byte so that they become bytes and not int's.

Hence here is the array after the  cast from int to  byte.

byte[] bytes = {69, 121, 101, 45, 62, 118, 101, 114, (byte) 196, (byte) 195, 61, 101, 98};

If you see, we casted the int values to bytes so that they sit in place of bytes.

Converting byte array to String : -

 Here is a small example to convert out byte array to a String in required charset format.

  String data = new String(bytes, StandardCharsets.UTF_8); 

Tuesday, September 15, 2015

Orphaned case error in Java Switch case

Orphaned Case Error in Java is one of the rarest errors you can see in Java. You can see this error  in cases like

  • When you write your case statements outside your switch statement. 

 switch (var) {  
       case 1:  
       //some code here....  
   case 2:  
       //some code here....  

  • If by mistake if you terminated your switch testaments unexpectedly 

 switch (var); { <--- statement terminated by ;   
     case 1:   
     //some code here....   

And in another case  where a case statement doesn't belong to switch and became orphan.

Note : Errors like this won't be seen if you are using an IDE since they compile your code on the fly and show the error message immediately, only traditional compilers will show this error, when you compile through your command line.

Wednesday, September 9, 2015

List of applications using GWT

In my initial days of GWT programming, with so many features why Google is not using GWT in it's own web applications. 

Later I found the below list of applications from Google which using GWT as their framework.




Hotel Finder



The New Blogger

Chrome Webstore  

Product Search

Public Data

New Google Groups


It seems there are many companies and famous websites also built with GWT. Will be back with complete list soon. 

Share it GWT lovers.

Contacting server with GWT. And complete example of RPC call.

There are couple of  possibilities to hit the database with GWT like RequestFactory and RPC.

Before getting started with server calls please go through,

 - GWT RPC (Which makes server calls Asynchronously)

 - RequestFactory (Alternative to GWT-RPC  which use proxies in between).

In this perticular post, let see the complete example of GWT RPC call to server.

Simple RPC structure can show  as below :

 GWT Code <===> InterfaceAsync <===> Interface (Synchronous)<===> Server Code 

Ok, Let's write code for small RPC, which is a PingPong. Client just says Ping to server and server responds with Pong.

ASynchronous Interface PingPongRPCInterfaceAsync , which we can able to access on client side.

     public interface PingPongRPCInterfaceAsync {  
     public void PingPongRPC (String message, AsyncCallback callback);  

The Synchronous Interface PingPongRPCInterface

   public interface PingPongRPCInterface extends RemoteService {  
     public String PingPongRPC(String message);  

Here is the  Service layer class which is equals to Servlet in J2EE and which implements our server side interface PingPongRPCInterface.

    public class PingPongRPCImpl extends  RemoteServiceServlet implements PingPongRPCInterface {  
     private static final long serialVersionUID = 1L;  
     public String pingPongRPC(Sting message)  
       // Hey I received a message here from client   
             // And sending response to client back   
       System.out.println("Message from client" + message);       
       String serverResponse= "Pong";  
       return serverResponse;              

Map the above server impl class in your web.xml;


We all done with implementation part and let see how we can use this service call in our client side.

       private final pingPongRPCInterfaceAsync beanService =  
       ServiceDefTarget endpoint = (ServiceDefTarget) service;  

Once you register your service you can just invoke the method from your client interface as

String message = "Ping to server";  
   beanService.pingPongRPC(message, callback);  
    AsyncCallback callback = new AsyncCallback()  
       public void onFailure(Throwable caught)  
         //Do on fail  
       public void onSuccess(String result)  
         //Process successfully done with result  

And please note down the below package structures:

PingPongRPCInterfaceAsync ,pingPongRPCInterface should be in client* package
PingPongRPCImpl   should be in  server package.

That's a complete example of RPC. Please post in comments if you have any doubt or exception in middle of RPC.


Thursday, September 3, 2015

LinkedList in Java- Implementation, difference with array list, pros cons and example with time complexity

Linked list in Java

Another powerful weapon in Java's Collection API is LinkedList which is Doubly-linked list implementation of the List and Deque interfaces.

There are N number of Collection containers in the whole framework (List, Map, Etc ...), but choosing the right container for your data is the toughest job than operating it.

Here are the reason's to choose LinkedList over other containers 

1) When you want to add or remove the elements from the container over the time. This is the case when you don't exactly know the number of elements before in hand. You can choose ArrayList also as it also accepts the no of elements overtime but you don't see the performance differences under the hood.

2) If you are sure that you are not going to access the elements with Random index which costs O(n) [read big O notation to understand what is O(n)]. That means you are getting the elements only in sequential order all the time.

3) When you want overtime iterations, Linked lists are really cheap to do that.

LinkedList implementation type 

Java used doubly linked list implementation where the consumer have a choice to move and backward direction. If it is Singly linked list implementation you cannot have a choice of iterate over the list is reverse. 

In Doubly Linked list implementation each Node of data consists the information of it's next and previous Nodes. If you remove any Node in between, the previous and next will gets updates as required. Look at source of Linked List implementation.

Example to LinkedList 

public static void main(String args[]) {  
      List<String> linkedlist = new LinkedList<String>(); 
      //adding elements to LinkedList 
      //Adding First and Last Elements  in LinkedList
      //This is how to get with index  
      //This is how to set with index  in LinkedList
      linkedlist.set(1, "NewFirstString");  
      // Removing first and last elements from LinkedList  
     // Removing elements from LinkedList with index  

Pro's of LinkedList

LinkedList class can contain duplicate elements.
Java LinkedList class maintains insertion order.
Java LinkedList class is non synchronized.
In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
ava LinkedList class can be used as list, stack or queue.

Con's of LinkedList:

LinkedList cannot be accessed Randomly
Extra storage space for a pointer is required in case of linked lists
Reverse Traversing is difficult (If singly LinkedList)
Access time for Individual Element is O(n) whereas in Array it is O(1).