Introduction to JAVA
Md. Tanvir Hossain
Student, Dept of CSE
Daffodil International University
1
Features of Java : Simple
 Intentionally created to be syntactically similar to C/C++
 Eliminate traditionally troublesome features of C/C++
• Pointer arithmetic (no troublesome * and &)
• Multiple inheritance
• Implicit type coercions // if (1) {x = 1 ;} is not well-formed
• Explicit memory management
• Preprocessor
2
Features of Java : Simple
 Eliminate features of C/C++
• struct, typedef,union
• generic, enum (both recovered since jdk5.0)
• (Programmer controlled) operator overloading (but preserving
method overloading)
 Features included as part of base language:
• Threads
• Exception handling
• reflection
3
Features of Java : Object Oriented
 Java is inherently object-oriented.
• Although many object-oriented languages began strictly as
procedural languages, Java was designed from the start to be
object-oriented.
 Object-oriented programming (OOP) is a popular
programming approach that is replacing traditional
procedural programming techniques.
4
Features of Java: Object Oriented
 One of the central issues in software development is how to
reuse code.
• Object-oriented programming provides great flexibility,
modularity, clarity, and reusability through encapsulation,
inheritance, and polymorphism.
5
What is object-oriented ?
 Systems are built from sets of classes
 Classes are instantiated at runtime to give objects
 Objects communicate via messages passing (i.e., method or
function invocation)
 (In java) everything is part of a class
6
 Supported OO Concepts:
• Data abstraction and Encapsulation
• Inheritance
• Polymorphism
– dynamic binding - overriding
– overloading
– generics
 Logical cluster of classes == package
7
Features of Java: Object Oriented
Features of Java : Distributed
 Distributed computing involves several computers working
together on a network.
 Java is designed to make distributed computing easy.
 Since networking capability is inherently integrated into
Java, writing network programs is like sending and
receiving data to and from a file.
8
Features of Java : Distributed
 Network programming is supported by built in JDK class
library:
• TCP sockets ,UDP packets ,IP addresses
• URLs ,RMI (Remote Method Invocation)
• Web Service
 Security features designed into language
9
 Language implementation
• Compiler – produce machine code
– Ex: Fortran, Cobol, C, C++
• Interpreter – execute source or compiled instructions on a virtual
machine
– Ex: Scheme, Haskell, Python., Java
 Hybrid compilation/interpretation : Java
10
Features of Java : Interpreted Language
 The Java Virtual Machine (JVM)
java code byte code (machine independent)
[ just-in-time compilation native code ] execution
 We need an interpreter(JVM) to run Java programs.
• The bytecode is machine-independent and can be executed on any
machine that has a Java interpreter, which is the Java Virtual
Machine (JVM).
11
Features of Java : Interpreted Language
compile
 characteristics of Java
• Strongly-typed language (cf Smalltalk and VisualBasic)
• Compile-time and runtime checking
• No pointer arithmetic
• Exception handling
• Automatic memory management
12
Features of Java : Robust
 Java compilers can detect many problems that would first
show up at execution time in other languages.
 Java eliminated certain types of error-prone programming
constructs found in other languages.
 Runtime exception-handling feature provides
programming support for robustness.
13
Features of Java : Robust
 Designed with security in mind.
• Java implements several security mechanisms to protect your
system against harm caused by stray programs.
• Allow users to download untrusted code over a network and run it in a
secure environment in which it cannot do any harm.
 Configurable security levels and restrictions.
14
Features of Java : Secure
 Subjected to intense scrutiny by security experts with
[potentially serious ] bugs found and fixed.
• become a big news if new bugs found!!
• A recent security flaw(2012/08) (0-day flaw)
 One of the best mainstream platforms with the strongest
security guarantee.
15
Features of Java : Secure
 Byte-codes are architecture neutral
• Write once, run anywhere
• With a Java Virtual Machine (JVM), you can write one program
that will run on any platform.
 Performance suffers somewhat by using bytecodes
16
Features of Java : Architecture Neutral
 Source code :
• Primitive type sizes are explicit - not architecture dependent
• Strings and characters are (16-bit) Unicode compliant
– easier for internationalization.
 Byte code:
• run everywhere where a JVM is available
 User experience :
• GUI libraries give a native graphic library-independent mechanism
for creating quality graphical interfaces (sort of)
• "They gave us a library that is good for writing programs that look
equally mediocre on the different systems."
17
Features of Java : Portable
 Interpreting leads to quicker development cycle
 Depends what you compare it to
• "Slightly faster than VB" - (Core Java, page 9)
• JITC(Just-In-Time Compiler) help greatly in this respect
• Sun’s Java HotSpot is Newest high performace JIT compiler.
 Can use native code for mission-critical performance
sections of code
• JNI: Java Native Interface
• Sacrifice portability.
18
Features of Java : High Performance
 Based on well-known 20 year old Hoare monitor
