You are on page 1of 24

Lecture 1

§  


 






  

2/11/2011 IICT, BUET 1


Java¶s lineage
˜ Java is related to C++, which is direct
descendent of C
˜ From C, Java derives its syntax
˜ Many of Java¶s object-oriented features
were influenced by C++
˜ OOP is a programming methodology that
helps organize complex programs through
the use of inheritance, encapsulation and
polymorphism

2/11/2011 IICT, BUET 2


The creation of JAVA
˜ Java was developed at Sun Microsystems at 1991,
after an effort of 18 months
˜ The original impetus for Java was not Internet!
Instead the primary motivation was the need for a
platform-independent language that could be used
to create software to be implemented in various
consumer electronic devices, such as microwave
ovens and remote controls
˜ The trouble with other languages is that they are
designed to be complied for a specific target

2/11/2011 IICT, BUET 3


The creation of JAVA (Contd.)
˜ We know, compilation of a language requires a compiler
targeted for that CPU, which is expensive and time
consuming to create (.i.e windows and linux C compiler)
˜ So effort was given to develop a platform-independent
language that could be used to produce code that would
run on a variety of CPUs under differing environments.
The ultimate result is Java
˜ A second force worked behind the creation of Java, that is
the emergence of World Wide Web or Internet
˜ The code for internet required portability and this
realization caused the focus of Java to switch from
consumer electronics to Internet programming

2/11/2011 IICT, BUET 4


Why Java is Important to the Internet
˜ In a network, two very broad categories of objects are
transmitted between server and your PC
˜ passive information (i.e. email, program¶s code)
˜ Dynamic, active programs (i.e. self-executing
program). Such a program is an active agent on the
client computer, yet is initiated by the server (i.e.
applets)
˜ An applet is an application designed to be transmitted
(dynamically downloaded) over the Internet and executed
by a Java-compatible Web browser.
˜ Applet is an intelligent program (not just an animator or
image file or media file) that can react to user input and
dynamically change
2/11/2011 IICT, BUET 5
Security
˜ Whenever you download a program, you are risking a
viral infection
˜ So most users don¶t download executable programs or
scan them prior to execution
˜ Another type of malicious programs gather private
information (i.e. credit card numbers, bank account
balances, passwords etc)
˜ Java provides a ³firewall´ between a networked
application and your computer
˜ Also Java achieves the protection by confining a Java
program to the Java execution environment and not
allowing it access to other parts of the computer

2/11/2011 IICT, BUET 6


Java¶s Magic: The Bytecode
˜ The secret behind the security and
portability of Java is that the output of a
Java compiler is not executable code.
Rather, it is bytecode
˜ Ê  is a highly optimized set of
instructions designed to be executed by the
Java run-time system, which is called the

    

˜ JVM is an      

2/11/2011 IICT, BUET 7


Basics of a Typical Java Environment
˜ Java systems contain
˜ Environment
˜ Language
˜ APIs
˜ Class libraries

2/11/2011 IICT, BUET 8


Basics of a Typical Java Environment (cont.)

˜ Java programs normally undergo five phases


˜ Edit
˜ Programmer writes program (and stores program on disk)
˜ Compile
˜ Compiler creates   from program
˜ Load
˜ Class loader stores bytecodes in memory
˜ Verify
˜ Verifier ensures bytecodes do not violate security
requirements
˜ Execute
˜ Interpreter translates bytecodes into machine language

2/11/2011 IICT, BUET 9


 


  
ð            
ð   #                  
 

   


           
            
ð   $       
       


ð   
   
ð   %        

          
             

 
ãã
ãã
ãã

ð   
   
ð   &                     
          
            
              
         
     
ãã
ãã
ãã

ð   
             
ð   '               
             
             
    
       !     
              
ãã         "    
ãã
ãã

2/11/2011 IICT, BUET 10


2/11/2011 IICT, BUET 11
Benefits of bytecode
˜ Translating a Java program into bytecode
helps makes it much easier to run a
program in a wide variety of environments
˜ Only JVM needs to be implemented for
each platform
˜ Once the run-time package exists for a
given system, any java program can run on
it

2/11/2011 IICT, BUET 12


An overview of JAVA

2/11/2011 IICT, BUET 13


Object Oriented Programming
˜ Two paradigms of programming
˜ Process-oriented model: characterizes a
program as a series of linear steps (i.e. C)
˜ What is happening?
˜ Code acting on data

˜ Object-oriented programming: organizes a


program around its data (that is, objects) and
a set of well-defined interfaces to that data
˜ Who is being affected?
˜ Data controlling access to code

2/11/2011 IICT, BUET 14


Essential elements of OOP
˜ Abstraction (the way humans manage
complexity)
˜ i.e. people don¶t think of a car as a set of tens of
thousands individual parts. They think of it as a well-
defined object with its own unique behavior. They
are free to utilize the object as a whole
˜ A complex system can be broken into more
manageable pieces, which are hidden by hierarchical
abstraction
˜ The three OOP Principles
˜ Encapsulation
˜ Inheritance
˜ Polymorphism
2/11/2011 IICT, BUET 15
Inheritance

2/11/2011 IICT, BUET 16


A First Simple Program
/*
This is a simple Java program. Call this file
"Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}

2/11/2011 IICT, BUET 17


Entering the program
˜ The name of a source file is very important
˜ In Java, all code must reside inside a class.
By convention, the name of that class
should match the name of the file that
holds the program
˜ A source file use the .java filename
extension
˜ Java is case-sensitive
2/11/2011 IICT, BUET 18
Compiling the Program
C:>\javac Example.java
˜ The javac compiler creates a file called
| , that contain the bytecode version
of the program
˜ To actually run program, you must use the Java
interpreter, called °
C:>\java Example
˜ When java source code is compiled, each
individual class is put into its own output file
named after the class and using the 

extension
2/11/2011 IICT, BUET 19
A closer look at the first program
˜ All Java application begin execution by calling
main()
˜ A complex program will have dozens of classes, only
one of which will need to have a 
 method to
get things started
˜ Java applets don¶t have any 
 method
˜ Java interpreter run only the class that have a main
method
˜   is a built-in method
˜ Ä is a predefined class that provides access to
the system, and  is the output stream that is
connected to the console
2/11/2011 IICT, BUET 20
Control statements
˜ if (condition) statement
˜ for(initialization;condition;iteration)
statement

2/11/2011 IICT, BUET 21


Lexical issues
˜ White space between tokens
˜ Identifiers
˜ Literals (the way constant value is created)
˜ Integer
˜ Floating point
˜ Character
˜ String
˜ Comments
˜ Separators
˜ Keywords
2/11/2011 IICT, BUET 22
Data types
˜ Integers
˜ long (64 bit)
˜ int (32 bit)
˜ short (16 bit)
˜ byte (8 bit)
˜ Floating-pointer numbers
˜ double (64 bit)
˜ float (32 bit)
˜ Characters (char, 8 bit)
˜ Booleans (boolean, true or false)

2/11/2011 IICT, BUET 23


Arrays
˜ Arrays in Java work differently than they do in other
languages
˜ First declare the array
int month_days[];
˜ Then allocate memory
month_days=new int[12];
˜ Arrays can be initialized during declaration
˜ Multidimensional arrays
int twoD[][]=new int[4][5];
˜ Alternative array declaration
int [] a2=new int[3];
˜ String type object
2/11/2011 IICT, BUET 24

You might also like