what is java technology (an introduction with comparision of .net coding)

46
1 By Shaharyar Khan [email protected] om

Upload: shaharyar-khan

Post on 11-Nov-2014

733 views

Category:

Education


4 download

DESCRIPTION

A introductory slides for those who want to learn and know some basics of Java.Also for those persons who want to compare coding difference between Java and .net

TRANSCRIPT

Page 1: What is Java Technology (An introduction with comparision of .net coding)

1

By Shaharyar Khan [email protected]

Page 2: What is Java Technology (An introduction with comparision of .net coding)

JAVA is a object oriented programming language with a built-in application programming interface (API) and also have support to handle graphics user interfaces and cater every programming challenge

Syntax of Java is inspired by C and C++

It’s more Platform Independent

It has a vast library of predefined objects and operations

Java manages the memory allocation and de-allocation for creating new objects. The program does not have direct access to the memory. The so-called garbage collector deletes automatically object to which no active pointer exists

2

Page 3: What is Java Technology (An introduction with comparision of .net coding)

Appeared in: 1995 Designed By: James Gosling and Sun MicroSystems Developer: Oracle Corporation Stable release: Java Standard Edition 7 Update 5

(1.7.5) (June 12, 2012; some days ago) Major Implementations: OpenJDK , SunJDK ,

OracleJDK Influenced by: Basically C and C++ Influenced: C# , Groovy ,J#, ada 2005 ,

Beanshell etc Operating System: Cross-platform License: GNU (General public license),

JAVA community process Extensions: .java , .class , .jar

3

Page 4: What is Java Technology (An introduction with comparision of .net coding)

Java compiler: Transform Java programs into Java byte code

Java byte code: Intermediate representation for Java programs

Java interpreter: Read programs written in Java byte code and execute them

Java virtual machine: Runtime system that provides various services to running programs

Java programming environment: Set of libraries that provide services such as GUI, data structures etc.

Java enabled browsers: Browsers that include a JVM + ability to load programs from remote hosts

4

Page 5: What is Java Technology (An introduction with comparision of .net coding)

The .class files generated by compiler are not executable binaries , So JAVA combine compilation and interpretation

Instead , They contain “byte-codes” to be executed by JVM

This Approach provides Platform Independence and greater security.

5

Page 6: What is Java Technology (An introduction with comparision of .net coding)

The Java Runtime Environment (JRE) is an implementation of the JVM (Java Virtual Machine) that actually executes our java programs.

Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc.

Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system

6

Page 7: What is Java Technology (An introduction with comparision of .net coding)

Java programs' execution speed improved significantly with the introduction of Just-In-Time complilation 1997/1998

the addition of language features supporting better code analysis (such as inner classes, StringBuffer class, optional assertions, etc.), and optimizations in the JVM itself

Some platforms offer direct hardware support for Java, there are microcontrollers that can run Java in hardware instead of a software Java Virtual Machine.

7

Page 8: What is Java Technology (An introduction with comparision of .net coding)

Everyone who want to work with JAVA should need to know about all Editions provided by JAVA

Mainly , JAVA provide these editions

JAVA Standard Edition a.k.a J2SE

JAVA Enterprise Edition a.k.a J2EE

JAVA Micro Edition a.k.a J2ME

Let’s have a look on all of these

8

•Micro Edition (ME)•Standard Edition (SE)•Enterprise Edition (EE)

Page 9: What is Java Technology (An introduction with comparision of .net coding)

J2se build under the umbrella of JAVA , which provide all core features provided by other programming languages.

• Java Basics• Classes and Objects• Utilities and Wrappers• Text Processing• Graphics Programming• Geometry• Graphics• Sequence Control• Packages

9

Page 10: What is Java Technology (An introduction with comparision of .net coding)

• Input Output• Collections• Interfaces and polymorphism• Inheritence• Exceptions• Threads• Reflection• GUIs and Event Handling• Inner and Adapter Classes

And Many more ……..

10

Page 11: What is Java Technology (An introduction with comparision of .net coding)

