1.99k likes | 2.01k Views
Introduction to Object-Oriented Programming and Java. Java Historical Aspects. Software engineers tried to write portable software April 1991, “Green” conducted by Sun to develop a system for developing consumer electronics’ logic realized the needs of a platform independent environment.
E N D
Java Historical Aspects • Software engineers tried to write portable software • April 1991, “Green” conducted by Sun to develop a system for developing consumer electronics’ logic • realized the needs of a platform independent environment.
Historical Aspects, cont.. • May 23, 1995 the Java Environment was announced by Sun Microsystems. • Popular browsers, such as Netscape Navigator and Internet Explorer, incorporate Java-based technology. • Stand alone environments developed.
Versions of Java Three major versions released by Sun: • Java 1.0.2 - still most widely supported by browsers • Java 1.1.5 Spring 1997, improvements to user interface, event handling. • Java 2 - released in December 1998. Check: http://java.sun.com for JDK
Java 2 • Swing - new features for creating a graphical user interface • Improvements to audio features • Drag and drop
Introduction to Java • Java Overview • Java is a (relatively) New Object-Oriented Programming Language. • Designed for the network. • Java syntax is simple. • Java is platform neutral.
Introduction to Java • Java Overview • Secure, High performance. • Multi-Threaded. • Java is defined with a very comprehensive class and object libraries (packages).
Introduction to Java • New Object-Oriented Language • All data structures, and programs that operate on it are encapsulated in the context of objects. • Code reuse is supported by Java's inheritance properties.
Introduction to Java • Designed to run on the net. • Net could be the Internet or an Intranet. • Built-in networking capability in a language package Java.net • Support client/server computing between different computers.
Introduction to Java • Java is relatively simple yet powerful • Java’s syntax is based on C & C++ syntax that makes it easy to learn. • Java forbids the use of pointers. Java automatically handles the referencing and de-referencing of objects. Eliminates memory leaks. • Rich pre-defined classes for I/O, net, & graphics.
Introduction to Java • Java can be “platform neutral”. • Java can be an interpreted language. Programs are interpreted to an intermediate optimized form called “byte-code” • The run-time interpreter reads the byte-code and executes using a model of an abstract machine (JVM).
Introduction to Java • Java can be “platform neutral”. • Java virtual machine converts byte-code to the specific machine code. • JVM & interpreter perform a very strong type checking. • Built-in Java memory model that performs “garbage collection” to prevent memory leaks.
Introduction to Java • Java Applet Security. • No Pointers. • Memory allocations and de-allocations are handled by JVM. • Byte-code verification every time an applet is loaded.
Introduction to Java • High Performance • Compared to other interpreted languages such as Basic, Visual Basic, etc. Java is much faster. • Although 20 times slower then C/C++, with JIT-compilers Java’s speed is somewhat comparable. • Compiled Java is as fast as other compiled languages
Introduction to Java • Java is Multi-Threaded • Native support for multi-tasking in the form of multi-threading. Multi-Threading = Several lines of execution run independently and simultaneously.
Introduction to Java • Classis the basic building block • The reserved word class indicates the beginning of a new class Syntax: (constructs in [ ] are optional) [<qualifier>] class classname [extends classname1] [implements interfacename] { ….. Class body; }
Introduction to Java • <class_qualifier> is one of the following reserved keywords • public • private • abstract • final
Introduction to Java • What’s in the class body? • Data members • Methods/messages that operate on the data.
Introduction to Java • Data Members Syntax: [<access_qualifier>] <Member type> MemberName; • Access qualifier can be one of the following • public final • private protected • static • Member Type • int, long, short, float, double, byteor aclass name
Introduction to Java • Methods/Messages Syntax: <access_qualifier> <return_type> methodName (arg1, arg2, …, argN) { method_body }
Java Methods • <access_qualifier> • public private • protected abstract • final • <return_type> • void, int, long, float, … or class name
Introduction to Java • Application are either: • Stand-alone. • Browser-Based. • Or both.
Introduction to Java • Stand-alone application • In a stand-alone application, JAVA’s run-time transfers execution to a specific method named main • The main method must be defined as public and static and should return no valuevoid. • And one parameter as an array of Strings
Introduction to Java • Example: class HelloWorld { … methods and members public static void main(String Args[]) { // method body } … more methods }
Introduction to Java • Simple Stand-alone application class HelloWorld { public static voidmain(String Args[]) { system.out.println(“Hello World\n”); } }
Introduction to Java • Browser-Based Application (Applet) • Must derive from java.applet.Applet • Must define the following methods • public void init() • public void start() • public void stop() • public void destroy() • public void paint(Graphics g)
A Class Definition main() and/or init(),start(), stop(), destroy() are defined Introduction to Java • Let’s put it together - structure of Java program Optional Package Statement Optional import Statements Class Definition(s)
Introduction to Java • Language Constructs and Semantics • Data types & Variables • Operators • Statements • Expression • Block • Assignment • Flow Control • Classes
Introduction to Java • Built-in data types - integer numbers • byte = 8-bit signed integer -128 to +127 • short = 16-bit or 2-byte signed integer • int = 32-bit or 4-byte • long = 64-bit or 8-byte.
Introduction to Java • Built-in data types - Real Numbers • float = 32-bits • double = 64-bits • Both float and double are defined according to IEEE 754-1975.
Introduction to Java • Built-in data types - characters & strings • Sequence of characters enclosed in double quotes are string literals - string is not a type. String is class Ex: “blue” ‘A’ “black dog” ‘c’ “1st line\n2nd line”
Introduction to Java • Built-in data type - Boolean • Boolean = can hold two values (true,false) • Boolean variables can only be involved in logical operation or expressions that evaluate to (true or false)
Introduction to Java • Variables must start with a letter followed by any number of letters, digits, underscore ( _ )or the dollar sign ($). Ex: MyHouse valid Her_Car_$ valid My_2nd_Car valid 12_ab_$ Invalid
Introduction to Java • Arithmetic Operators: • Binary operators • *, / multiplication and division • +, - addition and subtraction • % remainder • <<, >> shift left and shift right. • >>> shift-right fill zero extension.
Introduction to Java • Arithmetic Operators • Unary Operators • ++, -- increment and decrement they can appear before the variable and is called pre-incrementing or pre-decrement ex: ++a , --b and are equivalent to a=a+1, b=b+1. • - unary negative sign.
Introduction to Java • Assignment a = b for basic types, old value of a disappears to be replaced by the value of b. for objects a and b are pointing to the same object that is the object b is pointing to. a += b, is the same as a = a + b a -= b is the same as a = a - b a *= b is the same as a = a * b
Introduction to Java • Expressions • Operators are evaluated left-to-right. • Operators are grouped into16 precedence level.
Introduction to Java • Java evaluates expressions with mixed data types. • Implicit conversion occurs among basic data type byte, int, short, long, float, double • Implicit conversion promote types in an attempt to preserve precession.
Introduction to Java • Block statement • any number of statements enclosed within a pair of curl-brackets { } • Examples • body of main() • body of class • for any reason
Introduction to Java • Flow control statements (conditional) • if (BooleanExpression) statements-block-1 [ else statements-block-2 ] when BooleanExpression evaluates to true block-1 is executed, otherwise block-2 if it exists.
True False BooleanExpression If-then-else Java Statements Java Statements Introduction to Java
Introduction to Java True BooleanExpression If-then Java Statements False
Introduction to Java • Flow Control statements (branching) • switch (expression) case const_1: statement-block-1 case const_2: statement-block-2 case …. [default: statement-block-n ] value evaluated for expression should match one of the const_n otherwise the default-block is executed.
Introduction to Java Eval. switch C1? True C2? False C3?
Introduction to Java • Flow Control (branching) • Break [label] • continue [label] • return [ expression] All three statement cause execution to branch to the Label or return to the calling program.
Introduction to Java • Control Flow (repetition) • while (BooleanExpression) statement-block • do statement-block while(booleanExpression) • for (exp1; BooleanExpression; exp2) statement-block
Introduction to Java While-stmt Ex? True False
Introduction to Java True Ex? False
Introduction to Java Eval I Is I? Inc.
Introduction to Java • Classes - definition [<modifiers>] class <classname> { class_methods and Data … } • <Modifiers> • public - protected -abstract • private - final