Java 7 language features - Try with Resources statement

Hi Friends,

Java has come up with really cool features in version 1.7.Code was named as Dolphin and released on July 28,2011.If you are interested in knowing history of all Java releases,you can go throw my earlier post History of java language.                                      


How to Install Java/JDK on your windows machine

Dear Friends,

Let  us see how we can install Java on a windows machine.I am taking here an example of 32 bit Windows 7 machine and the version of Java that we are going to install here is 1.6

Step 1 : Go to Oracle site .I am pasting here the direct link from where Java6 exe can be downloaded.

http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html#jdk-6u45-oth-JPR

Search for file name -> jdk-6u38-windows-i586.exe and download this file to your local drive.



 Step 2 : Double click on this file and following screen will open .Click on Yes.



Step 3 : An installation wizard will open as can be seen in below screenshot.Click on Next.


Step 4 : On the below screen,wizard is trying to install Java by default in C:\Program Files\Java\jdk1.6.0_38 , but if you want to change the path ,you can change here by clicking on Change... button and giving path wherever you want to install Java/Jdk. Also as you can see there should be at least 300 MB of space available for installation ,so make sure you have necessary space available.If you are fine with path and size ,Click on Next.


Step 5 : It will take some time to copy files to required path and finally you will see following screen,where it will ask for installation of JRE. You can change path for JRE installation as well by clicking on Change... button and giving your path.Click on Next


Step 6: You will see following screens.Finally click on finish on second screenshot below



Step 7 : This is time to check whether installation has happened actually or not.Go to C:\Program Files\Java and you will see following.Both JDK and JRE have been installed.


Hope this would have helped you.Your suggestions,feedback ,any further questions are welcome.


Adding Custom templates in Eclipse for faster Java development

Dear Friends,

As we saw in the last tutorial Eclipse templates for faster java development that eclipse provides some inbuilt templates which we can use to expedite the coding.Apart from that we can create our own custom templates as well for the code which we find repetitive in nature and thus can save our precious time.

So to create a new custom template ,yo need to go to Windows->Preferences



Type Java in the search box and then go to Editor ->Templates


You will see list of inbuilt templates provided by eclipse already.

To add your own template,click on New...


In the Name ,give name which you want to give to the shorthand that will be visible when you will press Ctrl + Space

In Description ,you can mention briefly what this shorthand is about

Context is Java

Automatically insert if checked will automatically insert the code snippet for you on writing shorthand word and pressing ctrl + space,if there is no other matching template is available.
For example,if you write sysout and press ctrl+space,it will write System.out.println(); for you  when automatically Insert is checked.However,if automatically Insert is not checked ,then template proposal will be displayed ,which you need to select then

Pattern : This is the place where your template goes.

I am taking here four examples
1.List Iterator
2.Map Iterator
3.Logger
4.NullCheck



1.List Iterator

Name          : listIterator
Description : iterate over list
Context       : Java
Pattern        :

Iterator<${type}> itr = listVar.iterator();
while(itr.hasNext()){
${type} str = (${type})itr.next();
}



2. Map Iterator :

Name          : mapIterator
Description : iterate over map
Context       : Java
Pattern        :

for (Entry<${keyType:argType(map, 0)},${valueType:argType(map, 1)}> entry : ${map:var(java.util.Map)}.entrySet()) {
  ${keyType} key = entry.getKey();
  ${valueType} value = entry.getValue();
  ${cursor}
}



3. Logger :

Name          : logger
Description : logger Entry
Context       : Java
Pattern        :
private static final Logger logger = Logger.getLogger(${enclosing_type}.class);


4)  Null Check

Name          : NullCheck
Description : check for null value
Context       : Java
Pattern        :

 if (${var} != null) {
 ${line_selection}${cursor}
}



Java editor template variables

There are two types of variables which can be used while writing patterns for java code.These variables are resolved to their actual value when a template is evaluated.

Two types of variables
1) General template variable
2) Java specific template variable

We can use variables in two ways as follow :

Simple variables as below :
${array}

This defines a variable with name array,which can be referenced multiple times and that will resolve to array.

Full variables as below :
${array:var(java.util.iterator)}

This defines a variable with name as array that will resolve to local variable of type java.util.Iterator.It can be referenced multiple times by giving its name without type like ${array}

1) General template variable

VariableDescription
${cursor}Specifies the cursor position when the template edit mode is left. This is useful when the cursor should jump to another place than to the end of the template on leaving template edit mode.
${date}Evaluates to the current date.
${dollar}Evaluates to the dollar symbol $. Alternatively, two dollars can be used: $$.
${enclosing_method}Evaluates to the name of the enclosing method.
${enclosing_method_arguments}Evaluates to a comma separated list of argument names of the enclosing method. This variable can be useful when generating log statements for many methods.
${enclosing_package}Evaluates to the name of the enclosing package.
${enclosing_project}Evaluates to the name of the enclosing project.
${enclosing_type}Evaluates to the name of the enclosing type.
${file}Evaluates to the name of the file.
${line_selection}Evaluates to content of all currently selected lines.
${primary_type_name}Evaluates to the name primary type of the current compilation unit.
${return_type}Evaluates to the return type of the enclosing method.
${time}Evaluates to the current time.
${user}Evaluates to the user name.
${word_selection}Evaluates to the content of the current text selection.
${year}Evaluates to the current year.

2) Java specific template variables


