Computer Programming-2 Lecture 1 PDF
Document Details
Uploaded by SupportedLogarithm
2011
Dr. Sara A. shehab
Tags
Summary
This document is a set of lecture notes covering Computer Programming-2, focusing on the introduction to Java programming. It explains fundamental concepts and key characteristics of Java, along with examples.
Full Transcript
Computer Programming-2 Lecture 1 Dr.Sara A.shehab Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807...
Computer Programming-2 Lecture 1 Dr.Sara A.shehab Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Introduction to Java Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 ❑ Topics of the Review Essentials of object-oriented programming, in Java Java primitive data types, control structures, and arrays Using some predefined classes: – Math – JOptionPane, I/O streams – String, StringBuffer, StringBuilder – StringTokenizer Writing and documenting your own Java classes 3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Some Salient Characteristics of Java Java is platform independent: the same program can run on any correctly implemented Java system Java is object-oriented: – Structured in terms of classes, which group data with operations on that data – Can construct new classes by extending existing ones Java designed as – A core language plus – A rich collection of commonly available packages Java can be embedded in Web pages 4 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Java Processing and Execution Begin with Java source code in text files: Model.java A Java source code compiler produces Java byte code – Outputs one file per class: Model.class – May be standalone or part of an IDE A Java Virtual Machine loads and executes class files – May compile them to native code (e.g., x86) internally 5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Compiling and Executing a Java Program 6 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Classes and Objects The class is the unit of programming A Java program is a collection of classes – Each class definition (usually) in its own.java file – The file name must match the class name A class describes objects (instances) – Describes their common characteristics: is a blueprint – Thus all the instances have these same characteristics These characteristics are: – Data fields for each object – Methods (operations) that do work on the objects 7 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Grouping Classes: The Java API API = Application Programming Interface Java = small core + extensive collection of packages A package consists of some related Java classes: – Swing: a GUI (graphical user interface) package – AWT: Application Window Toolkit (more GUI) – util: utility data structures (important to CS 187!) The import statement tells the compiler to make available classes and methods of another package A main method indicates where to begin executing a class (if it is designed to be run as a program) 8 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ A Little Example of import and main import javax.swing.*; // all classes from javax.swing public class HelloWorld { // starts a class public static void main (String[] args) { // starts a main method // in: array of String; out: none (void) } } public = can be seen from any package static = not “part of” an object 9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Processing and Running HelloWorld javac HelloWorld.java – Produces HelloWorld.class (byte code) java HelloWorld – Starts the JVM and runs the main method 10 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ References and Primitive Data Types Java distinguishes two kinds of entities – Primitive types – Objects Primitive-type data is stored in primitive-type variables Reference variables store the address of an object – No notion of “object (physically) in the stack” – No notion of “object (physically) within an object” 11 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Primitive Data Types Represent numbers, characters, boolean values Integers: byte, short, int, and long Real numbers: float and double Characters: char 12 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Primitive Data Types Data type Range of values byte -128.. 127 (8 bits) short -32,768.. 32,767 (16 bits) int -2,147,483,648.. 2,147,483,647 (32 bits) long -9,223,372,036,854,775,808..... (64 bits) float +/-10-38 to +/-10+38 and 0, about 6 digits precision double +/-10-308 to +/-10+308 and 0, about 15 digits precision char Unicode characters (generally 16 bits per char) boolean True or false Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 13 rights reserved. 0132130807 ❑ Primitive Data Types (continued) 14 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Operators 1. subscript [ ], call ( ), member access. 2. pre/post-increment ++ --, boolean complement !, bitwise complement ~, unary + -, type cast (type), object creation new 3. * / % 4. binary + - (+ also concatenates strings) 5. signed shift >, unsigned shift >>> 6. comparison < >=, class test instanceof 7. equality comparison == != 8. bitwise and & 9. bitwise or | 15 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Operators 11. logical (sequential) and && 12. logical (sequential) or || 13. conditional cond ? true-expr : false-expr 14. assignment =, compound assignment += -= *= /= = >>>= &= |= 16 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Type Compatibility and Conversion Widening conversion: – In operations on mixed-type operands, the numeric type of the smaller range is converted to the numeric type of the larger range – In an assignment, a numeric type of smaller range can be assigned to a numeric type of larger range byte to short to int to long int kind to float to double 17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Declaring and Setting Variables int square; square = n * n; double cube = n * (double)square; – Can generally declare local variables where they are initialized – All variables get a safe initial value anyway (zero/null) 18 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Referencing and Creating Objects You can declare reference variables – They reference objects of specified types Two reference variables can reference the same object The new operator creates an instance of a class A constructor executes when a new object is created Example: String greeting = ″hello″; 19 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Java Control Statements A group of statements executed in order is written – { stmt1; stmt2;...; stmtN; } The statements execute in the order 1, 2,..., N Control statements alter this sequential flow of execution 20 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Java Control Statements (continued) 21 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Java Control Statements (continued) 22 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java ❑ Methods A Java method defines a group of statements as performing a particular operation static indicates a static or class method A method that is not static is an instance method All method arguments are call-by-value – Primitive type: value is passed to the method – Method may modify local copy but will not affect caller’s value – Object reference: address of object is passed – Change to reference variable does not affect caller – But operations can affect the object, visible to caller 23 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Appendix A: Introduction to Java Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807