21
Introduction to Sun Microsystems java High level OOPL Platform Independent Case Sensitive -T!alaji " !#"MS"

java

Embed Size (px)

DESCRIPTION

java clases

Citation preview

  • Introduction to Sun Microsystems java High level OOPL Platform Independent Case Sensitive

    -T.Balaji , B.E.,M.S.,

  • Can you make coffee with it?

  • It was meant to!!A programming language for appliances!

  • Doesnt Make Coffee Yet

  • So Whats Java Good For?Web applications!Java AppletServerJava Applet

  • Java on the Web: Java AppletsClients download applets via Web browserBrowser runs applet in a Java Virtual Machine (JVM)

    Interactive web, security, and client consistencySlow to download, inconsistent VMs (besides, flash won this war)ClientServer

  • Java on the Web: J2EEThin clients (minimize download)Java all server side

    THIS IS WHAT YOULL BE DOING!!ClientServer

  • The Java programming environmentCompared to C++: no header files, pointers, etc.

    Object-orientation: Classes + Inheritance

    Distributed: RMI, Servlet, Distributed object programming.Robust: Strong typing + garbage collection Architecture neutral: architecture neutral representationPortable

  • Java FeaturesWell defined primitive data types: int, float, double, char, etc.int 4 bytes [2,147,648, 2,147,483,647]Control statements similar to C++: if-then-else, switch, while, for InterfacesPackagesExceptionsMulti-ThreadingApplet model

  • Must Run on Any ArchitectureJava Virtual Machine

    Programin JavaJavaCompilerJavaBytecodeJava Virtual Machine

    WRITE ONCE, RUN ANYWHERE!pretty portable

  • The Java programming environment Java programming language specificationSyntax of Java programsJava byte code: Intermediate representation for Java programsJava compiler: Program that Transform Java programs into Java byte codeJava virtual machine: The whole technology is based on the concept of JVM. Translator of byte code into platform specific machine language. It is the Runtime system that provides various services to running programs (Logical representation of JRE)

  • JVM

  • JVMJVM stands for Java Virtual Machine. Its an abstract computer or virtual computer which runs the compiled java programs. Actually JVM is a software implementation which stands on the top of the real hardware platform and operating system. It provides abstraction between the compiled java program and the hardware and operating system. JIT: This Compiler converts byte code to native code.Old Interpreter was replaced with JIT to increase the performance of java applications. When JVM compiles the class file it does not compile the full class file in one shot. Compilation is done on function basis or file basis. Depending on need basis the compilation is done. This type of compilation is termed as JIT or Just-in- Time compilation.

  • How are Java programs written? Define a class HelloWorld and store it into a file: HelloWorld.java:public class HelloWorld {public static void main (String[] args) {System.out.println(Hello, World);}}Compile HelloWorld.javajavac HelloWorld.javaOutput: HelloWorld.classRun java HelloWorldOutput: Hello, World

  • How are variables declared?Fibonacci:class Fibonacci {public static void main(String[] arg) {int lo = 1;int hi = 1;System.out.println(lo);while (hi < 50) {System.out.println(hi);hi = hi + lo;lo = hi lo;}}}

  • How to define expressions?Arithmetic: +, -, *,/, %, =8 + 3 * 2 /4Use standard precedence and associativity rulesPredicates: ==, !=, >, =,
  • How are simple methods defined?Every method is defined inside a Java class definitionpublic class Movie {public static int movieRating(int s, int a, int d) {return s+a+d;}}public class Demo {public static void main (String argv[]) {int script = 6, acting = 9, directing = 8;displayRating(script, acting, directing);}public static void displayRating(int s, int a, int d){System.out.print(The rating of this movie is);System.out.println(Movie.movieRating(s, a, d));}}

  • How to extend classes?Inheritance: mechanism for extending behavior of classes; leads to construction of hierarchy of classes [Note: no multiple inheritance]What happens when class C extends class D:Inherits instance variablesInherits static variablesInherits instance methodsInherits static methodsC can:Add new instance variablesAdd new methods (static and dynamic)Modify methods (only implementation)Cannot delete anything

  • How to extend classes? public class Attraction {public int minutes;public Attraction() {minutes = 75;}public int getMinutes() {return minutes;}public void setMinutes(int d) {minutes = d;}}public class Movie extends Attraction {public int script, acting, directing;public Movie() {script = 5; acting = 5; directing = 5;}public Movie(int s, int a, int d) {script = s; acting = a; directing = d;} public int rating() {return script + acting + directing;}}public class Symphony extends Attraction {public int playing, music, conducting;public Symphony() {playing = music = conducting = 5;}public Symphony(int p, int m, int c) {playing = p; music = m; conducting = c;} public int rating() {return playing + music + conducting;}}

  • What are abstract classes? Abstract class: Merely a place holder for class definitions; cannot be used to create instances.;public abstract class Attraction {public int minutes;public Attraction() {minutes = 75;}public int getMinutes() {return minutes;}public void setMinutes(int d) {minutes = d;}public abstract void m();}Following is an error:Attraction x;x = new Attraction();Following is not an error:public class Movie extends Attraction { }public class Symphony extends Attraction { }Attraction x;x = new Movie ();x = new Symphony();

  • Packages How do we organize above classes into a single unit? Put them in file? However, only one public class per file (whose name is same as files) Solution: Place several files (compilation units) into a package