Conventions:• Classes• Keywords• Variables• Methods• Constants

11

Terms:• Instance Variables and Class varaibales• Reference Variables and Objects

1. test t; //only Ref2. new test(); //only obj available

for GC3. test t1 = new test(); //Properly reffered

object

Page 12: What is Java Technology (An introduction with comparision of .net coding)

These are some popular IDEs which are available for java

• Eclipse• NetBeans• BlueJ• JCreator• intelliJ IDEA• Borland JBuilder• Dr. JAVA

12

Page 13: What is Java Technology (An introduction with comparision of .net coding)

Lets Start discussion on J2se

13

Page 14: What is Java Technology (An introduction with comparision of .net coding)

Primitives (Exactly Same as in c#)

14

Type Contains Default Size Range

boolean true or false false 1 bit NA

char Unicode character unsigned

\u0000 16 bits or2 bytes

0 to 216-1 or \u0000 to \uFFFF

byte Signed integer 0 8 bit or 1 byte

-27 to 27-1 or -128 to 127

short Signed integer 0 16 bit or 2 bytes

-215 to 215-1 or -32768 to 32767

int Signed integer 0 32 bit or 4 bytes

-231 to 231-1 or -2147483648 to 2147483647

long Signed integer 0 64 bit or 8 bytes

-263 to 263-1 or -9223372036854775808 to 9223372036854775807

float IEEE 754 floating pointsingle-precision

0.0f 32 bit or 4 bytes

�1.4E-45 to �3.4028235E+38

double IEEE 754 floating point double-precision

0.0 64 bit or 8 bytes

�439E-324 to �1.7976931348623157E+308

Page 15: What is Java Technology (An introduction with comparision of .net coding)

Access Specifiers (A minor difference from c# )• public• protected• default (no specifier)• private

15

 Situation   public   protected   default   private 

 Accessible to class  from same package? 

yes yes yes no

 Accessible to class  from different package? 

yes  no, unless

it is a subclass 

no no

Page 16: What is Java Technology (An introduction with comparision of .net coding)

16

Page 17: What is Java Technology (An introduction with comparision of .net coding)

Example code for Access Specifiers

public String publicObj; private int privateObj; protected String protectedObj; String defaultObj; //Default

Same as for methods

public void publicMethod(); private void privateMethod(); protected void protectedMethod(); void defaultMethod(); //Default

17

Page 18: What is Java Technology (An introduction with comparision of .net coding)

18

abstract continue for new switch

assert*** default goto* packagesynchronized

boolean do if private this

break double implements protected throw

byte else import public throws

case enum**** instanceof return transient

catch extends int short try

char final interface static void

class finally long strictfp** volatile

const* float native super while* not used

**  added in 1.2

***  added in 1.4

****  added in 5.0

IMPORTANT:We can’t suggest a variable name same as any keyword of java like int catch; (it is wrong and it will generate an error)

Page 19: What is Java Technology (An introduction with comparision of .net coding)

19

Page 20: What is Java Technology (An introduction with comparision of .net coding)

20

}

Page 21: What is Java Technology (An introduction with comparision of .net coding)

21

Page 22: What is Java Technology (An introduction with comparision of .net coding)

22

In last slide , We have seen this line

package com.deltasoft.testpackage;

Same as declaring namespace in .net

Classes should build in packages so we can separate the code and keep code clean

At the end JVM can generate a jar file which contain all packages in compiled form.

Page 23: What is Java Technology (An introduction with comparision of .net coding)

23

Page 24: What is Java Technology (An introduction with comparision of .net coding)

24

I/O StreamsByte Streams: handle I/O of raw binary data.Character Streams: handle I/O of character data,

automatically handling translation to and from the local character set.

Buffered Streams: optimize input and output by reducing the number of calls to the native API.(both input, output as well as reader, writers)

I/O from the Command Line: describes the Standard Streams and the Console object.(ex: InputStreamReader)

Data Streams: handle binary I/O of primitive data type and String values.

Object Streams: handle binary I/O of objects.Basically for serialization

Page 25: What is Java Technology (An introduction with comparision of .net coding)

25

import java.io.FileReader; import java.io.FileWriter;import java.io.IOException;public class CopyCharacters {

public static void main(String[] args) throws IOException { FileReader inputStream = null;

FileWriter outputStream = null;try {

inputStream = new FileReader(“Myfile.txt"); outputStream = new FileWriter("characteroutput.txt");

int c; while ((c = inputStream.read()) != -1)

{ outputStream.write(c);

} } finally {

if (inputStream != null) { inputStream.close();

} if (outputStream != null)

{ outputStream.close();

} }

} }

Page 26: What is Java Technology (An introduction with comparision of .net coding)

26

These are core interfaces through which all data structres are inherited.

Page 27: What is Java Technology (An introduction with comparision of .net coding)

27

InterfacesHash table Implementations

Resizable array Implementations

Tree Implementations

Linked list Implementations

Hash table + Linked list Implementations

Set HashSet   TreeSet  LinkedHashSet

List   ArrayList   LinkedList  

Queue          

Map HashMap   TreeMap  LinkedHashMap

Page 28: What is Java Technology (An introduction with comparision of .net coding)

28

General-purpose implementations are the most commonly used implementations, designed for everyday use.

Special-purpose implementations are designed for use in special situations and display nonstandard performance characteristics, usage restrictions, or behavior.(Type Safe)

Concurrent implementations are designed to support high concurrency, typically at the expense of single-threaded performance. These implementations are part of the java.util.concurrent package.(also blocking/non blocking , concurrent etc)

Page 29: What is Java Technology (An introduction with comparision of .net coding)

29

Wrapper implementations are used in combination with other types of implementations, often the general-purpose ones, to provide added or restricted functionality.(We can say extended functionality of General Purpose Implementations)

Convenience implementations are mini-implementations, typically made available via static factory methods, that provide convenient, efficient alternatives to general-purpose implementations for special collections (for example, singleton sets).

Abstract implementations are skeletal implementations that facilitate the construction of custom implementations (for persistence or high performance)

Page 30: What is Java Technology (An introduction with comparision of .net coding)

30

import java.util.*; //All collection classes are present in java.util packagepublic class CollectionTest {

public static void main(String [] args) { System.out.println( "Collection Example!\n" ); int size; HashSet collection = new HashSet (); String str1 = "Yellow", str2 = "White", str3 = "Green", str4 =

"Blue"; Iterator iterator; collection.add(str1); collection.add(str2); collection.add(str3);collection.add(str4); System.out.print("Collection data: ");iterator = collection.iterator(); while (iterator.hasNext()){

System.out.print(iterator.next() + " "); } size = collection.size(); if (collection.isEmpty()){

System.out.println("Collection is empty"); } else{

System.out.println( "Collection size: " + size); }

} }

Page 31: What is Java Technology (An introduction with comparision of .net coding)

31

Only difference is extends keyword. We just replace “: “ with “extends”.Remaining things and concept are almost same rahter than one or two things which will be discuss in next slides.

Page 32: What is Java Technology (An introduction with comparision of .net coding)

32

Here is some difference in java and .net.

In .net , we can only override those methods which are declared with vritual keyword

But

In java , for our child classes all methods are available for overridding (of parent) and we haven’t need to specify any keyword to tell JVM that I will override this method.

Only those methods , which are declared final are not available for overriding to child classes.

Page 33: What is Java Technology (An introduction with comparision of .net coding)

33

Difference occurs while implementing In .net

class InterfaceImplementer :IMyInterface

In JAVA class InterfaceImplementer implements

IMyInterface

So , the difference is implements keyword.

Page 34: What is Java Technology (An introduction with comparision of .net coding)

34

Same like .net , JAVA does’t support multiple inheritence but supports multiple childs to be inherit.

class TradingSystem{public String getDescription(){return "electronic trading system";}}class DirectMarketAccessSystem extends TradingSystem{public String getDescription(){return "direct market access system";}}class CommodityTradingSystem extends TradingSystem{public String getDescription(){return "Futures trading system";} //This is basically a

polymorphism}

Page 35: What is Java Technology (An introduction with comparision of .net coding)

35

Exceptions concept and syntax are exactly same in JAVA and .net

try{

//code block}

catch(Exception ex){

//code block}

In java ,Exception is basically derived from class Throwable .Lets see the hierarchy of Exceptions.

Page 36: What is Java Technology (An introduction with comparision of .net coding)

36

JAVA supports Reported and unreported exception handling

Page 37: What is Java Technology (An introduction with comparision of .net coding)

37

ClassNotFoundException,AclNotFoundException, ActivationException, AlreadyBoundException, ApplicationException, AWTException, BackingStoreException, BadAttributeValueExpException,BadBinaryOpValueExpException, BadLocationException, BadStringOperationException, BrokenBarrierException, CertificateException,

DatatypeConfigurationException, DestroyFailedException, ExecutionException, ExpandVetoException, FontFormatException, GeneralSecurityException, NullPointerException,IllegalAccessException, ArrayIndexOutOfBoundException,IllegalArgumentException,TypeMisMatchException,InvalidApplicationException, InvalidMidiDataException,

Page 38: What is Java Technology (An introduction with comparision of .net coding)

38

Thread is a very vast topic even thousands of books have written on this topic

Here We are not going to discuss about what is Thread but only we will see what are the major differences in implementation and syntax of Threads between .net and JAVA

In .net We create Threads in this wayclass Launcher{

void Coundown() {lock(this) {

for(int i=4;i>=0;i--) {

Console.WriteLine("{0} seconds to start",i);

} Console.WriteLine("GO!!!!!");

} }

}

Page 39: What is Java Technology (An introduction with comparision of .net coding)

39

Class Demo{static void Main( string[] args){ Launcher la = new Launcher(); Thread firstThread = new Thread(new

ThreadStart(la.Coundown)); Thread secondThread =new Thread(new

ThreadStart(la.Coundown)); Thread thirdThread = new Thread(new

ThreadStart(la.Coundown)); firstThread.Start(); secondThread.Start(); thirdThread.Start();}

}

Page 40: What is Java Technology (An introduction with comparision of .net coding)

40

In JAVA , We have two ways to create a Thread

By implementing Runnable interfaceORBy extending Thread class

When We implement a interface , Then We have to create a Thread object in that class

When we extend our class from a Thread , Then we have to create object of our own class.

Let’s take a look in examples

Page 41: What is Java Technology (An introduction with comparision of .net coding)

41

Page 42: What is Java Technology (An introduction with comparision of .net coding)

42

class NewThread extends Thread { NewThread() { // Create a new, second thread

super("Demo Thread"); System.out.println("Child thread: " + this); start(); // Start the thread } // This is the entry point for the second thread.public void run() {

try { for(int i = 5; i > 0; i--) {

System.out.println("Child Thread: " + i);

// Let the thread sleep for awhile.

Thread.sleep(500); }

} catch (InterruptedException e) { System.out.println("Child interrupted.");

} System.out.println("Exiting child thread."); }

}

Page 43: What is Java Technology (An introduction with comparision of .net coding)

43

class ExtendThread { public static void main(String args[]) {

new NewThread(); // create a new thread try {

for(int i = 5; i > 0; i--) { System.out.println("Main Thread: "

+ i); Thread.sleep(1000); }

} catch (InterruptedException e) { System.out.println("Main thread

interrupted."); }

System.out.println("Main thread exiting."); }

}

Page 44: What is Java Technology (An introduction with comparision of .net coding)

44

Reflection and Serialization is also a very important features provided by JAVA

There purpose and implementations are same as in c#

Serialization provide us Data persistence through out the network

Reflection is use for reverse engineering

Page 45: What is Java Technology (An introduction with comparision of .net coding)

45

We have looked into some core things that are compulsory to start work with JAVA programming.

Now , after interpreting these things anyone can easily explore JAVA features

Page 46: What is Java Technology (An introduction with comparision of .net coding)

46