Goals Understand the basics of Java. Introduction to Java Write simple Java Programs. 1 2 Java - An Introduction Java is Compiled and Interpreted Java - The programming language from Sun Microsystems Programmer Hardware and Operating System Java -Allows anyone to publish a web page with Java code in it Java - CPU Independent language Source Code Byte Code Text Editor Compiler Interpreter.java file.class file Notepad, emacs,vi javac java appletviewer netscape 3 4 Compiled Languages Architecture Neutral & Portable Programmer Source Code Object Executable Code Code Text Editor Compiler linker.c file.o file a.out file Notepad, emacs,vi gcc 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. 5 6
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 Java Integrates Power of Compiled Languages and Flexibility of Interpreted Languages 7 8 Rich Class Environment Applets v/s Applications Core Classes language Utilities Input/ Output Low-Level Networking Abstract Graphical User Interface Internet Classes TCP/ IP Networking WWW and HTML Distributed Programs Different ways to run a Java executable are: Application- A stand-alone program that can be invoked from command line. A program that has a main method Applet- A program embedded in a web page, to be run when the page is browsed. A program that contains no main method 9 10 Applets v/s Applications Java Development Kit Application Executed by the Java interpreter Applets- Java enabled web browser 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 11 12
Hello World Program Processing // HelloWorld.java: Hello World program class HelloWorld public static void main(string args[]) System.out.println( Hello World ); 13 Compilation # javac HelloWorld.java results in HelloWorld.class Execution # java HelloWorld Hello World 14 Closer Look at - Hello World Simple Java Applet The class has one method main() public static void main(string args[]) System.out.println( Hello World ); Command line input arguments are passed in the String array args[] e.g java HelloWorld John Jane args[0] John args[1] Jane // HelloWorld Applet import java.applet.applet; public class HelloWorld extends Applet public void paint(graphics g) g.drawstring ( Hello World!,25, 25); 15 16 Calling an Applet Execution of Applets <HTML> <TITLE>HELLO WORLD APPLET</TITLE> <HEAD>THE HELLO WORLD APPLET</HEAD> <APPLET CODE=HelloWorld.class width=500 height=500> </APPLET> </HTML> 1 2 3 4 5 Accessing from Microsoft.COM APPLET Development hello.java AT SUN.COM Hello Java hello.class AT SUN S WEB SERVER <app= Hello > Create Applet tag in HTML document The Internet The browser creates a new window and a new thread and then runs the code Hello 17 18
Variables Local Variables are declared within the block of code Variable has a type preceding the name Initial value is set by initialization expressions. type variablename = initialvalue; e.g. int lo = 1; Basic Data Types Types boolean either true of false char 16 bit Unicode 1.1 byte 8-bit integer (signed) short 16-bit integer (signed) int 32-bit integer (signed) long 64-bit integer (singed) float 32-bit floating point (IEEE 754-1985) double 64-bit floating point (IEEE 754-1985) String (class for manipulating strings) Java uses Unicode to represent characters internally 19 20 Declaring Variables - example Constants class SquaredNumbers public static void main(string args[]) int lo = 1; int squared = 0; while (squared <= 25) Constants are similar to variables except that they hold a fixed value. Constants are declared with the reserved word final. final int MAX_LENGTH = 420; By convention upper case letters are used for defining constants. 21 22 Declaring Constants - example Comments class SquaredNumbers public static void main(string args[]) final int MAX_NUMBER = 25; int lo = 1; int squared = 0; while (squared <= MAX_NUMBER) English text scattered through the code are comments JAVA supports 3 types of comments /* * / - Usually used from multi-line comments // - Used for single line comments /* * * / - Documentation comments 23 24
Javadoc Control Flow Effort to make Java self-documenting Control Flow Statements in JAVA while loop True OOP style, encapsulate documentation within code :) Comments beginning with /* * and ending with * / can be extracted and turned into html documentation for loop do-while loop if-else statement switch statement JAVA does not support a goto statement 25 26 while loop for loop while (squared <= MAX) for (int i = 1; i < MAX; i++) System.out.println(i); // prints 1 2 3 4 5 27 28 do-while loop if-else loop do while (squared <= MAX) if ( i < 10) System.out.println( i is less than 10 ); else System.out.println( i is greater than or equal to 10 ); 29 30
switch statement switch (c) case a : System.out.println ( The character is a ); break; case b ; System.out.println ( The character is b ); break; default; System.out.println ( The character is not a or b ); break; C HelloWorld - Revision # include < stdio.h> Int main() printf("hello World\ n"); 31 32 C Variables - Revision C Constant - Revision # include <stdio.h> int main() int i, squared; i = 0; squared = 0; while (squared < = 25) squared = i* i; printf("squared = %d\n",squared); i+ + ; # include <stdio.h> # define MAX_NUMBER 25 int main() int i, squared; i = 0; squared = 0; while (squared < = MAX_NUMBER) squared = i* i; printf("squared = %d\n",squared); i+ + ; 33 34