Posts

Reversing of char array using java

Hi everyone, in this blog post I will writing about reversing a char array using java. This program requires a char array as its input and it will return the reversed version of that char array. The program which I have written to demonstrate reversing of char array is as follows: package  com.techjourney.methods; public   class  ReversingCharArray {           public   static   void  main(String args[])     {          char  firstArray[] =  new   char []{ 'G' , 'i' , 'l' , 'c' , 'h' , 'r' , 'i' , 's' , 't' };          char  secondArray[] =  new   char []{ 'D' , 'r' , 'a' , 'v' , 'i' , 'd' };                  System.out.println( " First array before reversi...

arraycopy method in java

Hi everyone, its been a long time since I posted. So here is a new one. In this blogpost, I will writing about arraycopy() in java. Its a common interview question these days. It copies an array from specified source array, beginning at the specified position to the specified position of destination array. arraycopy() has got 5 parameters. They are: 1) src - The source array 2) srcPos - The position in source array from where the elements are copied 3) des - The destination array 4) desPos - The position in destination array where the elements copied from source array is pasted. 5) length - Number of elements to be copied from source array. Please check the below program to understand more about arraycopy(). package  com.techjourney.methods; public   class  ArrayCopyMethod {      public   static   void  main(String args[])     {          int ...

SuggestBox in GWT - The correct way to create and populate.

Hi everyone, its been a while. Here, in this post, I will be discussing about creating, initializing and populating a suggestbox in gwt. Suggestbox is a text box or text area which displays a pre-configured set of selections that match users input. Suggestbox is similar to an autocomplete search box. Each Suggestbox is associated with a single SuggestOracle. The SuggestOracle is used to provide a set of selections given a specific query string. SuggestBox uses a MultiWordSuggestOracle as its oracle. Following are the steps to create and populate a SuggestBox and declaration of MultiSuggestOracle. 1) First step is to declare MultiSuggestOracle. private   final  MultiWordSuggestOracle oracle =  new  MultiWordSuggestOracle(); 2) Create SuggestBox @UiField  (provided =  true )  SuggestBox sbox =  new  SuggestBox(oracle); The important thing to remember is to add (provided = true). 3) Populating the SuggestBox ...

Dead Code

Hi Everyone, in this post I will be describing about dead code. I have heard about dead code only recently and thought of putting it in blog. 'Dead code' as it name suggests is a piece of code which is dead or has got no chance of it being executed. In other words dead code is a type of code which is unreachable. I will provide an example which will helps in understanding more about dead code. Consider a piece of code in Java. String fullName = employee.getFirstName() +  " "  + employee.getLastName(); if (fullName!= null ) { fullName =  "Sujith" ; } In the above example the code in the 'if' loop would never be executed. The reason is string 'fullName' cannot be empty because of the space (" ") we are adding between employee.getFirstName() and employee.getLastName().  It doesn't matter even if both employee.getFirstName() and employee.getLastName() be null, "fullName" would not be null. Some IDE's li...

Maven project which adds another project as a dependency.

Image
Hi Everyone, in this post, I will be discussing about how to add another project as a dependency in maven. Maven is a build automation tool primarily used for java projects. Currently, maven is the most common build tool used in the software market. Please check the wiki page for maven to know more topics. I will be demonstrating this example by creating 2 projects named 'MavenProject' and 'MavenProject2'. The first project 'MavenProject' is added as a dependency to the second project 'MavenProject2'. To add a project as a dependency in Maven, the project is added as a dependency to pom.xml of the project which refers the latter project. pom.xml is the xml representation of maven project. It stands for project object model. Click here to read more about pom.xml. The structure of the first project is as follows: The class file MavenProject.java is as follows: package  com.techjourney.maven; public   class  MavenProjec...

Java program to get the details about memory

Hi everyone, in this post, I am writing about how to get the details of maximum memory, free memory, total memory and used memory in java. maxMemory() - Specifies the maximum amount of memory java virtual machine will attempt to use. maxMemory is returned in bytes totalMemory() - Specifies the total amount of memory in java virtual machine. The totalMemory will vary according to the host environment. totalMemory is returned in bytes. freeMemory() - Specifies the amount of free memory in jvm. freeMemory is returned in bytes. The information about all these memory types are provided by Runtime class of java. The memory used by jvm can be obtained from different methods of the Runtime class. Following java program specifies a clear idea about getting details about  3 types of memories. package  com.techjourney; /**  * Created with IntelliJ IDEA.  * User: Sujith  * Date: 6/19/13  * Time: 11:1...

instanceof operator in java

Hi everyone, in this blog post, I will be describing about instanceof operator in java. Instanceof operator is mainly used to specify whether an object is an instance of a class. It allows to determine the type of the object. It is a boolean operator which returns true if the object is an instance of the specified class.  Instanceof operator is a useful tool when there is a collection of objects and we are not sure why they are used. Here is a sample program which will give a better idea about the instanceof operator. package  com.example; import  java.util.ArrayList; import  java.util.HashMap; import  java.util.LinkedHashSet; import  java.util.List; import  java.util.Map; import  java.util.Set; public   class  InstanceOfExample {      public   static   void  main(String[] args) {         List myList =  new  Ar...