VariableDescription
${array}Evaluates to a proposal for an array visible in the current scope.
${array_element}Evaluates to a name for a new local variable for an element of the ${array} variable match.
${array_type}Evaluates to the element type of the ${array} variable match.
${collection}Evaluates to a proposal for a collection visible in the current scope.
${exception_variable_name}Exception variable name in catch blocks.
${index}Evaluates to a proposal for an undeclared array index.
${iterator}Evaluates to an unused name for a new local variable of type java.util.Iterator.
${iterable}Evaluates to a proposal for an iterable or array visible in the current scope.
${iterable_element}Evaluates to a name for a new local variable for an element of the ${iterable} variable match.
${iterable_type}Evaluates to the element type of the ${iterable} variable match.
${todo}Evaluates to a proposal for the currently specified default task tag.


Any suggestions,feedback,questions on the post are welcome.





History of Java language

Dear  Friends,

Let us see evaluation of one of the most used programming language today i.e Java.

  • Java language project was started in June 1991 by team of engineers in Sun microsystem including James Gosling, Mike Sheridan, and Patrick Naughton known by Green Team and lead by James Gosling.
    James Gosling
  • Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time.But it was just right for the Internet, which was just starting to take off. In 1995, the team announced that the Netscape Navigator Internet browser would incorporate Java technology.
  • The language was initially called Oak after an Oak tree that stood outside Gosling's office. However they could not continue with this name as it was already trademarked by Oak Technologies.
  • After lot of brainstorming,team came up with few names  like DNA,Silk,Java,Lyric,pepper,Neon,Dynamic,Jolt,revolutionary and few others.
  • DNA,Silk and Java were among shortlisted names and finally Java was selected as final name from Java Coffee.
  • Sun Microsystems released the first public implementation as Java 1.0 in 1995.It promised "Write Once, Run Anywhere" (WORA), providing no-cost run-times on popular platforms.  Major web browsers soon incorporated the ability to run Java Applets within web pages, and Java quickly became popular. The Java 1.0 compiler was re-written in Java by Arthur van Hoff to comply strictly with the Java 1.0 language specification.
  • Today, Java not only permeates the Internet, but also is the invisible force behind many of the applications and devices that power our day-to-day lives. From mobile phones to handheld devices, games and navigation systems to e-business solutions, Java is everywhere!

Here are the history of all Java releases with language features included in each release.

Version 1.0
Code named Oak and released on January 23, 1996


Version 1.1
Released on February 19, 1997
New features in JDK 1.1
JDBC (Java Database Connectivity)
Inner Classes
Java Beans
RMI (Remote Method Invocation)
Reflection (introspection only)

Version 1.2
Code named Playground and released on December 8, 1998.
New features in J2SE 1.2
Collections framework.
Java String memory map for constants.
Just In Time (JIT) compiler.
Jar Signer for signing Java ARchive (JAR) files.
Policy Tool for granting access to system resources.
Java Foundation Classes (JFC) which consists of Swing 1.0, Drag and Drop, and Java 2D class libraries.
Java Plug-in
Scrollable result sets, BLOB, CLOB, batch update, user-defined types in JDBC.
Audio support in Applets.

Version 1.3
Code named Kestrel and released on May 8, 2000
New features in J2SE 1.3
Java Sound
Jar Indexing
A huge list of enhancements in almost all the java area.

Version 1.4
Code named Merlin and released on February 6, 2002 (first release under JCP).
New features in J2SE 1.4
XML Processing
Java Print Service
Logging API
Java Web Start
JDBC 3.0 API
Assertions
Preferences API
Chained Exception
IPv6 Support
Regular Expressions
Image I/O API

Version 1.5
Code named Tiger and released on September 30, 2004
New features in J2SE 5.0
Generics
Enhanced for Loop
Autoboxing/Unboxing
Typesafe Enums
Varargs
Static Import
Metadata (Annotations)
Instrumentation

Version  1.6
Code named Mustang and released on December 11, 2006.
New features in Java SE 6
Scripting Language Support
JDBC 4.0 API
Java Compiler API
Pluggable Annotations
Native PKI, Java GSS, Kerberos and LDAP support.
Integrated Web Services.
Lot more enhancements.

Version 1.7
Code named Dolphin and released on July 28, 2011
New features in Java SE 7
Strings in switch Statement
Type Inference for Generic Instance Creation
Try with Resources
Multiple Exception Handling
Re-throwing exceptions with more inclusive type checking
Underscore in literals
Binary Literals
Java nio Package

Version 1.8
Lambdas and Functional Interfaces
Method parameter Reflection
Repeating annotations
Improved Type Inference
Extended Annotations Support


Any feedback ,suggestions ,questions on the post are welcome.

How Java is Platform Independent

Anyone who starts learning Java will agree that the first thing they will hear is that Java is Platform Independent language.

SO WHAT DOES THAT MEAN BY PLATFORM INDEPENDENT.

For this to understand ,first we need to understand what do we mean by platform.

Platform = Hardware + Operating System

So that means when we say Java is platform independent ,then we mean that code written in Java language can be executed on any of the computer with different  hardware and operating system.
So same java code can run on machine with Windows on it and it will run fine on machine with Linux.

So the next question is how Java achieve platform Independence?

Answer lies here.

When we compile our java code(.java file) using Java compiler,java compiler will convert this java code to byte code in the form of .class file.Normally in other languages like C, the compiled code is the exact set of instructions the CPU requires to execute the program,however in Java, this compiled code is a set of instructions for a virtual CPU which is required to work the same on every physical machine.This virtual CPU or virtual machine we call as Java virtaul machine or JVM.



Since the JVM must translate this bytecode into machine language, and since the machine language depends on the operating system being used, we can say  that  JVM is platform dependent.in other words, the JVM is not platform independent.So its different JVMs for different operating systems which make Java Platform Independent.There are standard specifications published by Oracle Corporation which each vendor who is implementing JVM needs to follow,such that each JVM would be able to understand the byte code.


Any feedback ,suggestions ,questions on the post are welcome.