Java

January 28, 2009

Java Basics

Filed under: Uncategorized — sarjukottapuram @ 9:43 am

java

Origins of the language

 

    James Gosling and others at Sun, 1990 – 95

 

    Oak language for “set-top box”

     small networked device with television display

     support for graphics

     execution of simple programs

     communication between local program and remote site

     no “expert programmer” to deal with crash, etc.

    Internet application

    simple language for writing programs

 

Design Goals

    Portability

    • Internet-wide distribution: PC, Unix, Mac

 

    Reliability

    • Avoid program crashes and error messages

 

    Safety

    • Programmer may be malicious

 

    Simplicity and familiarity

    • Appeal to average programmer; less complex than C++

 

    Efficiency

 

General Design Decisions

    Simplicity

    • Almost everything is an object

    • No functions, no multiple inheritance, no go to, no

 

    Operator overloading

 

    Portability and Network transfer

    • Bytecode interpreter on many platforms

 

    Reliability and Safety

    • Typed source and typed bytecode language

    • Run-time type and bounds checks

    • Garbage collection

 

Java System

    The Java programming language

    Compiler and run-time system

    • Programmer compiles code

    • Compiled code transmitted on network

    • Receiver executes on interpreter (JVM)

    • Safety checks made before/during execution

 

    Library, including graphics, security, etc.

    • Large library made it easier for projects to adopt Java

 

    Interoperability

    Provision for “native” methods

 

 

Applets, Servlets and Applications

    An applet is designed to be embedded in a Web page, and run by a browser

 

    Applets run in a sandbox with numerous restrictions; for example, they can’t read files and then use the network

 

    A servlet is designed to be run by a web server

 

    An application is a conventional program

 

Building Standalone JAVA Programs

    Prepare the file foo.java using an editor

 

    Invoke the compiler: javac foo.java

 

    This creates foo.class

 

    Run the java interpreter:  java foo

 

Java .class Life Cycle

 

Programs written in Java are compiled into machine language

 

javamach1

 

    A different Java bytecode interpreter (JVM) is needed for each type of computer

 

    This is one of the essential features of Java: the same compiled program can be run on many different types of computers.

 

Java Virtual Machine

    The .class files generated by the compiler are not executable binaries

    so Java combines compilation and interpretation

 

    Instead, they contain “byte-codes” to be executed by the Java Virtual Machine

    other languages have done this, e.g. UCSD Pascal

 

    This approach provides platform independence, and greater security

 

What is an Object?

    Objects are conceptually similar to real-world objects: they consist of state and related behavior

 

    An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods

 

    Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication.

 

    Hiding internal state and requiring all interaction to be performed through an object’s methods is known as data encapsulation — a fundamental principle of object-oriented programming.

 

Bundling code into individual software objects provides a number of benefits, including:

    Modularity

    Information-hiding

    Code re-use:

    Plugability and debugging ease:

 

What Is a Class?

    Object is an instance of the class

 

     A class is the blueprint from which individual objects are created.

 

    A class consists of

    a collection of fields, or variables, very much like the named fields of a struct

    all the operations (called methods) that can be performed on those fields

    can be instantiated

 

    A class describes objects and operations defined on those objects

 

The class hierarchy

    Classes are arranged in a hierarchy

 

    The root, or topmost, class is Object

 

    Every class but Object has at least one superclass

 

    A class may have subclasses

 

    Each class inherits all the fields and methods of its (possibly numerous) superclasses

 

HelloWorld (standalone)

 

public class HelloWorld {

  public static void main(String[] args) {

    System.out.println(“Hello World!”);

  }

}

 

Note that String is built in

println is a member function for the System.out class

 

Comments are almost like C++

 

    /* This kind of comment can span multiple lines */

    // This kind is to the end of the line

    /**
  * This kind of comment is a special
  * ‘javadoc’ style comment
  */

 

OOPs – means Object Oriented Programming

 

    Encapsulation, Inheritance and Polymorphism are main pillars of OOPs.  

 

    Encapsulation: Encapsulation is the process of binding together the methods and data variables as a  single entity. This keeps both the  data and functionality code safe  from the outside world. It hides the data within the class and makes it available only through  the methods.

 

    Inheritance: Inheritance allows a class (subclass) to acquire the properties and behavior of another class (superclass). In java, a class can inherit only one class (superclass) at a time but a class can have any number of subclasses. It helps to reuse, customize and enhance the existing code.

    Polymorphism : Polymorphism allows one interface to be used for a set of actions i.e. one name may refer to different functionality. Polymorphism allows a object to accept different requests of a client (it then properly interprets the request like choosing appropriate method) and responds  according to the current state of the runtime system, all without bothering the user. 

 

    There are two types of polymorphism :

    Compile-time polymorphism

    Runtime Polymorphism

 

What is Inheritance?

    Object-oriented programming allows classes to inherit commonly used state and behavior from other classes .

 

inheri

 

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike.

 

What Is an Interface?

    Methods form the object’s interface with the outside world

 

    An interface is a group of related methods with empty bodies

 

    Implementing an interface allows a class to become more formal about the behavior it promises to provide

 

    If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

 

What is a Package?

A package is a namespace that organizes a set of related classes and interfaces.

 

Access Modifiers

    Access modifiers are used to specify the visibility and accessibility of a class, member variables and methods. They can also be used with the member variables and methods to specify their accessibility. 

 

    public keyword specifies that the public class, the public  fields and the public methods can be accessed from anywhere.
       

    private: This keyword provides the accessibility only within a  class i.e. private fields and methods can be accessed only within the same class.
      

    protected: This modifier makes a member of the class available to all classes in the same package and all sub classes of the class. 
      

    default : Its not a keyword. When we don’t write any access modifier then default is considered. It allows the class, fields and methods accessible within the package only.

 

Variables

The Java programming language defines the following kinds of variables:

 

    Instance Variables – instance variables because their values are unique to each instance of a class (to each object) 

 

    Class Variables (Static Variables )  – A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.

 

    Local Variables – local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.

 

    Parameters –

    Main data types are int, double, boolean, char

 

    Also have byte, short, long, float

 

    boolean has values true and false

 

    Declarations look like C, for example,

    double x, y;

    int count = 0;

 

Expressions

    Assignment statements mostly look like those in C; you can use  =,  +=,  *=  etc.

 

    Arithmetic uses the familiar  +    *  /  %

 

    Java also has ++ and –

 

    Java has boolean operators  &&  ||  !

 

    Java has comparisons  <   <=  ==  !=  >=  >

 

    Java does not have pointers or pointer arithmetic

 

Name conventions

    Java is case-sensitive; maxval, maxVal, and MaxVal are three different names

 

    Class names begin with a capital letter

 

    All other names begin with a lowercase letter

 

    Subsequent words are capitalized: theBigOne

 

    Underscores are not used in names

 

    These are very strong conventions!

Creating and using an object

    Person john;
john = new Person ( );
john.name = “John Smith”;
john.age = 37;

    Person mary = new Person ( );
mary.name = “Mary Brown”;
mary.age = 33;
mary.birthday ( );

An array is an object

    Person mary = new Person ( );

    int myArray[ ] = new int[5];

    or:

    int myArray[ ] = {1, 4, 9, 16, 25};

    String languages [ ] = {“Prolog”, “Java”};

Blog at WordPress.com.