Starting Out With Java Lecture Notes PDF
Document Details
Uploaded by WellConnectedPhiladelphia
GIFT University
2020
Dr. Muhammad Faheem
Tags
Summary
This document is a lecture set on Java programming. Topics covered include fundamental programming concepts, comments, data structures, and output. The lecture set was presented on November 24, 2020.
Full Transcript
Starting Out with Java From Control Structures through Objects Session 2 (Chapter 2): Java Fundamentals Dr. Muhammad Faheem November 24, 2020 1 / 25 Parts of a Java Program Comments: Ignored by...
Starting Out with Java From Control Structures through Objects Session 2 (Chapter 2): Java Fundamentals Dr. Muhammad Faheem November 24, 2020 1 / 25 Parts of a Java Program Comments: Ignored by the compiler! (Introduced later!) Example: // This is a comment! Class Header: Consists of Access Modifier (e.g. public), class keyword, and name (e.g. Student) of class. Curly Braces: Associated with Class Header: Define scope of the class. Associated with Method: Define the scope of method. 2 / 25 Parts of a Java Program Main Method: Every Java application must have main method. The main method starts the Java program. Java Statements: When the program runs, the statements within the main method will be executed. Class headers, method headers, and curly braces does not end with semicolon. Example: System.out.println("Programming is a great fun!"); 3 / 25 Analyzing the Example Read the comments of Code Examples for Session 2! 4 / 25 Console Output Performing output in Java, as well as many other tasks, is accomplished by using the Java API (Application Programmer Interface). API is a standard library of prewritten classes for performing specific operations. Java statements to print a message on screen. System.out.println("Java is a Programming Language"); System.out.print("GIFT University"); System is a class that is part of Java API. System class holds out object as well as other members. println, and print are member of object class. 5 / 25 Console Output println Method: Cursor advances to the beginning of next line after printing the message. Next item will be printed on the screen will begin in this position. print Method: Similar to println, however does not advance the curson to next line after printing message. Value printed on screen is placed within parentheses. Value is known as arguement for above methods. 6 / 25 Example: Console Output See the Code Example 2 for Session 2! 7 / 25 Comments Comments are for the reader, not for the compiler. Three Types: Single line: begin with // // This is a C++ program. // Welcome to C++ Programming. Multiple line: enclosed between /∗ and ∗/ Javadoc comment: enclosed between / ∗ ∗ and ∗/ 8 / 25 Java Tokens Token: The smallest individual unit of a program written in any language. Java tokens include keywords, operators, special symbols, constants, and identifiers. 9 / 25 Special Characters // double slash Marks the beginning of a single line comment. () open and close parenthesis Used in a method header to mark the parameter list. {} open and close curly braces Encloses a group of statements, such as the contents of a class or a method. "" quotation marks Encloses a string of characters, such as a message that is to be printed on the screen. ; semi-colon Marks the end of a complete programming statement. 10 / 25 Java Escape Sequence \n newline Advances the cursor to the next line for subsequent printing. \t tab Causes the cursor to skip over to the next tab stop. \b backspace Causes the cursor to back up, or move left, one position. \r carriage return Causes the cursor to go to the beginning of the current line, not the next line. \\ backslash Causes a backslash to be printed. \’ single quote Causes a single quotation mark to be printed. \" double quote Causes a double quotation mark to be printed. 11 / 25 Java Escape Sequences Escape sequences are comprised of two characters but treated by compiler as a single Character. Starts with a backslash character (\) and end with a control character. Example: System.out.print("Web Crawlers:\n"); System.out.print("\t Heritrix\n\t IMF Crawler \n "); System.out.print("\t ACEBot"); Output: Web Crawlers: Heritrix IMF Crawler ACEBot 12 / 25 Example: Java Escape Sequences See the Code Example 3 for Session 2! 13 / 25 Identifiers Identifiers are name of something that appears in a program, such as variables, constants, functions and classes. Java rules for Identifiers: Consists of letters, digits, underscore character (_), and dollar sign ($). Must not begin with a digit. Java is case sensitive (i.e., Number is not the same as number). Reserved keywords may not be used as Identifiers. Identifiers cannot include spaces. 14 / 25 Identifiers Examples of legal Identifiers: first1 _second employeeSalary Examples of illegal Identifiers: Hello! one+two employee Salary 3rd 15 / 25 Naming Conventions Identifiers should be descriptive. Identifiers can be self-documenting: CENTIMETERS_PER_INCH Avoid run-together words, e.g., annualsale can be renamed as annualSale (known as camel case) or annual_sale. Use lower-camel case for variable names. Use upper-camel case for class names (e.g. MountainBike). Java naming conventions: http://java.sun.com/docs/ codeconv/html/CodeConventions.doc8.html 16 / 25 Reserved word symbols (Keywords) Cannot be redefined within program. Cannot be used for anything other than their intended use. Examples: - int - short - float - double - char - final - void - return See Table 1.3 for complete list of keywords. 17 / 25 Whitespaces Every Java program contains whitespaces. Include blanks, tabs, and newline characters. Used to separate special symbols, reserved words, and identifiers. Proper utilization of whitespaces is important. Can be used to make the program more readable. 18 / 25 Variable A named Memory Location whose contents can be changed. A variable is accessed using name. 19 / 25 Variable Declaration To declare a variable, must specify its data type. Syntax: dataType identifier; Examples: int counter; long interestRate; char grade; 20 / 25 Assignment Statement A value can assigned to a variable using assignment operator (i.e. =). Syntax: dataType identifier = expression; //Variable declaration and initialization OR identifier = expression; //Assumed identifier is already declared! int counter = 1; //Variable declaration and initialization OR counter = 2; //Assumed identifier is already declared! 21 / 25 Example: Variable Initialization Examples: int noOfStudents = 40; String className = "Introduction to Programming"; double avgCGPA = 3.1; Data Types will be introduced latter! 22 / 25 Example: Printing variable Value Example: int noOfStudents = 40; System.out.print("Number of Students: ") System.out.println(noOfStudents); // Note: No quatation marks Output: Number of Students: 40 23 / 25 Example: Printing variable Value See the Code Example 4 for Session 2! 24 / 25 Credit Tony Gaddis, Starting out with Java: From Control Structures through Objects, 6th Edition, Pearson 2016. 25 / 25