Thursday, January 17, 2013

Java Tutorial

Java Tutorial

Java is an object oriented programming language, developed by James Gosling at Sun Microsystems and it is released in 1995. Now Oracle Corporation is taking care of java development and the new release is Java 7.

Origin of Java

James Gosling, Mike Sheridan, and Patrik Naughton started to develop a programming language for electronic consumer devices like interactive television. But the attempt was very advanced at that time and it was not successful. Initially the project was known as Oak, later renamed to Green and finally came to Java.

Version
Release
Codename
Changes
Comments
JDK 1.0
January 23, 1996
Oak

The first stable version – JDK 1.0.2 called Java1
JDK 1.1
February 19, 1997

Inner class, Java Beans, JDBC, RMI, reflection which supported Introspection, retooling of AWT

J2SE 1.2
December 8, 1998
Playground
Strictfp, swing, JIT Compiler, Java Plugin, Java IDL, Collection framework
Rebranded to Java 2
J2SE 1.3
May 8, 2000
Kestrel
Hotspot JVM, RMI with CORBA, JavaSound, JNDI, JPDA, Synthetic proxy classes.

J2SE 1.4
February 6, 2002
Merlin
Assert, regular expressions, exception chaining, IPv6 support, Non blocking NIO, logging API, Image IO API, Integrated XML parser and XSLT processor (JAXP), Integrated Security and cryptography extensions, Java Web start, Preferences API
First release of the Java platform developed under the Java Community Process.
Java 1.5
September 30, 2004
Tiger
Generics, Metadata/Annotations, Autoboxing/Unboxing, Varargs, Enhanced For each loop, Static imports, Automatic Stub generation of RMI objects, Swing-new sin look and feel called Synth, java.util.concurrent, Scanner class
Java 5.0
Java SE 6
December 11, 2006
Mustang
Scripting language support, Performance improvement, Webservice support through JAX- WS, JDBC 4.0, Java Compiler API, 

Java SE 7
July 28, 2011
Dolphin
JVM Support for dynamic languages, Compressed 64-bit pointers, Strings in switch, automatic resource management in try-statement, simplified varargs method invocation, allowing underscores in numeric literals, SCTP,
From Oracle.

Software Development Kit

Software development kit or SDK is a set of development tools that allows us for creating software applications or software packages.

Java Development Kit

JDK is Java SDK, it is an extended subset of SDK. This product aimed for java developers to create/run java programs.

Other than Sun (now Oracle) JDK, there are other JDKs commonly available for variety of platforms.

1.       GCJ from GNU (GNU Compiler for Java)
2.       IBM J9 JDK
3.       JRockit
4.       Blackdown Java
5.       Apple’s Mac OS  runtime Java

Along with JDK toolkit, the following features are available

1.       Java Development Kit
2.       Demos and sample examples
3.       Source code
4.       Public JRE
5.       Java DB

Public JRE is not needed for development purpose. A private JRE is available along with Java development kit.

Java Technology

Java technology is both a programming language and platform.

                Java Programming Language: It is a high level language

Java Program

Java program is a set of instructions or human readable character code to do a particular task. This program is storing in a file with a name and .java extension. There are three steps involved for the development of java program.

1.       Compose a Java Program
2.       Compile the Java program.
3.       Run the java class.

Compose a Java Program

Create a file with <filename>.java and write the set of instructions to do a particular task.

Sample ‘Helloworld’ Program

Open a notepad and write the following instructions

                class MyClass
                {
                                public  static void main(String[] args)
                                {
                                                System.out.println(“Hello World”);
                                }
                }

Save the file with “MyClass.java”

Each instruction in our program has certain meanings. We will cover all those later.

Compile the Java Program

To compile the above java program, we required a command tool ‘javac’. This javac is available with java development tool kit. Go to the java installation home directory, for example I have installed java into my C:\. Then go to C:\Program Files\Java\jdk1.7.0\bin and we can see ‘javac.exe’.

If we are copying our java program in this bin directory, then we can compile our java program using javac .
Open a command prompt window (Goto Windows Run and type ‘cmd’) and go to the bin directory(cd C:\Program Files\Java\jdk1.7.0\bin)
Then type,

javac MyClass.java

But if we are copying all our java files in bin directory, bin directory will be full of java files. It is not advisable to keep multiple kinds of files in same location. So if we need to compile the java program in user defined locations, we need to set the ‘PATH’ variable.

PATH is an environment variable, specifying a set of directories where executable programs are located.
We can set PATH variable in two ways.

  1. Setting PATH variable for each user sessions.
Open a command prompt and navigate to the directory where java program is created.
set PATH= C:\Program Files\Java\jdk1.7.0\bin;
  1. Setting PATH variable globally.
Right click on ‘My Computer’ and select ‘Properties’



                                Select ‘Advanced’ tab and click on ‘Environment Variables’.




We can see ‘User variables’ and ‘System Variables’.




If PATH variable is available in ‘User Variable’, then edit the PATH variable by adding ‘;’ and add the path (C:\Program Files\Java\jdk1.7.0\bin; ). Otherwise create a new user variable.




Now we can compile a java program from anywhere.
                Javac MyClass.java
