Java Introduction

Java is a truly object-oriented programming language developed by Sun Microsystems.
Goal was:"Write once, run anywhere, any time, forever"
The language is modeled on C++.

History

  • Java was conceived by James Gosling, Patrick Naughton, Chris Warth, ED Frank and Mike Sheridian at Sun Microsystem in 1991.
  • Initially Java was named as "Oak", because there was a Oak tree in front of the Sun Micosystems' office. But the name was already registered by some body so that name could not be used.
  • So one day the team went to a coffee shop and the name Java was struck to them the name was chosen so.

Features of Java

  • Simple
  • Secure
  • Portable
  • Object Oriented
  • Robust
  • Multithreaded
  • Architecture Neutral
  • Interpreted
  • High Performance
  • Distributed
  • Dynamic

Object Oriented Concepts

  • All tangible things around us are objects.
  • Has got a name, a set of attributes (or properties) and behaviors (or functions).

Example :
Class Rose


Objects and Class

  • Object oriented programming supports the view that programs are composed of objects that interact with one another.
  • Object must have unique identity (name): Car, Bicycle, Red ball.
  • Consider a car:
    • Color, wheel, engine, car seat, doors are properties(data declaration)
    • Move, horn, stop are behaviors(methods)
    • Class of an ObjectA class defines a collection of objects of same type.


Example

class Person
{
float height;
int age;
String lastName;
void run(){...}
void eat(){...}
void sleep(){...}
}

  • The process of creating an object from a class is called instantiation.

Object-Oriented Principles



Java Working

  • The designers of Java used the combination of compilation and interpretation.
  • Programs written in java are compiled into machine level language. But it is a machine level language for a computer that does not really exist.
  • This is so called Virtual Computer is known as Java Virtual Machine (J.V.M.).
  • The machine language for the JVM is called Java Byte Code. This java byte code can be run on any computer.
  • But the computer needs an interpreter for the java byte code.
  • This java byte code interpreter is different for different types of computer.
  • But once the byte code is generated it can be run on any type of machines.
  • Writing interpreter is easy and it is small.
  • Thus instead of writing compiler for each type of computer, interpreter is written.
  • Thus, even if a new type of computer is developed, it is easy to write an interpreter.

How Java works?

  • A compiler converts the java program into intermediate language form --Byte code, which is platform independent. This code is for a machine which is not in existence, it is called as Java Virtual Machine (JVM)
  • On any machine when you compile java code using javac compiler, byte code equivalent to the source is generated.
  • Byte code enables "run-anywhere" capability.
  • You invoke java command which will feed the byte code to the machine-dependent interpreter.


Adding main() Method

This is the point where the execution of the program begins.
Object Creation Using 'new'
class Person{
...
public static void main(String args[]){
Person rama;
rama = new Person();
// Person rama = new Person();
rama.run();
rama.sleep();
}
}

First Program


class Hello
{
   public static void main(String args[])
   {
     System.out.println("Hello My Dear Students");
   }
}

Working with the program

  • The above program is to be saved with the filename Hello.java.
  • It is compiled using the command -
            javac Hello.java
  • This creates a file named Hello.class (Byte Code),
  • To execute the program use the command - java Hello

Next>>Data types and Operators

Our aim is to provide information to the knowledge seekers. 

comments powered by Disqus












Footer1