synchronization
 Thread support built into language
 Thread synchronization primitives supplied
 Garbage collector runs permanently as a low priority
background thread
 Issue:
• Still hard to use right for general programmers:
• New package java.util.concurrency and other extensions added
since jdk5 allow easier and correct use of threads.
19
Features of Java : Multithreaded
 Class linking, layout, name resolution and object references
not resolved until run-time
 Runtime Type Information (RTTI) available
• Can check the type of objects at run-time
• java.reflect.* package
 Class class for dynamic instantiation
• Can create objects of types unkown until runtime.
String name = getSpecies(); // get a name of a class
Object p = Class.forName(name).newInstance();
if(p instanceof Hen) {…}
else if(p instanceof Coq) { …}
else {… }
20
Features of Java : Dynamic
 Green Project (1990)
• need a small PL for developing consumer device operating software
• Requirements: small size, robust, portable, extremely reliable ,real-
time performance
 Oak // the resulting language
• Originally used C++, then realized a new language was needed
• Original requirements same as for current language
 Java (1993) // OAK was renamed Java
• Intended market never eventuated (no technology/product sold)
• WWW starting to takeoff
• Language design "based on" many current OO languages (e.g.,
C++, Eiffel, Smalltalk, Cedar/Mesa, Objective C)
• 1995/5/23 Sun launched Java
 JDK 1.0 released early 1996/1/23 ( 211 classes / 8 pkgs) 21
Early history of Java
Evolution of the Java Language
22
Java Jargon
23
How JAVA code are structured
 A java application consists of
• many packages
• Some from JRE (built-ins)+ some from 3rd party + your own ]
 A package consists of
• 0 or more classes
• 0 or more subpackages
 A java source file xxx.java is called a compilation unit,
which may
• contain multiple java class definitions and
• may be compiled into multiple java byte code (***.class ).
24
Package and class naming
 A class or package has two names:
• simpleName
• fully qualified name = parentPackageName . simpleName
• packageName = parentPackageName . simpleName
• For package, we use only its full name.
– Ex:
– java.lang.String
– javax.swing.JButton
25
Package and class naming
 The package/subpackage/(*.class, *.java) structure is not
only logically analogous to directory/ subdirectory/ file in
OS file system but in fact they are stored physically in file
system in this way.
• Namely, if package p is located in a directory d then subpackage p.q
of p would be stored in subdirecty q of d, and class p.A of p would
be stored in file named A.class or A.java.
26

