Vous êtes sur la page 1sur 21

Java

Introduction:Java is a general-purpose, object-oriented programming language. We can develop two types of java programs.
Stand-alone web applets

application

Stand-alone applications are programs written in java to carry out certain tasks on a stand-alone local computer. Java can be used to develop programs for all kinds of applications, which earlier, were developed using languages like C & C++.

A stand-alone java program involves two steps. 1. Compiling source code into bytecode using javac compiler 2. Executing the bytecode program using java interpreter.

Applets are small java programs developed for Internet applications. An applet located on a distant computer (Server) can be downloaded via internet & executed on a local computer (Client) using a java-capable browser. We can develop applets for doing everything from simple animated graphics to complex games & utilities. Since applets are embedded in an HTML (Hypertext Markup Language) document & run inside a Web page, creating & running applets are more complex than creating an application.
Stand-alone programs can read & write files & perform certain operation that applets cannot do. An applet can only run within a Web browser.

Java Source Code

Java Compiler Applet Type Java enabled Web Browser Application Type Java Interpreter

Output

Output

Two Ways of using Java

A simple java program


Class A { Public static void main (String args[]) { System.out.println (java is better than C++); } }

**********OUTPUT********** Java is better than C++

Java program structure


A java program may contain many classes of which only one class defines a main method. Classes contain data members & methods that operate on the data members of the class. Methods may contain data type declarations & executable statemants. To write a java program, we first define classes & then put them together. A java program may contain one or more sections.

________________________ ________________________ ________________________ ________________________

Documentation Section Package Statement Import Statements

Suggested Optional Optional Optional Optional

Interface Statements Class Definitions

Main Method Class { Main Method Definition }

Essential

General Structure of a Java Program

Java Virtual Machine


All language compilers translate source code into machine code for a specific computer. Java compiler also does the same thing. Then, how does Java achieve architecture neutrality? The answer is that the java compiler produces an intermedia code known as bytecode for a machine that does not exist. This machine is called the Java Virtual Machine & it exists only inside the computer memory. The process of compiling a java program into bytecode which is also referred to as virtual machine code.

Java Program

Java Compiler

Virtual Machine

Process Of Compilation

The virtual machine code is not machine specific. The machine specific code is generated by the java interpreter by acting as an intermediary b/w the virtual machine & the real machine. The interpreter is different for different machines.
Bytecode Java Interpreter Machine Code

Process Of Converting Bytecode Into Machine Code

Static
Static:- Then memory will intilized itself without making object. To call a static method in a static method we need not to make object.

Static Method Program


class test { static void show() { System.out.println("PROGRAM 1"); } void display() { System.out.println("PROGRAM 2"); } } class demo { public static void main(String args[]) { test.show();//to call a static method in another static method we do not need to make an object test obj=new test(); obj.display(); }

**************OUTPUT*************** PROGRAM1 PROGRAM2

Inheritance
A

1.Single inheritance (one super class)


A B C

2.Multilevel inheritance (several super class)

3.Multiple inheritance (derived form a derived class)

Single Inheritance Program


class test { public void show() { System.out.println("Hi"); } public void show1() { System.out.println("BYE"); } } class demo extends test { void display() { System.out.println("HELLO"); } } class demo9 { public static void main(String args[]) { demo obj= new demo(); obj.display(); obj.show(); obj.show1(); } }

************OUTPUT***********
Hello Hi Bye

packages
Packages:- when we compile a class. Package is a collection of class files or you can say class. Default with in a package it act as public but if we try to use the default method outside the package then it will act as private.

Package p1; Public class text { Public void show() { System.out.println (hello); } Void display() { System.out.println (bye); } Public static void main (string args[]) { Test obj=new test(); Obj. display(); Obj. show(); } }

Package Program

********************OUTPUT******************* bye hello

Package Program
Package p2; Import p1.test; Class a { public static void main(steing args[]) { test obj=new test(); //error obj.display(); obj.show(); } } ************OUTPUT************ HELLO

Interfaces
We discussed about classes & how they can be inherited by other classes. we also learned about various forms of inheritance & pointed out that java does not support multiple inheritance. That is, classes in java cannot have more than one superclass. For instance, a definition like is not permitted in java. However, the designers of java could not overlook the importance of multiple inheritance.

Interface Program
Interface I1 { Public void show(); } abstract class A Implements I1 { Void show1() { System.out.println (BYE); } } Class B extends A { Void show() { System.out.println (HELLO); } } Public static void main (string args[]) { B obj=new B(); obj.show(); } } ***********************OUTPUT******************************************** HELLO BYE

Vous aimerez peut-être aussi