Monday 29 December 2014

Eclipse Shortcut Keys

Ctrl + 3

Usually coders have trouble finding files in a long list of projects, and it can be time-consuming to find a file. This shortcut brings up the quick access box, that brings up files, views and commands as you are searching for them. Instead of drilling down in the explorer, just search for it!

Ctrl + Shift + G

Finds references to a variable throughout the workspace.

Ctrl + F8

Shift between perspectives (Java, Debug, DDMS) quickly

Ctrl + F11

Run last launched application.

Ctrl + Space

If you forget a function,variable or class name, just type a couple of letters and hit this shortcut to autocomplete with suggestions. Even generates conditional statements after matching with starting words.

Ctrl + O

Displays all the methods and variables and methods available in the current class selected.

Ctrl + Shift + O

Organize imports. This comes very handy when you suddenly have to pull up external classes and functions (such as the Math class) and Java will throw a syntax error. With this shortcut, the missing packages will automatically be imported.

Ctrl + Shift + F

This shortcut automatically formats your code, according to predefined formatting rules set in the IDE. This is a boon for all the messy programmers out there who need their code to be indented properly!

Ctrl + Shift + P

Finds the matching bracket for the current body of code. Very useful when figuring out missing bracket syntax issues.

Ctrl + PgUp/PgDn

Toggles between editor views, much faster to switch between different classes user is working on.

Alt + Shift + Z

Surround a block of code with a try/catch clause.

Alt + Shift + R

Rename any variable,method or class and Eclipse automatically renames all references.

Alt + Shift + L

Highlight an expression commonly used in your program and extract it as a variable- this will make your code cleaner.

Ctrl + 1

Quickfix. Wherever Eclipse shows an error, this shortcut can be used to quickly solve the problem with one of the suggestions shown.

Alt + Shift + T

Opens the quick refactoring menu. Can choose from a variety of refactorings to make the code cleaner and more efficient.

Ctrl + Shift + T

Open/Search for types

Ctrl + Q

Position the cursor back at the last changed location in the editor.

Alt + Shift + N

Open up create menu directly to choose a new class/project.

F4 while selecting variable

Show type hierarchy.

Ctrl + 2,L or F

Assign statement/expression to a new local variable or field.

Saturday 27 December 2014

Java Arrays

Description     
     An array is a group of similar typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. Array is simple type of data structure which can store primitive variable or objects. For example, imagine if you had to store the result of six subjects we can do it using array. To create an array value in Java, you use the new keyword, just as you do to create an object.

Defining and constructing one dimensional array

      Here, type specifies the type of variables (int, boolean, char, float etc) being stored, size specifies the number of elements in the array, and array-name is the variable name that is reference to the array. Array size must be specified while creating an array. If you are creating a int[], for example, you must specify how many int values you want it to hold (in above statement resultArray[] is having size 6 int values). Once an array is created, it can never grow or shrink.

Initializing array: You can initialize specific element in the array by specifying its index within square brackets. All array indexes start at zero.

                              resultArray[0]=69;

This will initialize first element (index zero) of resultArray[] with integer value 69. Array elements can be initialized/accessed in any order. In memory it will create structure similar to below figure.


Array Literals

      The null literal used to represent the absence of an object can also be used to represent the absence of an array. For example:

                    String [] name = null;

In addition to the null literal, Java also defines special syntax that allows you to specify array values literally in your programs. This syntax can be used only when declaring a variable of array type. It combines the creation of the array object with the initialization of the array elements:

                      String[] daysOfWeek = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”,                                                                “Friday”, “Saturday”};
This creates an array that contains the seven string element representing days of the week within the curly braces. Note that we don't use the new keyword or specify the type of the array in this array literal syntax. The type is implicit in the variable declaration of which the initializer is a part. Also, the array length is not specified explicitly with this syntax; it is determined implicitly by counting the number of elements listed between the curly braces.

Let’s see sample java program to understand this concept better. This program will help to understand initializing and accessing specific array elements.

package arrayDemo;

import java.util.Arrays;

public class ResultListDemo {


public static void main(String[] args) {
//Array Declaration
int resultArray[] = new int[6];
//Array Initialization
resultArray[0]=69;
resultArray[1]=75;
resultArray[2]=43;
resultArray[3]=55;
resultArray[4]=35;
resultArray[5]=87;
//Array elements access
System.out.println("Marks of First Subject- "+ resultArray[0]);
System.out.println("Marks of Second Subject- "+ resultArray[1]);
System.out.println("Marks of Third Subject- "+ resultArray[2]);
System.out.println("Marks of Fourth Subject- "+ resultArray[3]);
System.out.println("Marks of Fifth Subject- "+ resultArray[4]);
System.out.println("Marks of Sixth Subject- "+ resultArray[5]);
}

}

Output



Alternative syntax for declaring, initializing of array in same statement

                  int [] resultArray = {69,75,43,55,35,87};

Multidimensional Arrays

In Java, multidimensional arrays are actually arrays of arrays. These, as you might expect, look and act like regular multidimensional arrays. However, as you will see, there are a couple of subtle differences. To declare a multidimensional array variable, specify each additional index using another set of square brackets. For example, the following declares a two dimensional array variable called twoDim. This will create matrix of the size 2x3 in memory.

                   int twoDim[][] = new int[2][3];




Let’s have look at below program to understand 2-dimentional array

package arrayDemo;

