Académique Documents
Professionnel Documents
Culture Documents
Java
History of Java
1990
Group of engineers and programmers and Sun is created
James Gosling
Master programmer to create the Java Language
Partick Naughton
Designed the visual interfaces used to promote the Java environment
History of Java
Green Team Application Consumer Electronics Single Controller Programming a household thermostat Coordinating a VCR and TV Cable TV switchboxes 1991 Behind the Green Door document Code had to be small and tight as the devices did not have a lot of power or memory Different CPUs , not to be tied down to a single architecture. Proposed a distributed, a network of totally dissimilar devices, using byte streams to communicate with the controller.
visit www.muenggg.blogspot.com for more study material.
History of Java
Oak Language
Naughton developed visual representation interface for the new language
The user could open a cartoon TV Guide Select a movie Perform various operations on the VCR Gosling got the animation working in this new
language, which he called outside his office window.
visit www.muenggg.blogspot.com for more study material.
Oak,
after tree
History of Java
1993
Green Team is incorporated as
FirstPerson, Inc. (subsidiary of Sun) Looking to license technology
Cellular phone Industrial automation systems Interactive television CD-ROMs Commercial online services
visit www.muenggg.blogspot.com for more study material.
History of Java
WWW appeared on the Internet and transformed a text-based
Internet into graphical rich environment. Non Commercial Web Browser : Mosaic
1995
1999
2000
20002 2004
2005
Approximately 4.5 million developers use Java technology Over 2.5 billion Java technology-enabled devices are available
visit www.muenggg.blogspot.com for the JRE download java.com bundles the Google Toolbar with more study material.
Source Code
Source Code
Windows Environment
Source Code
Sun Environment
Compiling/ Linking
Java Environment
javac classname.java
Compiling
Java Bytecode
Programmers Computer
Users Computer Java Virtual Machine (Standalone or Browser)
java classname
visit www.muenggg.blogspot.com for more study material.
Compiling
javac SumOneToTen.java
Java Bytecode
Programmers Computer
Users Computer Java Virtual Machine
(Standalone or Browser)
java SumOneToTen
visit www.muenggg.blogspot.com for more study material.
Java Compiler
The same .class file can execute on any platform, as long as the JRE is installed there
JRE for Mac
Java Buzzwords
Simple Object Oriented
Interpreted
Portable Architecture neutral High Performance Robust
Secure
Multi-threaded Dynamic
Distributed
Simple
easy to learn and use effectively cleaned up version of syntax of C++ small construction of software that can run stand alone in small machines familiar language Programmers can migrate easily to Java platform
visit www.muenggg.blogspot.com for more study material.
Object Oriented
True object oriented language almost everything is an object Program code + data reside within objects and classes extensive set of classes in packages
Distributed
Routines for TCP/IP protocols like HTTP and FTP Open and access objects across network with URLs with ease Networking capabilities are strong and easy to use Servlets: server side processing is efficient RMI: communication between distributed objects
visit www.muenggg.blogspot.com for more study material.
Robust
Emphasis on early checking for possible problems Also dynamic ( run-time) checking Concept of true arrays Eliminates the possibility of overwriting memory and corrupting data
Secure
Used in networked/distributed environments
Emphasis on security Construction of virus free and tamper free systems
Architecture neutral
Designed to support applications on networks Networks: variety of CPUs & OS Compiler generates an architecture neutral object file format : bytecode Compiled code is executable on many processors given the presence of Java run time system Java compiler does this by generating byte-code instructions.
visit www.muenggg.blogspot.com for more study material.
Portable
No implementation-dependent aspects of
specification
Java program can be easily moved from one computer system to another Size of primitive data types are fixed Behavior of arithmetic on them is fixed
visit www.muenggg.blogspot.com for more study material.
Interpreted
Can execute Java bytecodes on any machine to which the interpreter is ported.
High Performance
Performance of interpreted byte-codes is usually more than adequate.
Dynamic
More dynamic than C and C++ Designed to adopt to an evolving environment Libraries can add new methods and variables without any effect on their clients
Multithreaded
Handling multiple tasks Benefits: interactive responsiveness and real-time behavior
No operator overloading
No automatic coercions No pointers
No template classes
No support for global variables No type modifiers like , auto , register , signed and unsigned visit extern www.muenggg.blogspot.com for
more study material.
Capabilities of Java
Client-Side Java: Applets AWT JFC (2D, Swing ,Acc..) Networking JavaBeans Server-Side Java: Servlets JSP JDBC Networking JavaBeans
Distributed Technologies RMI Java IDL (CORBA) JINI & JavaSpaces EJB
visit www.muenggg.blogspot.com for more study material.
Typical Java Development Environment Phase 1 : Edit Phase 2 : compile Phase 3 : load Editor Disk
compiler
Class loader
Disk
Primary memory
Disk
Phase 4 : verify
Primary memory
Primary memory
Phase 5 : execute
verify execute
Bytecode verifier confirms that all bytecodes are valid and do not violate Javas security restrictions JVM reads the bytecodes and translates them into the language the computer can understand.
Java Source (Java) Java Byte codes move locally or through network
Java Interpreter
Just-in-time compiler
Java Compiler
JVM
Runtime System
Operating System
Hardware
Java Environment
A Java program
A Java program
public class HelloWorldApp {
public static void main(String [ ] args) { System.out.println("Hello World!"); } }
visit www.muenggg.blogspot.com for more study material.
A Java program
Java is case sensitive.
A Java program
public class HelloWorldApp
Keyword public is an access modifier. Keyword class begins the class definition for a class named name. Everything in Java must reside inside the class. HelloWorldApp is a Java identifier that specifies the name of the class to be defined. Every class definition in Java begins with an opening brace ({ ) and ends with a matching closing brace ( })
visit www.muenggg.blogspot.com for more study material.
A Java program
public static void main(String [ ] args)
Defines a method named main.
Starting point for the interpreter to begin execution of the program. A Java application can have any number of classes but only one of them must include a main method to initiate execution.
visit www.muenggg.blogspot.com for more study material.
A Java program
public static void main(String args[ ] ) public: an access specifier that declares the main method as unprotected and therefore making it accessible to all other classes. static : declares this method as one that belongs to the entire class and not a part of any objects of the class. The main method must always be declared as static since the interpreter uses this method before any objects are created. void : main method does not return any value. Braces mark the beginning and end of the body of the method.
visit www.muenggg.blogspot.com for more study material.
main as static?
When you execute the Java Virtual Machine (JVM) with the java command, the JVM attempts to invoke the main method of the class you specifywhen no objects of the class have been created. Declaring main as static allows the JVM to invoke main without creating an instance of the class.
visit www.muenggg.blogspot.com for more study material.
A Java program
String args[ ] declares a parameter named args which is an array of objects of class String. This array is the mechanism through which the runtime system passes information to your application. Each string in the array is called a command-line argument. The println method is a member of out object which is a static data member of visit www.muenggg.blogspot.com for System class. more study material.
A Java program
Compiler Errors
Message : 'javac' is not recognized as an internal or external command, operable program or batch file.
Meaning :Windows cannot find the compiler (javac). Update the PATH variable
visit www.muenggg.blogspot.com for more study material.
The message usually displays the type of the error, the line number where the error was detected, the code on that line, and the position of the error within the code.
In addition to verifying that your program is syntactically correct, the compiler checks for other basic correctness. e.g. testing.java:13: Variable count may not have been initialized. count++
visit www.muenggg.blogspot.com for more study material.
Runtime Errors
Message : Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp Meaning : java cannot find your bytecode file, HelloWorldApp.class. Set the CLASSPATH variable
Identifiers in Java
Names given to entities that a programmer can manipulate in the program. Three aspects Legal identifiers Suns Java code conventions Java Beans naming standard
visit www.muenggg.blogspot.com for more study material.
Constants
Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators.
JavaBeans Standards
Comments can appear at the beginning or end of any line in the source code file
If there is a public class in a file, the name of the file must match the name of the public class. If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
visit www.muenggg.blogspot.com for more study material.
Data types
Strongly typed language Every variable must have a data type Eight primitive data types in Java Four integer types Two floating-point number types One character data type One Boolean (logical) data type.
visit www.muenggg.blogspot.com for more study material.
int short
4 bytes 2 bytes
long
byte
8 bytes
1 byte
- 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
-128 to 127
float
4 bytes
double
8 bytes
Character type
To store characters. contains a single, 16-bit Unicode character. A larger range is needed to represent characters found in languages other than English. Unicode characters are actually represented by unsigned 16-bit integers, which means 216 possible values, ranging from 0 visit www.muenggg.blogspot.com forto 65535
more study material.
Boolean type
used for evaluating logical conditions. Two values : true and false Cannot convert between integers and boolean values.
0010011
value bits
short
111110100000011
Variables
A container to hold a value Basic unit of storage All variables must have a name , type and an optional initializer. All variables have a scope which defines their visibility & life time. Must be declared before use.
visit www.muenggg.blogspot.com for more study material.
Variables
Syntax: type identifier [=value] [,identifier [=value ] . ]; Semicolon is necessary because declaration is a complete Java statement. a
Examples : Variables
int count ; float x , y; double salary; byte b ; char c1 , c2, c3 ; boolean done;
Initializing variables
Use assignment statement to give a value to a variable.
variable = value ;
Initializing variables
Declare and initialize the variable on the same line. type variable = value ; int m = 5 , n = 10; int days = 12 ; char yes = x double total = 75.30;
visit www.muenggg.blogspot.com for more study material.
Dynamic initialization
public class DynInit { public static void main(String [] args) { double a = 3.0 , b= 4.0; double c = Math.sqrt(a * a + b * b); System.out.println(Hypotenuse is + c); } }
visit www.muenggg.blogspot.com for more study material.
Scope of variables
Part of the program from where the variable is accessible is called the scope. 3 kinds instance variables class variables local variables
visit www.muenggg.blogspot.com for more study material.
Instance variables
Defined inside the class, but outside of any method. Only initialized when the class is instantiated. Instance variables are the fields that belong to each unique object. Have a default value depending on data type.
visit www.muenggg.blogspot.com for more study material.
Instance variables
Can use any of the four access levels Can be marked final Can be marked transient Cannot be marked abstract Cannot be marked synchronized Cannot be marked strictfp Cannot be marked native Cannot be marked static, because then they'd become class variables.
Local variables
Variables declared and used within a method. A local variable starts its life inside the method, it's also destroyed when the method has completed. Before a local variable can be used, it must be initialized with a value.
visit www.muenggg.blogspot.com for more study material.
Local variables
Can also be declared inside program blocks between { and } braces.
Shadowing
class TestServer { int count = 9; // Declare an instance variable named count public void logIn() { int count = 10; // Declare a local variable named count System.out.println("local variable count is " + count); } public void count() { System.out.println("instance variable count is " + count); } public static void main(String[] args) { new TestServer().logIn(); new TestServer().count(); } visit www.muenggg.blogspot.com for }
more study material.
Variable scope class Layout { static int s = 340; // static variable int x ; // instance variable { x = 5 ; int x2 = 10; } Layout() { x +=8 ; int x3 = 6; } void dostuff() { int y = 0 ; for (int z = 0 ; z < 4 ; z++) { y += z + x; } } visit www.muenggg.blogspot.com for more study material. }
Variable scope
variable s x x2 x3 y type Static variable Instance variable Block variable Constructor variable , local variable Local variable
visit www.muenggg.blogspot.com for more study material.
Block variable
Literals
Source code representation of a fixed value e.g. 'b' 42 false 2546789.343
Integer Literal
Three ways to represent integer numbers in the Java language: decimal (base 10) octal (base 8) hexadecimal (base 16) Octal Literals Octal integers use only the digits 0 to 7. An integer in octal form is represented by placing a zero in front of the number. visit www.muenggg.blogspot.com for
more study material.
Integer Literal
Hexadecimal Literals are constructed using 16 distinct symbols.
An integer in Hex. form is represented by placing a 0x in front of the number. 0XCAFE and 0xcafe are both legal and have the same value.
visit www.muenggg.blogspot.com for more study material.
Integer Literal
All three integer literals are defined as int by default.
Can be specified as long by placing a suffix of L or l after the number. long a = 110500L long b = 0xFFFFl
visit www.muenggg.blogspot.com for more study material.
To assign a floating-point literal to a variable of type float (32 bits), you must attach the suffix F or f to the number. visit www.muenggg.blogspot.com for
more study material.
float f = 23.467890f;
Boolean Literals
Source code representation for boolean values. Defined as true or false. e.g. boolean t = true ; boolean f = 0 ; //Compiler error
visit www.muenggg.blogspot.com for more study material.
Character Literals
Represented by single character in single quotes. e.g. char a = a ; char b = ? char c = 890 ; char d = 1500
visit www.muenggg.blogspot.com for more study material.
Constants
Use the keyword final to denote a constant. assign to a variable once and then its value is set once and for all. public class Constants { public static void main(String [] args) { final double CM_PER_INCH = 2.54; double w = 8.5 , h = 11.0; System.out.println(Paper size in cms : + w * CM_PER_INCH + by +h * CM_PER_INCH); } }
visit www.muenggg.blogspot.com for more study material.
Constants
constant that is made available to multiple methods inside a single class. Referred to as class constant set up a class constant with keywords static and final Declared outside of methods.
Constants
public class Constants2 { public static void main(String [] args) { double w = 8.5 , h = 11.0; System.out.println(Paper size in cms : + w * CM_PER_INCH + by +h * CM_PER_INCH); } public static final double CM_PER_INCH = 2.54; }
visit www.muenggg.blogspot.com for more study material.