740 likes | 1.69k Views
Java and its Evolution. Contents. Java Introduction Java Features How Java Differs from other OO languages Java and the World Wide Web Java Environment Build your first Java Program Summary and Reference. Java - An Introduction.
E N D
Contents • Java Introduction • Java Features • How Java Differs from other OO languages • Java and the World Wide Web • Java Environment • Build your first Java Program • Summary and Reference
Java - An Introduction • Java - The new programming language developed by Sun Microsystems in 1991. • Originally called Oak by James Gosling, one of the inventors of the Java Language. • Java Authors: James , Arthur Van , and others • Java is really “C++ -- ++ “
Java Introduction • Originally created for consumer electronics (TV, VCR, Freeze, Washing Machine, Mobile Phone). • Java - CPU Independent language • Internet and Web was just emerging, so Sun turned it into a language of Internet Programming. • It allows you to publish a webpage with Java code in it.
Sun white paper defines Java as: • Simple and Powerful • Safe • Object Oriented • Robust • Architecture Neutral and Portable • Interpreted and High Performance • Threaded • Dynamic
Java is Compiled and Interpreted Programmer Hardware and Operating System Source Code Byte Code Text Editor Compiler Interpreter .java file .class file java appletviewer netscape Notepad,emacs,vi javac
Total Platform Independence JAVA COMPILER (translator) JAVA BYTE CODE (same for all platforms) JAVA INTERPRETER (one for each different system) Windows 95 Macintosh Solaris Windows NT
Architecture Neutral & Portable • Java Compiler - Java source code (file with extension .java) to bytecode (file with extension .class) • Bytecode - an intermediate form, closer to machine representation • A interpreter (virtual machine) on any target platform interprets the bytecode.
Architecture Neutral & Portable • Porting the java system to any new platform involves writing an interpreter. • The interpreter will figure out what the equivalent machine dependent code to run
Overlap of C, C++, and Java C++ C Java
Java better than C++ ? • No Typedefs, Defines, or Preprocessor • No Global Variables • No Goto statements • No Pointers • No Unsafe Structures • No Multiple Inheritance • No Operator Overloading • No Automatic Coercions • No Fragile Data Types ?
Java Integrates Power of Compiled Languages and Flexibility of Interpreted Languages
Java Applications • We can develop two types of Java programs: • Stand-alone applications • Web applications (applets)
Applications v/s Applets • Different ways to run a Java executable code are: Application- A stand-alone program that can be invoked from command line . A program that has a “main” method. It is executed by the Java Interpreter. Applet- A program embedded in a web page , to be run when the page is browsed . A program that contains no “main” method. It is executed on a Java-enabled web browser.
Java Development Kit • javac - The Java Compiler • java - The Java Interpreter • jdb- The Java Debugger • appletviewer -Tool to run the applets • javap - to print the Java bytecodes • javaprof - Java profiler • javadoc - documentation generator • javah - creates C header files
Process of Building and Running Java Programs Text Editor Java Source Code javadoc HTML Files javac Java Class File javah Header Files java jdb Output
Java Program Structure • A Java program consists of: • One or more classes • A class contains one or more methods • A method contains program statements • We will explore these terms in detail
Java Program Structure // comments about the class public class MyProgram { } class header class body Comments can be placed almost anywhere
Java Program Structure // comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body
Hello World // HelloWorld.java public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);
Hello World // HelloWorld.java • Creates a “class” called HelloWorld • Compiled to HelloWorld.class • Classes used to define objects… later public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);
Hello World // HelloWorld.java • The “main” method is where it starts to run • Ignore “public static void” and “String[] args” for now public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);
Hello World // HelloWorld.java • Contains one “statement” • The System.out.println function comes from the Java “class library” • Ends with a semicolon (all statements do) public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);
Compiling and Running • Create the file HelloWorld.java in a text editor • Compile: • javac HelloWorld.java • Run: • java HelloWorld • Output: • Hello World!
Comments • Three kinds of comments: • To simplify: comments are good // a one-line comment /* a multi-line comment */ /** a javadoc comment */
Reserved Words and Identifiers • Reserved words are specified by the language • All Java reserved words are in the text • Identifiers are specified by a programmer • Maybe you: e.g. HelloWorld • Maybe someone else: e.g. println
Declaring Variables • Syntax: • Examples: • int count1, int count 2; • int count = 0; • String course1 = “CMPT 126”; <variable declaration> ::= <type> <declarator>, …. ; <declarator> ::= <identifier> <declarator> ::= <identifier> = <expression>
Primitive Data Types in Java • Four integer types: • byte, short, int, long • Two floating point types • float, double • One of them is for characters • char • One of them is for boolean values • boolean
Expressions and Assignment • An expression is a combination of one or more operators and operands • Arithmetic operators: +, -, *, /, % • Use the normal order of operations e.g. int exp = 2 * 5 +7; count = count + 1; count++; • Boolean operators: &&, ||
More Assignment Operators • x += y is equivalent to x = x + y
Example: String Objects • We have already seen one object type in Java: String • A String object is a list of characters e.g. “Hello world!” or “My name is Aaron” • Can be passed to print or println • Can be concatenated using the (+) operator e.g. “Hello world! ” + “My name is Aaron” “I can also append numbers, like “ + 2
Object Instances • We must create a new “instance” of an object to store something • Each object type has a constructor (more later) • Create instances using the reserved world new e.g. course = new String(“CMPT 126”); • This creates a new String in memory • It stores the characters “CMPT 126” • The assignment sets course to refer to this instance
References and Instances String course; course: new String(“CMPT 126”) CMPT 126 course = new String(“CMPT 126”); course: CMPT 126
Summary • Java has emerged as a general purpose OO language. • It supports both stand alone and Internet Applications. • Makes the Web Interactive and medium for application delivery. • Provides an excellent set of Tools for Application Development. • Java is ubiquitous!