Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
MSI 692: Special Topics in Information Technology Sanjay Goel University at Albany, SUNY Fall 2004 Sanjay Goel, School of Business, University at Albany, SUNY 1 Outline for the Class Part 1 • • • • • Introduction What is Java? How is Java different from other languages? Tools for Programming in Java Java Language – Lexical elements – – – – – Keywords Identifiers Literals Operators Punctuation Sanjay Goel, School of Business, University at Albany, SUNY 2 Outline for the Class Part 2 • • • • Data Types Variables and initializations Methods Numbers – – – – Types Ranges Expressions Conversions • Operators – Assignment and Increment – Precedence and Associativity Sanjay Goel, School of Business, University at Albany, SUNY 3 Programming Sanjay Goel, School of Business, University at Albany, SUNY 4 Programming Why do we need to write programs? • Humans talk in a different language than computers do and we need to translate human talk into computer talk or instructions. – You write the program in a language you are comfortable with – The compiler takes your program and smooshes it, digests it, and creates a stream of zeros and ones for the computer to understand. • Normally, different computers have different languages that they understand similar to the various human languages (i.e. French, English, Sanskrit & German) • Thus, we have different compilers for different computers. Sanjay Goel, School of Business, University at Albany, SUNY 5 Programming What are the advantages of Java? • Eliminates pitfalls of C/C++ by eliminating pointer arithmetic and explicit memory management. • Is object-oriented which helps enforce better programming techniques and to better visualize the code • Interpreted environment which improves speed of development – Reduces compile-link-load cycle – Is portable across multiple platforms • Runs more than one parallel activity via threads • Enables security by checking code modules that are loaded Sanjay Goel, School of Business, University at Albany, SUNY 6 Programming How is Java different from other languages? • Uses a Java Virtual Machine (JVM) – JVM is a byte-code interpreter unique for different hardware – Takes burden of compling on multiple hardware away from user • C/C++: Source Compiler Machine Code • Java: Source Java Compiler Byte Code Java Virtual Machine Machine Code • Garbage Collection – No memory management • Code Security Sanjay Goel, School of Business, University at Albany, SUNY 7 Programming What are the advantages? • Expands your programming horizons across the enterprise – You are able to write programs which span across the internet – You can have a database sitting in one location on a compute server in another location and a web server in a third place which will communicate – Java makes it easier to program across the network – You can develop user interfaces • All these facilities are available as extensions of the basic core Java language. – We are going to focus on the core language in this class Sanjay Goel, School of Business, University at Albany, SUNY 8 Programming IDE Options • Requirements for Development of Programs – Editor (Emacs/Wordpad) – Compiler (Java SDK for Windows) – Execution Environment (Unix Shell/DOS Prompt) • Several Different Development Environments exist – – – – – BlueJ JBuilder Forte from Java JDeveloper from Oracle Java Visual Café • If any one wants to install java on their personal computers a link to the instructions is located on the syllabus. – Please interact with Tony Manes or D.J. Anderson with questions about this installation. – Come to me if they are unavailable or unable to help. Sanjay Goel, School of Business, University at Albany, SUNY 9 Language Constructs Sanjay Goel, School of Business, University at Albany, SUNY 10 Language Constructs Introduction • Like any language a programming language has some symbols and some rules to combine these symbols to make sense. – A compiler breaks individual characters the program into tokens. These tokens are then interpreted with respect to the context they exist in and processed. • Most of the previous languages were based on the 127 ASCII characters – Adequate representation to the English language keyboard. • Java is based on Unicode that has 64000 characters – Allows representation of multiple languages other than English for programming. Sanjay Goel, School of Business, University at Albany, SUNY 11 Language Constructs Types of Tokens • There are five types of tokens which are processed: – – – – – Keywords Identifiers Literals Operators Punctuation • There are two types of tokens that are not processed – White Space – Comments Sanjay Goel, School of Business, University at Albany, SUNY 12 Language Constructs White Space • Uses – Used to separate tokens in program that are not separated by separators – Make the program more readable • There are three types of white spaces – space (when you hit the space bar) – tab (when you hit the tab key) – newline (when you hit the newline key) Sanjay Goel, School of Business, University at Albany, SUNY 13 Language Constructs Comments • Comments are used for documenting the code. • There are 3 types of comments – Single line comments // This is a single line comment – Multi-line comment /* This is a multi-line comment So write multiple lines here or only one if you choose to */ – javadoc comments /** This is a javadoc comment to generate documentation at the end of the code @author Sanjay Goel @deprecated */ Sanjay Goel, School of Business, University at Albany, SUNY 14 Keywords Reserved words with special meaning • These are reserved words in the language and have a special meaning to them. – There are 47 key words in Java language. – The keyword is separated from other keywords or identifiers by white space, comment or punctuation. • const and goto have no meaning in java but had meaning in C & C++. – They have been included to facilitate error reporting when users use these words in java – null, true and false are not keywords but are literals. Sanjay Goel, School of Business, University at Albany, SUNY 15 Keywords Keywords in Java • The keywords in the Java language are as follows: – – – – – – – – – – – – abstract double int strictfp boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto protected transient const if public try continue implements return void default import short volatile do instanceof static while Sanjay Goel, School of Business, University at Albany, SUNY 16 Identifiers Naming the Java elements • These are the names that used to specify elements of the Java program, such as class variable or method i.e. – These are the names that you assign to different constructs of the program. • An identifier is any sequence of Java letters and digits. – The first of that must be a Java letter. – A key word can not be an identifier and the literals null, true and false can not be identifiers. • Java letters include the upper and lower case letters of the English alphabet – i.e. a-z, A-Z, $, and underscore "_" • Java digits are 0-9 Sanjay Goel, School of Business, University at Albany, SUNY 17 Literals Value representations for primitive numeric types • Java has built in types for numbers, characters and Booleans. – It also has a built in class for String. • Literals are the value representations for the primitive numeric types, the Boolean and character as well as the standard class type of String. – – – – – – – – – String String char char Boolean Boolean double int int "Carrot“ "Emily“ 'a‘ 'b‘ true false 3.21 12 234234 Sanjay Goel, School of Business, University at Albany, SUNY 18 Operators Mathematical • Operators – /, *, +, -, = • Precedence – Hierarchy that determines the order in which operators are evaluated • Associativity – Order in which operators of equal precedence are evaluated (Left to Right or Right to Left) • BODMAS – Binary operation of Division, Multiplication, Addition, Subtraction Sanjay Goel, School of Business, University at Albany, SUNY 19 Operators Relational • They determine the relationship between two operands (values). • They are used in a condition expression in order to test whether a particular kind of relationship exists between two operands. • All relational operators return a Boolean Value, which evaluates if the relationship between the operands is true or false. • Java utilizes a conditional expression, which uses one or more relational operators to discover if the statement is true or false. Sanjay Goel, School of Business, University at Albany, SUNY 20 Operators Relational • Relational Operators: Equal to == Not equal to != Greater than > Less than < Greater than or equal to >= Less than or equal to <= • Example: The if..else statement. If the expression is true, execute the if statement, and if it is false, execute the else statement. b < = a • If b is less than or equal to a, then the expression is true, and the if statement is executed. If b is greater than a, then the expression is false, and the else statement is executed. Sanjay Goel, School of Business, University at Albany, SUNY 21 Operators Logical • In order for Java to assess two relational expressions, they must be linked together using a logical operator. • Logical Operators: And && OR ll And & OR l Ternary if-then-else ?: • The AND Logical Operator (&&)- For the statement to be true, both relational expressions have to be true. If one or both of the expressions is false, then the AND logical operator returns a Boolean false. Sanjay Goel, School of Business, University at Albany, SUNY 22 Operators Logical • The OR Logical Operator (ll)- For the statement to be true, only one of the relational expressions has to be true. If both of the expressions are false, then the OR logical operator returns a Boolean false. • The Single AND and OR Logical Operators (&,l)- When using the && and ll Operators, only the first expressions are evaluated. If the first expression is false in the && Operator, then the second does not need to be evaluated. If the first expression is true in the ll Operator, then the second does not need to be evaluated. However, when using the single AND and OR Operators, both expressions need to be evaluated regardless of if they return a Boolean true or false. • The Ternary Operator (?:)- Uses a relational expression and two values. If the relational expression is true, the ternary operator uses the first value. If it is false, it uses the second value. Sanjay Goel, School of Business, University at Albany, SUNY 23 Operators Bitwise • Bitwise Operators help the programmer change the value of a bit (binary digit) from zero to one, or one to zero. • Bitwise AND Operator (&)- It compares two bits. If both of the bits are 1’s, then the AND operator returns a 1. If only one bit is 1 or both are 0, then it returns a 0. Example 00001111 & 00001010 00001010 • Bitwise Inclusive OR Operator (l)- If one or both bits are a binary 1, then a binary 1 is returned. If both bits are a binary 0, then a binary 0 is returned. Example 00001111 l 00001010 00001111 Sanjay Goel, School of Business, University at Albany, SUNY 24 Operators Bitwise • Bitwise Exclusive OR Operator (^)- If one bit is a binary 1, then a binary 1 is returned. If both bits are a binary 1, then a binary 0 is returned. If both bits are a binary 0, then a binary 0 is returned. Example- 00001111 ^ 00001010 00000101 • Bitwise Left Shift Operator (<<)- This operator moves bits one position to the left. Then leftmost bit then drops, and the rightmost bit is replaced with a 0. Example- 00001111 00011110 Sanjay Goel, School of Business, University at Albany, SUNY 25 Operators Bitwise • Bitwise Signed Right Shift Operator (>>)- This operator moves bits one position to the right. The empty position on the left is filled with a value that represents the same sign as the rightmost bit that was shifted. (positive bit = 0, negative bit = 1) Example- 10001111 10000111 • Bitwise Unsigned Right Shift Operator (>>>)- This operator moves bits one position to the right. The empty position on the left is always filled with a binary 0 (the sign of the bit is not significant). • Bitwise Complement Operator (~)- This operator reverses the values of the bits. Binary 1’s become Binary 0’s, and vice versa. Example- 00001111 11110000 • “Two’s Complement”- This term refers to reversing all of the bits, and then adding 1 to the result. Sanjay Goel, School of Business, University at Albany, SUNY 26 Punctuation Separators • These are the separators used in expressions i.e. – ";" – "," – "{ }” Sanjay Goel, School of Business, University at Albany, SUNY 27 Data Types and Variable Declarations Sanjay Goel, School of Business, University at Albany, SUNY 28 Data Types and Variable Declarations Introduction • Data type determines how data is represented in computer memory. Each variable has to have a defined type • There are 8 primitive types. – Numeric - byte, short, int, long, float, double – Character – char – Logic - boolean (can have value of true or false) • In addition there are built in classes like Button, Frame, Point, String. – There are more than a few thousand classes and new classes are added continuously. – There is a SUN community process that determines what new classes to add. • The data values of classes are called objects. Sanjay Goel, School of Business, University at Albany, SUNY 29 Data Types and Variable Declarations Variables • These are identifiers that are used to refer to values stored in the computers memory. – e.g. int i; – int j, k; • Variables can be initialized also – e.g. int j = 10; – boolean flag = true, iflag = false; – String sentence = "Who am I"; Sanjay Goel, School of Business, University at Albany, SUNY 30 Number Types Sanjay Goel, School of Business, University at Albany, SUNY 31 Number Types Types of Numbers • Two types – Integer representations – Floating point representations • A binary digit (bit) is the smallest unit of information that can be stored on the computer and can have a binary value i.e. 0 or 1. • A combinations of bits is used to represent larger range of values. • For integers bits are interpreted with the binary number system. – Integers can be represented as a combination of binary digits or bits. – Different precisions are used to store different numbers • Negative numbers use two’s complement representation – Allows use of binary arithmetic operations on signed integers – Positive 2's complement numbers are represented as simple binary. – Negative 2's complement numbers are represented as the binary number that when added to a positive number of the same magnitude equals zero. Sanjay Goel, School of Business, University at Albany, SUNY 32 Number Types Negative Numbers and Floating Point Types • Integral Types byte 8 -128 to 127 short 16 -32768 to 32767 char 16 0 to 65536 int 32 -2147483648 to –2147483647 long 64 -9223372036854775808 to 9223372036854775807 • Floating point types Float 32 Precision: 7 digits Double 64 Precision: 15 digits Sanjay Goel, School of Business, University at Albany, SUNY 33 Number Types Arithmetic Expressions • There are 5 basic arithmetic operators – – – – – Addition Subtraction Multiplication Division Modulus Sanjay Goel, School of Business, University at Albany, SUNY 34 Number Types Conversion • Any primitive numeric types can be used with these arithmetic operators. • If the operators are of different types the types are converted so that they are of the same type. • This conversion scheme is called the binary numeric promotion. – if either operand is double the other operand is converted to double – else if either operand is float other operand is converted to float – else if either operand is long other operand is converted to long – else both operands are converted to int. Sanjay Goel, School of Business, University at Albany, SUNY 35 Number Types Explicit Type Conversions • There are two type of conversions • Widening primitive conversions – These go from a lower resolution to a higher resolution type and thus there is no loss of information. – These conversions are sometimes done via binary numeric promotion – Example: int x = 1, y = 2; float z = x + y; Sanjay Goel, School of Business, University at Albany, SUNY 36 Number Types Explicit Type Conversions cont’d • Narrowing primitive conversions – These go from a higher resolution type to a lower resolution type and can lead to loss of resolution. – An explicit conversion is required via a cast operator to achieve this. – This is dangerous and should be done carefully int i = 127, j = 128; byte iAsByte = (byte)i; byte jAsByte = (byte)j; System.out.println(i); System.out.println(j); output: 127 -128 – Since the max positive number is 127 the number representation gets screwed up in memory.) Sanjay Goel, School of Business, University at Albany, SUNY 37 Number Types Operators • Assignment operator : "=“ – assigns the rhs to the lhs – i.e. i = 10; += -= *= %= >>= <<= &= ^= != • Increment and decrement operators ++i --i i++ i-- Sanjay Goel, School of Business, University at Albany, SUNY 38 Number Types Associativity and precedence • Precedence - hierarchy of evaluation • Associativity - If level is the same what is the order of evaluation • You can make the expression more explicit by using parentheses. – e.g. a + b * c vs. (a + b) * c Sanjay Goel, School of Business, University at Albany, SUNY 39 Building a Program Sanjay Goel, School of Business, University at Albany, SUNY 40 Building a Program Sample Program • Let us examine a simple program • Prior to dissecting this let us look at the various structures in the program. • Just like in writing a book you construct the basic characters into words, words into statements, statements into paragraph and paragraphs into chapters and chapters into books. • Similarly we can arrange the program constructs into a hierarchy. Sanjay Goel, School of Business, University at Albany, SUNY 41 Building a Program Methods • Methods are group of instructions with a name. • You can write very simple programs in a single monolithic block but programs can get large and as large as a few million lines. – You need to separate pieces out for ease of development and maintenance. • The way to separate them is writing methods which are a group of instructions with a name and signature. – Depending on the size of the program there can be many methods. – Some methods are already defined in the language. • main method – The main method is a special method. The program starts execution from the main method. public static void main(String args[]) { } Sanjay Goel, School of Business, University at Albany, SUNY 42 Building a Program Writing to console • System.out.print(ln) – System.out.print("I want to test without carriage return"); – System.out.println("I want to test with carriage return"); Hello World program /** Hello world program for class lecture * @author Sanjay Goel * @created August 26, 2001 * */ import java.io.*; public class HelloWorld { public static void main(int args[]) { String string1 = "Hello "; String string2 = "World"; System.out.println(string1 + string2); } } Sanjay Goel, School of Business, University at Albany, SUNY /** Program to sum two numbers * @author Sanjay Goel * @created August 26, 2001 * */ import java.io.*; public class Sum{ int a = 10; int b = 30; int sum = a + b; System.out.println("a = " + a + " b = " + b + " Sum = " + sum); } 43 Review Sanjay Goel, School of Business, University at Albany, SUNY 44 Recap Keywords and Identifiers • Keywords – Reserved words which have special purpose and can’t be used for any thing else – 47 in total – new words may be added as language expands • Identifiers – Also called variables are names of the data elements which you have stored in memory – Formed by sequence of Java letters and digits – All keywords and three literals (true, false, null) can not be used as identifiers – Note: Java letters are A-Z, a-z, underscore & $ – Java numbers 0-9 Sanjay Goel, School of Business, University at Albany, SUNY 45 Recap Literals • Literals – These are the values you assign to the variables – The variables have to be of specific types • Types of Literals – Numeric • Whole Numbers – integer type numerics • Fractional Numbers – floating point type numerics – Char • single character, digit, symbol – Boolean logic – Strings – Text Sanjay Goel, School of Business, University at Albany, SUNY 46 Recap Numeric Types • The numeric types are: – – – – – – – Byte Short Int Char Long Float Double 8 bits 16 bits 32 bits 32 bits 64 bits 32 bit 64 bits • Why do we have so many different types? – To economize on use of memory – We use more memory for larger whole numbers or for fractional numbers with higher precision. • The ranges were discussed in the previous class. Sanjay Goel, School of Business, University at Albany, SUNY 47 Recap Operators, Punctuation, White Space & Comments • Operators – These are the symbols used for arithmetic operations on numbers – Unary operators operate only on one number ++, -– Binary operators operate on two numbers +, -, /, % • Punctuation – These are the elements that act as separators – ; {} [] , • White Space – Spaces, tabs, newline • Comments – Three types: Single line, multiline, Javadoc comment Sanjay Goel, School of Business, University at Albany, SUNY 48 Recap Initialization, Methods, and I/O • Variable initialization – Specify type, value, and name – Terminated by a semicolon • Methods – User Defined – System Defined • Simple I/O – System.out.println(“This is a test”); – Console.in.readInt(); Sanjay Goel, School of Business, University at Albany, SUNY 49 Recap Arithmetic Expressions and Conversion • Arithmetic Expressions – Syntax – Mixed mode arithmetic – Binary numeric promotion • Type conversion • Widening primitive conversions – Goes from lower precision to higher precision – Can be automatic or an explicit cast • Narrowing primitive conversions – Has to be explicit Sanjay Goel, School of Business, University at Albany, SUNY 50 Recap Arithmetic Operators, Associativity and Precedence • Arithmetic Operators – Variable = expression (a = b + c) – Assignment (=, +=, -=, *=, /=, %=) – Increment and Decrement Operators (++, –-) • Precedence – order in which operators are evaluated • Associativity – Order in which operators of same precedence are evaluated (L->R, R->L) • You can make the expression more explicit by using parentheses. Sanjay Goel, School of Business, University at Albany, SUNY 51