Introduction to JAVA

  • 1.
    Introduction to JAVA Md.Tanvir Hossain Student, Dept of CSE Daffodil International University 1
  • 2.
    Features of Java: Simple  Intentionally created to be syntactically similar to C/C++  Eliminate traditionally troublesome features of C/C++ • Pointer arithmetic (no troublesome * and &) • Multiple inheritance • Implicit type coercions // if (1) {x = 1 ;} is not well-formed • Explicit memory management • Preprocessor 2
  • 3.
    Features of Java: Simple  Eliminate features of C/C++ • struct, typedef,union • generic, enum (both recovered since jdk5.0) • (Programmer controlled) operator overloading (but preserving method overloading)  Features included as part of base language: • Threads • Exception handling • reflection 3
  • 4.
    Features of Java: Object Oriented  Java is inherently object-oriented. • Although many object-oriented languages began strictly as procedural languages, Java was designed from the start to be object-oriented.  Object-oriented programming (OOP) is a popular programming approach that is replacing traditional procedural programming techniques. 4
  • 5.
    Features of Java:Object Oriented  One of the central issues in software development is how to reuse code. • Object-oriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism. 5
  • 6.
    What is object-oriented?  Systems are built from sets of classes  Classes are instantiated at runtime to give objects  Objects communicate via messages passing (i.e., method or function invocation)  (In java) everything is part of a class 6
  • 7.
     Supported OOConcepts: • Data abstraction and Encapsulation • Inheritance • Polymorphism – dynamic binding - overriding – overloading – generics  Logical cluster of classes == package 7 Features of Java: Object Oriented
  • 8.
    Features of Java: Distributed  Distributed computing involves several computers working together on a network.  Java is designed to make distributed computing easy.  Since networking capability is inherently integrated into Java, writing network programs is like sending and receiving data to and from a file. 8
  • 9.
    Features of Java: Distributed  Network programming is supported by built in JDK class library: • TCP sockets ,UDP packets ,IP addresses • URLs ,RMI (Remote Method Invocation) • Web Service  Security features designed into language 9
  • 10.
     Language implementation •Compiler – produce machine code – Ex: Fortran, Cobol, C, C++ • Interpreter – execute source or compiled instructions on a virtual machine – Ex: Scheme, Haskell, Python., Java  Hybrid compilation/interpretation : Java 10 Features of Java : Interpreted Language
  • 11.
     The JavaVirtual Machine (JVM) java code byte code (machine independent) [ just-in-time compilation native code ] execution  We need an interpreter(JVM) to run Java programs. • The bytecode is machine-independent and can be executed on any machine that has a Java interpreter, which is the Java Virtual Machine (JVM). 11 Features of Java : Interpreted Language compile
  • 12.
     characteristics ofJava • Strongly-typed language (cf Smalltalk and VisualBasic) • Compile-time and runtime checking • No pointer arithmetic • Exception handling • Automatic memory management 12 Features of Java : Robust
  • 13.
     Java compilerscan detect many problems that would first show up at execution time in other languages.  Java eliminated certain types of error-prone programming constructs found in other languages.  Runtime exception-handling feature provides programming support for robustness. 13 Features of Java : Robust
  • 14.
     Designed withsecurity in mind. • Java implements several security mechanisms to protect your system against harm caused by stray programs. • Allow users to download untrusted code over a network and run it in a secure environment in which it cannot do any harm.  Configurable security levels and restrictions. 14 Features of Java : Secure
  • 15.
     Subjected tointense scrutiny by security experts with [potentially serious ] bugs found and fixed. • become a big news if new bugs found!! • A recent security flaw(2012/08) (0-day flaw)  One of the best mainstream platforms with the strongest security guarantee. 15 Features of Java : Secure
  • 16.
     Byte-codes arearchitecture neutral • Write once, run anywhere • With a Java Virtual Machine (JVM), you can write one program that will run on any platform.  Performance suffers somewhat by using bytecodes 16 Features of Java : Architecture Neutral
  • 17.
     Source code: • Primitive type sizes are explicit - not architecture dependent • Strings and characters are (16-bit) Unicode compliant – easier for internationalization.  Byte code: • run everywhere where a JVM is available  User experience : • GUI libraries give a native graphic library-independent mechanism for creating quality graphical interfaces (sort of) • "They gave us a library that is good for writing programs that look equally mediocre on the different systems." 17 Features of Java : Portable
  • 18.
     Interpreting leadsto quicker development cycle  Depends what you compare it to • "Slightly faster than VB" - (Core Java, page 9) • JITC(Just-In-Time Compiler) help greatly in this respect • Sun’s Java HotSpot is Newest high performace JIT compiler.  Can use native code for mission-critical performance sections of code • JNI: Java Native Interface • Sacrifice portability. 18 Features of Java : High Performance
  • 19.
     Based onwell-known 20 year old Hoare monitor synchronization  Thread support built into language  Thread synchronization primitives supplied  Garbage collector runs permanently as a low priority background thread  Issue: • Still hard to use right for general programmers: • New package java.util.concurrency and other extensions added since jdk5 allow easier and correct use of threads. 19 Features of Java : Multithreaded
  • 20.
     Class linking,layout, name resolution and object references not resolved until run-time  Runtime Type Information (RTTI) available • Can check the type of objects at run-time • java.reflect.* package  Class class for dynamic instantiation • Can create objects of types unkown until runtime. String name = getSpecies(); // get a name of a class Object p = Class.forName(name).newInstance(); if(p instanceof Hen) {…} else if(p instanceof Coq) { …} else {… } 20 Features of Java : Dynamic
  • 21.
     Green Project(1990) • need a small PL for developing consumer device operating software • Requirements: small size, robust, portable, extremely reliable ,real- time performance  Oak // the resulting language • Originally used C++, then realized a new language was needed • Original requirements same as for current language  Java (1993) // OAK was renamed Java • Intended market never eventuated (no technology/product sold) • WWW starting to takeoff • Language design "based on" many current OO languages (e.g., C++, Eiffel, Smalltalk, Cedar/Mesa, Objective C) • 1995/5/23 Sun launched Java  JDK 1.0 released early 1996/1/23 ( 211 classes / 8 pkgs) 21 Early history of Java
  • 22.
    Evolution of theJava Language 22
  • 23.
  • 24.
    How JAVA codeare structured  A java application consists of • many packages • Some from JRE (built-ins)+ some from 3rd party + your own ]  A package consists of • 0 or more classes • 0 or more subpackages  A java source file xxx.java is called a compilation unit, which may • contain multiple java class definitions and • may be compiled into multiple java byte code (***.class ). 24
  • 25.
    Package and classnaming  A class or package has two names: • simpleName • fully qualified name = parentPackageName . simpleName • packageName = parentPackageName . simpleName • For package, we use only its full name. – Ex: – java.lang.String – javax.swing.JButton 25
  • 26.
    Package and classnaming  The package/subpackage/(*.class, *.java) structure is not only logically analogous to directory/ subdirectory/ file in OS file system but in fact they are stored physically in file system in this way. • Namely, if package p is located in a directory d then subpackage p.q of p would be stored in subdirecty q of d, and class p.A of p would be stored in file named A.class or A.java. 26