1 / 198

Introduction to Object-Oriented Programming and Java

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.

szack
Télécharger la présentation

Introduction to Object-Oriented Programming and Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Object-Oriented Programming and Java

  2. 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.

  3. 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.

  4. 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

  5. Java 2 • Swing - new features for creating a graphical user interface • Improvements to audio features • Drag and drop

  6. 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.

  7. Introduction to Java • Java Overview • Secure, High performance. • Multi-Threaded. • Java is defined with a very comprehensive class and object libraries (packages).

  8. 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.

  9. 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.

  10. 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.

  11. 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).

  12. 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.

  13. 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.

  14. 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

  15. 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.

  16. 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; }

  17. Introduction to Java • <class_qualifier> is one of the following reserved keywords • public • private • abstract • final

  18. Introduction to Java • What’s in the class body? • Data members • Methods/messages that operate on the data.

  19. 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

  20. Introduction to Java • Methods/Messages Syntax: <access_qualifier> <return_type> methodName (arg1, arg2, …, argN) { method_body }

  21. Java Methods • <access_qualifier> • public private • protected abstract • final • <return_type> • void, int, long, float, … or class name

  22. Introduction to Java • Application are either: • Stand-alone. • Browser-Based. • Or both.

  23. 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

  24. Introduction to Java • Example: class HelloWorld { … methods and members public static void main(String Args[]) { // method body } … more methods }

  25. Introduction to Java • Simple Stand-alone application class HelloWorld { public static voidmain(String Args[]) { system.out.println(“Hello World\n”); } }

  26. 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)

  27. 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)

  28. Introduction to Java • Language Constructs and Semantics • Data types & Variables • Operators • Statements • Expression • Block • Assignment • Flow Control • Classes

  29. 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.

  30. 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.

  31. 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”

  32. 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)

  33. 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

  34. Introduction to Java • Arithmetic Operators: • Binary operators • *, / multiplication and division • +, - addition and subtraction • % remainder • <<, >> shift left and shift right. • >>> shift-right fill zero extension.

  35. 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.

  36. 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

  37. Introduction to Java • Expressions • Operators are evaluated left-to-right. • Operators are grouped into16 precedence level.

  38. 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.

  39. 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

  40. 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.

  41. True False BooleanExpression If-then-else Java Statements Java Statements Introduction to Java

  42. Introduction to Java True BooleanExpression If-then Java Statements False

  43. 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.

  44. Introduction to Java Eval. switch C1? True C2? False C3?

  45. 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.

  46. Introduction to Java • Control Flow (repetition) • while (BooleanExpression) statement-block • do statement-block while(booleanExpression) • for (exp1; BooleanExpression; exp2) statement-block

  47. Introduction to Java While-stmt Ex? True False

  48. Introduction to Java True Ex? False

  49. Introduction to Java Eval I Is I? Inc.

  50. Introduction to Java • Classes - definition [<modifiers>] class <classname> { class_methods and Data … } • <Modifiers> • public - protected -abstract • private - final

More Related