public class twoDimArrayDemo {

public static void main (String []args){

int twoDim [][] = new int [2][3];
twoDim[0][0]=1;
twoDim[0][1]=2;
twoDim[0][2]=3;
twoDim[1][0]=4;
twoDim[1][1]=5;
twoDim[1][2]=6;

System.out.println(twoDim[0][0] + " " + twoDim[0][1] + " " + twoDim[0][2]);
System.out.println(twoDim[1][0] + " " + twoDim[1][1] + " " + twoDim[1][2]);
}

}

Output

Inbuilt Helper Class (java.util.Arrays) for Arrays Manipulation:

Java provides very important helper class (java.util.Arrays) for array manipulation. This class has many utility methods like array sorting, printing values of all array elements, searching of element, copy one array into other array etc. Let’s see sample program to understand this class for better programming. In below program float array has been declared. We are printing the array elements before sorting and after sorting.

package arrayDemo;

import java.util.Arrays;

public class ArraySortingDemo {

public static void main(String[] args) {
//Declaring array of float elements
float [] resultArray = {69.4f,75.3f,43.22f,55.21f,35.87f,87.02f};
System.out.println("Array Before Sorting- " + Arrays.toString(resultArray));
//below line will sort the array in ascending order
Arrays.sort(resultArray);
System.out.println("Array After Sorting- " + Arrays.toString(resultArray));
}
}

Output



Similar to “java.util.Arrays” System class also has functionality of efficiently copying data from one array to another. Syntax as below,

    public static void arraycopy(Object src, int srcPos,Object dest, int destPos, int length)

The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.

Important points:

You will get “ArrayIndexOutOfBoundsException” if you try access an array with an illegal index, that is with a negative number or with a number greater than or equal to its size.
Arrays are widely used with loops (for loops, while loops). There will be more sample program of arrays with loops tutorial.

Summary

An array is a group of similar typed variables that are referred to by a common name.
Array can be declared using indexes or literals.
Single dimensional array is widely used but we can have multi-dimensional.

Wednesday 17 December 2014

What is Cloud Computing ?

Cloud Computing! is a concept of abstract computing with resources delivery on demand.

     I am trying to explain like a drop in the ocean about Cloud Computing.
What it is ?
why it is? and
am I in Cloud Computing?.
My answer is – Yes
      if you are working with internet technology, You are related with cloud computing. While you are working on your personal computer with the internet connection, You are connected with the cloud networks.

      In a cloud computing the resources of hardware and networking are shared over the internet. A company providing the services of cloud computing (like: Amazon, IBM) provides the network infrastructure world wide, where users share resources with lower cost. In cloud computing we don’t need to setup own server rooms, hardware, etc individually. We just use them through web browser even we don’t know where the servers situated.

      For an example of Amazon cloud services. We just Login to our AWS management console through web browser and can create, manage server instances, s3 and many other services. It’s not to be bad to say a Single window management for all your network services.

Services of Cloud Computing ?

Cloud computing services are divided into three categories: Infrastructure as a Service (IaaS), Platform as a Service (PaaS), Software as a Service (Saas).


Deployment Models of Cloud Computing 

      Cloud Computing mainly has 4 deployment models with there own working process and accessibilities.

                        Public
                        Private
                        Hybrid

                        Community

Benefits cloud computing ?

1. Low cost - Mostly cloud services are available with cheaper costs due to its resource sharing. Like cloud services providers take a full advantages of virtualization.

2. Easy to Manage - Cloud services are easy to manage, we don’t need to work physically with the devices.

3. Flexibility & Scalability - As much as my experience with cloud computing, It’s is very flexible, We can create servers within minutes. We can use or start any services with the few clicks only. The most impotent feature is Scalability. If our organization grows, we can add more service of upgrade existing services within a minute.

4. Pay-Per-Use Services - Most of cloud services providers offers to Pay-Per-Use services. it means there are no boundation of billing date time. Like if we started any server for 1 hours only, we need to pay for 1 hour only.

Reference http://tecadmin.net/

How to Install JAVA 7 (JDK 7u71) on RedHat 6/5

      During installation of Java using rpm and tar files I faced issues many times. After that I found a better way to install java from Sun site. We can also install multiple version of java easily if required.
Using below steps I have installed java successfully,

Step 1: Download Archive File
For 32 Bit –

              $ cd /usr/local/java

              $ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u71-b14/jdk-7u71-linux-i586.tar.gz"

For 64 Bit –

             $ cd /usr/local/java

             $ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u71-b14/jdk-7u71-linux-x64.tar.gz"

After completing download, Extract archive using following command. Use archive file as per your system configuration.
           
              $ tar -xzf jdk-7u71-linux-x64.tar.gz

Step 2: Setup Environment Variables

      After extracting java archive file, we just need to export the JAVA_HOME and AVA_BIN.  Most of java based application’s uses environment variables to work.
Use following commands to set up it.
Open .bash_profile from user home directory using following command copy the export JAVA_HOME path and save the .bash_profile and restart the putty session.

              $ vi .bash_profile
               export JAVA_HOME=/usr/local/java/jdk1.7.0_71
               PATH=$JAVA_HOME:$JAVA_HOME/bin:$PATH:$HOME/bin
               export PATH
Step 3: Check JAVA Version

Use following command to check which version of java is currently being used by system.
             
              # java -version

                      java version "1.7.0_71"
                      Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
                      Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

We are done with java installation on RedHat 6,  now we can start working with installed java.

For more information and reference check http://tecadmin.net/