If the program does not have any errors, command prompt returns. Otherwise it will display all the compilation errors. If any compilation errors, visit the program again, solve the issues and compile again till the command prompt returns without any error.
Run the Java class
After the successful compilation of the java program, check the directory. We can see a new file is generated.

                MyClass.class

This is a binary file which contains byte codes. While compiling, the java compiler compiles the java instructions and creates the byte codes. We can run this classfile using ‘java’ command. ‘java’ command is available in the same bin directory (C:\Program Files\Java\jdk1.7.0\bin)

                Java MyClass

Output is:
                Hello World


Examples

HelloWorld.java

public class HelloWorld {
       public static void main(String[] args)
       {
              System.out.println("Hello World");
       }
}
Output

Hello World

HelloToAll.java

public class HelloToAll {

       public static void main(String[] args) {
              System.out.println("Welcome to the Java World");
              System.out.println("I am teaching you Java");
              System.out.println("Please practice all the programs");
       }

}

Output

Welcome to the Java World
I am teaching you Java
Please practice all the programs

We can incorporate any number of system.out.println statements in a program.

HelloToLiterals.java

public class HelloToLierals
{
       public static void main(String[] args)
       {
              System.out.println("Hello To All");
              System.out.println('a');
              System.out.println(100);
              System.out.println(100.06);
              System.out.println(true);
       }

}
Output

Hello To All
a
100
100.06
True

Methods.java

public class Methods {

       public static void main(String[] args) {
              System.out.println("I am a main method. I am going to call test1 method");
              test1();
       }
       public static void test1()
       {
              System.out.println("I am a method");
       }

}

Output

I am a main method. I am going to call test1 method
I am a method

PremitivesAndString.java

public class PremitivesAndString {
       static int i;
       static byte b;
       static float f;
       static double d;
       static boolean bo;
       static short s;
       static long l;
       static char c;
       static String str;
      
       public static void main(String[] args) {
              System.out.println("Integer default Value ==> " + i);
              System.out.println("Byte default Value ==> " + b);
              System.out.println("Float default Value ==> " + f);
              System.out.println("Double default Value ==> " + d);
              System.out.println("Boolean default Value ==> " + bo);
              System.out.println("Short default Value ==> " + s);
              System.out.println("Long default Value ==> " + l);
              System.out.println("Character default Value ==> " + c);
              System.out.println("String default Value ==> " + str);
       }

}
Output
Integer default Value ==> 0
Byte default Value ==> 0
Float default Value ==> 0.0
Double default Value ==> 0.0
Boolean default Value ==> false
Short default Value ==> 0
Long default Value ==> 0
Character default Value ==> _
String default Value ==> null

Methodsmany.java

public class MethodsMany {
       static int i;
       public static void main(String[] args) {
              System.out.println("I am a Main Method. I am calling test1() method in many times.");
              test1();
              test1();
              test1();
              test1();
              System.out.println("I have called test1() method " + i + " Times");
       }
       public static void test1()
       {
              System.out.println("I am in a test method");
              i++;
       }

}

Output

I am a Main Method. I am calling test1() method in many times.
I am in a test method
I am in a test method
I am in a test method
I am in a test method
I have called test1() method 4 Times


Java Programming language – Data Types.

Java programming language is statically typed. Variables must first be declared before they use;

This involves

<Stating the type> <name> = <value>;

Eg: int i=10;
Java programming language supports eight primitive data types.
1.  byte
2.  short
3.  int
4.  long
5.  float
6.  double
7.  boolean
8.  char

Data Type
Default Value
Bits
Range
byte
0
8 bits
-128 to 127
short
0
16 bits
-32,768 to 32,767
int
0
32 bits

long
0L
64 bits

float
0.0f
32 bits

double
0.0d
64 bits

boolean
False

True and false
char
'\u0000'
16 bit
'\u0000' to '\uffff'

Global fields are declared and but not initialized will be set to a reasonable default by the compiler. Generally speaking, the default value will be either zero or null.

Local variables are slightly different. The compiler never assigns a default value to an uninitialized local variable. Accessing an uninitialized local variable will result in a compile time error.

UnInitializedLocalVariable.java

public class UnInitializedLocalVariable {

      
       public static void main(String[] args) {
              int i;
              System.out.println("I am using i, before initializing " + i);
       }

}

Compile time error: The local variable i may not have been initialized.

12 comments:

  1. I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.
    AWS Training in pune
    AWS Online Training

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I have found in quite some time. Nicely written and great information. thanks for sharing this informative blog. I will post a link to this page on my blog. I am sure my visitors will find that very useful. This is one of the best resources I have found in quite some time.

    Aws Training in Chennai

    Aws Training in Velachery

    Aws Training in Tambaram

    Aws Training in Porur

    Aws Training in Omr

    Aws Training in Annanagar

    ReplyDelete
  4. Are you looking for the best Azure training in Chennai? Here is the best suggestion for you, Infycle Technologies the best Software training institute in Chennai to study Azure platform with the top demanding courses such as Graphic Design and Animation, Cyber Security, Blockchain, Data Science, Oracle, AWS DevOps, Python, Big data, Python, Selenium Testing, Medical Coding, etc., with best offers. To know more about the offers, approach us on +91-7504633633, +91-7502633633

    ReplyDelete

http://wizpert.com/gireeshkumar