Object-Oriented Programming - Lecture 1-bis: Language Basics - Lê Hồng Phương

● Variables ● Operators ● Expressions, statements and blocks ● Control flow statements Variables ● As you learned in the previous lesson, an object stores its state in fields. ● In the Java programming language, the terms “field” and “variable” are both used.

pdf24 trang | Chia sẻ: candy98 | Lượt xem: 473 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Object-Oriented Programming - Lecture 1-bis: Language Basics - Lê Hồng Phương, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Lecture 1-bis: Language Basics Lê Hồng Phương phuonglh@gmail.com Department of Mathematics, Mechanics and Informatics, Vietnam National University, Hanoi 2012-2013 2 Content ● Variables ● Operators ● Expressions, statements and blocks ● Control flow statements 2012-2013 3 Content ● Variables ● Operators ● Expressions, statements and blocks ● Control flow statements 2012-2013 4 Variables ● As you learned in the previous lesson, an object stores its state in fields. ● In the Java programming language, the terms “field” and “variable” are both used. ● A field is a kind of variable. public class Bicycle { int cadence = 0; int speed = 0; int gear = 1; } 2012-2013 5 Kinds of variables ● Instance variables (non-static fields) – Non-static fields are called instance variables because their values are unique to each instance of a class. ● Example: – The speed of one bike is independent of the speed of another. 2012-2013 6 Kinds of variables ● Class variables (static fields) – A field declared with the static modifier. – There is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. ● Example: add a field to define the number of gears for a particular kind of bicyle. public class Bicycle { int cadence = 0; int speed = 0; int gear = 1; static int numGears = 6; } 2012-2013 7 Kinds of variables ● Local variables – A method often stores its temporary state in local variables. – The syntax for declaring a local variable is similar to declaring a field (Ex: int count = 0;) – Local variables are only visible to the methods in which they are declared, they are not accessible from the rest of the class. 2012-2013 8 Kinds of variables ● Parameters – Parameters of a method are always classified as variables, not fields. public static void main(String[] args) { } 2012-2013 9 Naming convention ● Variable names are case-sensitive: – Unlimited-length of Unicode letters and digits – Beginning with a lowercase letter ● If a name consists of only one word, spell that word in all lowercase letters. ● If it consists of more than one word, capitalize the first letter of each subsequent word. Ex: float basicSalary; String savingAccountName; 2012-2013 10 Primitive data types ● Java supports 8 primitive data types: – byte: 8-bit, signed integer, [-128..127] – short: 16-bit, signed integer, [-32,768..32,767] – int: 32-bit, signed int, [-2,147,483,648..-2,147,483,647] – long: 64-bit, signed integer – float: 32-bit floating point (IEEE 754) – double: 64-bit floating point (IEEE 754) – boolean: has only two possible values: true or false. – char: 16-bit Unicode character, [0..65,535], [\u0000..\uFFFF] 2012-2013 11 Literals ● Integer and floating-point literals: ● Character and string literals: – Any Unicode (UTF-16) characters – Escape chars: \b, \t, \n, \f, \r, \”, \', \\ int decVal = 26; // The number 26, in decimal int hexVal = 0x1a; // The number 26, in hexadecimal int binVal = 0b11010; // The number 26, in binary double d1 = 123.4; double d2 = 1.234e2; float f1 = 123.4f; 2012-2013 12 Arrays ● An array is a container object that holds a fixed number of values of a single type. ● The length of an array is established when the array is created. After creating, its length is fixed. ● Each item in an array is called an element, each element is accessed by its numerical index. 2012-2013 13 Arrays ● Declaring an array: ● Creating, initializing, accessing an array: int[] anArray; int[] anArray; anArray = new int[10]; anArray[0] = 100; anArray[1] = 200; anArray[2] = 300; int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 }; 2012-2013 14 Content ● Variables ● Operators ● Expressions, statements and blocks ● Control flow statements 2012-2013 15 Operators ● Operators are special symbols that perform specific operations on one, two or three operands and then return a result. ● Operators listed according to precedence order: – postfix, unary, multiplicative, additive, shift, relational, equality, bitwise, logical, ternary, assignment 2012-2013 16 Operators ● postfix: expr++, expr-- ● unary: ++expr, --expr, +expr, -expr, ~, ! ● multiplicative: *, /, % ● additive: +, - ● shift: > ● relational: , =, instanceof ● equality: ==, != ● bitwise AND: & ● bitwise exclusive OR: ^ ● bitwise inclusive OR: | ● logical AND: && ● logical OR: || ● ternary: ? : ● assignment: =, +=, -=, *=, /=, %=, &=, ^=, |=, >= 2012-2013 17 Content ● Variables ● Operators ● Expressions, statements and blocks ● Control flow statements 2012-2013 18 Expressions, statements and blocks ● An expression is a construct made up of variables, operators and method invocations that evaluates to a single value. ● A statement is roughly equivalent to a sentence in natural languages. – It forms a complete unit of execution. – Statements are separated by semicolons (;). ● A block is a group of 0 or more statements between balanced braces. 2012-2013 19 Content ● Variables ● Operators ● Expressions, statements and blocks ● Control flow statements 2012-2013 20 Control flow statements ● The statements inside source files are generally executed from top to bottom, in the order that they appear. ● Control flow statements break up the flow of execution by employing decision making, looping, and branching. – Decision making statements: if-then, if-then-else, switch – Looping statements: for, while, do-while – Branching statements: break, continue, return 2012-2013 21 The if-then, if-then-else statements void applyBrakes() { // the "if" clause: bicycle must be moving if (isMoving){ // the "then" clause: decrease current speed currentSpeed--; } } void applyBrakes() { if (isMoving) { currentSpeed--; } else { System.err.println("The bicycle has alreadly stopped!"); } } 2012-2013 22 The switch statement int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } 2012-2013 23 The while, do-while and for statements while (expression) { statements; } do { statements; } while (expression); What is the difference between the two statements? for (initialization; termination; increment) { statements; } ● The initialization is executed once, when the loop begins. ● When the termination expression evaluates to false, the loop terminates. ● The increment expression is invoked after each iteration. 2012-2013 24 Branching statements ● The break statement terminates the innermost switch, for, while or do-while statement. ● The continue statement skips the current iteration of a for, while or do-while loop. ● The return statement exists from the current method and control flow returns to where the method was invoked. – return a value – does not return value
Tài liệu liên quan