Core Java Introduction Byju Veedu Ness Technologies...

Post on 17-Jan-2016

235 views 2 download

transcript

Core JavaIntroduction

Byju Veedu Ness Technologies

httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html

Java is

Simple: Small language, Small Interpreter(40k) and large libraries Object Oriented: Supports

encapsulation,inheritance,abstraction and polymorphism. Architecture neutral: Java source files are compiled to byte

codes and are interpreted by the jvm. Portable: Primitive data types and their behaviour are specified by

the language Libraries define portable interfaces. Distributed: Libraries for network programming and remote

method invocation. Multithreaded: Threads are easier to create and use. Secure: Sandbox model, java byte code verification. Automatic memory management :Garbage collection

Architecture

• http://www.artima.com/insidejvm/ed2/introarch.html

Java Virtual Machine

Load class files and execute the byte codes. Class files from java api that are needed by the program

are loaded into JVM. Byte codes are executed in execution engine

Class Loader

Execution Engine

Host Operating Systema

Program class files Java api class files

Class loader• Bootstrap class loader

Loads the core Java libraries[5] (<JAVA_HOME>/lib directory). This class loader, which is part of the core JVM, is written in native code.

• Extensions class loader loads the code in the extensions directories (<JAVA_HOME>/lib/ext or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

• System class loader The system class loader loads code found on java.class.path, which maps to the system CLASSPATH variable. This is implemented by the

sun.misc.Launcher$AppClassLoader class. • User Defined class loaders

By default, all user classes are loaded by the default system class loader, but it is possible to replace it by a user-defined ClassLoader.

Garbage collectionAutomatically removes unused objects from memory and thus developer need not explicitly free the memory of the objects used

Benefits• Avoids memory leakage• Avoid memory corruption• Increase productivity

Installation• Download and install Java sdk for your Operating system• Set JAVA_HOME environment variable to be the path of home

directory of your java installation• Add %JAVA_HOME%/bin to path environment variable in

windows• Type java –version in your command line will give details of your

java installation.

Hello worldpackage com.training;

class HelloWorldApp {

public static void main(String[] args) { System.out.println("Hello World!"); // Display

the message.

}

}

Running the program

• Create a source file(.java file)A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files. It should be .java file and the name of the file should match the name of the public class in the file. There can be only one public class in a java source file.

• Compile the source file into a .class fileThe Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.

Javac HelloworldApp.java This will create a HelloworldApp.class file in the package specified in the java file.

• Run the programThe Java application launcher tool (java) uses the Java virtual machine to run your application. Java HelloworldApp

• HelloWorldApp, will simply display the greeting "Hello world!".

Data Types Integral

byte shortintlong

TextualcharString

Floating Point typesfloatdouble

Boolean Typetruefalse

ArraysA fixed length structure that stores multiple values of the same type.

// Creating an array of type String of length 10String [] names =new String[10]names[0]=“Kumar”;names[1]=“Raj”;// Print array valuesfor(int i=0;i<names.length;i++){System.out.println(names[i]);}// multi dimentionalint [][] matrix = new int[4];matrix[0] = new int[10];matrix[1]=new int[2-0];System.out.println(“first element :”+matrix[0][0]);

Why java?

OOP simplified (compared to C/C++). Multithreading support. A good collection library. Platform independence. JVM and automatic memory management Security with sandbox model Comparatively good performance.

Hands-on

• Hello world program• Use of PATH and CLASSPATH

Create a class and add to classpath and try to use it.

Create a jar and add to ext\lib and use it.• Input and output through console.

(Write a program to accept two numbers and to print the result)

• Use of different data types

(Program a calculator which supports four basic operations)• Using Arrays

Accept names separated by space in a line and print the count.

• Jar creation

References

• http://download.oracle.com/javase/tutorial/tutorialLearningPaths.html

• http://wikis.sun.com/display/code/Home• http://download.oracle.com/javase/tutorial/deployment/jar/

index.html• http://download.oracle.com/javase/tutorial/essential/

environment/paths.html• http://arhipov.blogspot.com/2011/01/java-bytecode-

fundamentals.html• http://onjava.com/pub/a/onjava/2003/11/12/classloader.html• http://www.artima.com/insidejvm/ed2/• http://www.youtube.com/watch?v=QMV45tHCYNI&feature=channel