1 Web Programming with Java Java Basics Huynh Huu Viet Email: viethh@uit.edu.vn Department of Information Systems 2008 © Department of Information Systems - University of Information Technology 2 Outline Primitive Data Types and Operations Control Structures Selection Statements Loop Statements Methods Arrays Q&A 2008 © Department of Information Systems - University of Information Technology 3 Identifiers Identifiers: Names for the things you will refer to in your programs Used for programming entities as variables, constants, methods, classes, and packages Rules for naming identifiers: A sequence of characters that consists of letters, digits, underscores (_), and dollar signs ($). Cannot start with a digit. Cannot be a reserved word. Cannot be true, false, or null. An identifier can be of any length. 2008 © Department of Information Systems - University of Information Technology 4 Variables & Constants Variables: used to store data in a program datatype variableName; datatype variable1, variable2, , variablen; Constants Represents permanent data that never changes final datatype CONSTANTNAME = VALUE; What is benefits of using constants ??? • Don't have to repeatedly type the same value; • Can be changed in a single location if necessary; • Makes the program easy to read. 2008 © Department of Information Systems - University of Information Technology 5 Data Types and Operations (1) 2008 © Department of Information Systems - University of Information Technology 6 Data Types and Operations (2) 2008 © Department of Information Systems - University of Information Technology 7 Numeric Type Conversions Conversions rules If one of the operands is double, the other is converted into double. Otherwise, if one of the operands is float, the other is converted into float. Otherwise, if one of the operands is long, the other is converted into long. Otherwise, both operands are converted into int. 2008 © Department of Information Systems - University of Information Technology 8 Operand Evaluation Order The rule of evaluating an expression: Rule 1: Evaluate whatever sub-expressions you can possibly evaluate from left to right. Rule 2: The operators are applied according to their precedence Rule 3: The associativity rule applies for two operators next to each other with the same precedence You can use parentheses to force an evaluation order Does parentheses slow down the execution of the expression??? 2008 © Department of Information Systems - University of Information Technology 9 Operator Precedence (1) 2008 © Department of Information Systems - University of Information Technology 10 Operator Precedence (2) Example: int a = 0; int x = a + (++a); Îx=? int a = 0; int x = ++a + a; Îx=? [...]... else Statements if (booleanExpression) { stat(s)-for-the-true-case; } else { stat(s)-for-the-false-case; } 2008 © Department of Information Systems - University of Information Technology 16 if Statements - notes Common mistake else matching 2008 © Department of Information Systems - University of Information Technology 17 switch Statements switch (switch-expression) { case value1: statement(s)1; break;... (loop-continuation-condition) { // Loop body Statement(s); } 2008 © Department of Information Systems - University of Information Technology 21 do-while Loop do { // Loop body; Statement(s); } while (loop-continuation-condition); 2008 © Department of Information Systems - University of Information Technology 22 for Loop for (initial-action; loop-continuation-condition; action-after-each-iteration) { //... = -1 ; y = (x > 0) ? 1 : -1 ; Why we need this operator??? 2008 © Department of Information Systems - University of Information Technology 19 Control Structures Selection Statements if Statements switch Statements Conditional Expressions Loops while Loop do-while Loop for Loop break and continue 2008 © Department of Information Systems - University of Information Technology 20 while Loop while (loop-continuation-condition)... statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; } switch-expression must yield a value of char, byte, short, or int The value1, , and valueN must have the same data type as the value of the switch-expression and be constant expressions 2008 © Department of Information Systems - University of Information Technology 18 Conditional Expressions Syntax: • booleanExpression... Systems - University of Information Technology 23 Loops Which Loop to Use? Common errors: 2008 © Department of Information Systems - University of Information Technology 24 break and continue break Immediately ends the innermost loop that contains it Generally used with an if statement continue Only ends the current iteration Generally used with an if statement 2008 © Department of Information Systems -. .. of Information Systems - University of Information Technology 28 Example Compilation error: 2008 © Department of Information Systems - University of Information Technology 29 Overloading Methods Two methods have the same name but different parameter lists (signature) within one class Overloading methods ?? Overload method Some examples 2008 © Department of Information Systems - University of Information...boolean Data Type & Operations Java also provides & and | operators (unconditional) A little bit different from && and || operators (conditional) (x != 0) & (100 / x) ??? 2008 © Department of Information Systems - University of Information Technology 11 Programming Errors (1) Syntax errors (compilation errors) public class ShowSyntaxErrors {... © Department of Information Systems - University of Information Technology 12 Programming Errors (2) Logic Errors public class ShowLogicErrors { public static void main(String[] args) { // Add number1 to number2 int number1 = 3; int number2 = 3; number2 += number1 + number2; System.out.println("number2 is " + number2); } } 2008 © Department of Information Systems - University of Information Technology... switch Statements Conditional Expressions Loops while Loop do-while Loop for Loop break and continue 2008 © Department of Information Systems - University of Information Technology 14 Outline Primitive Data Types and Operations Control Structures Selection Statements Loop Statements Methods Arrays Q&A 2008 © Department of Information Systems - University of Information Technology 15 if Statements Simple... myList = {1.9, 2.9, 3.4, 3.5}; 2008 © Department of Information Systems - University of Information Technology 32 Array Basics (2) foreach Loops (JDK 1.5 above) Syntax: for (elementType element: arrayRefVar) { // Process the element } Ex for (double element: myList) { System.out.println(element); } 2008 © Department of Information Systems - University of Information Technology 33 Copying Arrays Using assignment . 1 Web Programming with Java Java Basics Huynh Huu Viet Email: viethh@uit.edu.vn Department of Information Systems 2008 © Department of Information Systems - University of Information. { stat(s)-for-the-true-case; } else { stat(s)-for-the-false-case; } 2008 © Department of Information Systems - University of Information Technology 17 if Statements - notes Common mistake else matching. Systems - University of Information Technology 16 if Statements Simple if Statements if else Statements if (booleanExpression) { statement(s); } if (booleanExpression) { stat(s)-for-the-true-case; }