350 likes | 364 Views
Программирование на Java: Основы языка. Вячеслав Гребенюк ЦТДО, каф. ИИ, ХНУРЭ. Content. Language Basics.
E N D
Программирование на Java: Основы языка Вячеслав Гребенюк ЦТДО, каф. ИИ, ХНУРЭ
Content (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Language Basics • public class BasicsDemo { public static void main(String[] args) { int sum = 0; for (int current = 1; current <= 10; current++) { sum += current; } System.out.println("Sum = " + sum); }} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variables • Definition: A variable is an item of data named by an identifier • Variable declaration:type name; (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variables example • public class MaxVariablesDemo { public static void main(String args[]) { //integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; //real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; //other primitive types char aChar = 'S'; boolean aBoolean = true; • //Display them all. System.out.println("The largest byte value is “+ largestByte + "."); System.out.println("The largest short value is “+ largestShort + "."); (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variables example cont. • System.out.println("The largest integer value is “ + largestInteger + "."); System.out.println("The largest long value is “ + largestLong + "."); System.out.println("The largest float value is “ + largestFloat + "."); System.out.println("The largest double value is “ + largestDouble + "."); if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is uppercase."); } else { System.out.println("The character " + aChar + " is lowercase."); } System.out.println("The value of aBoolean is “ + aBoolean + "."); }} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variables example results • The largest byte value is 127. • The largest short value is 32767. • The largest integer value is 2147483647. • The largest long value is 9223372036854775807. • The largest float value is 3.4028235E38. • The largest double value is 1.7976931348623157E308. • The character S is uppercase. • The value of aBoolean is true. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
More about variables • Data Types • Variable Names • Scope • Variable Initialization • Final Variables (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Data Types: primitive (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Data Types: primitive cnt’ (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Data types: other • Arrays, classes, and interfaces are reference types (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variable Names • It must be a legal identifier • It must not be a keyword , a boolean literal (true or false), or the reserved word null • It must be unique within its scope (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variable Names • By Convention: • Variable names begin with a lowercase letter and class names begin with an uppercase letter. • If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter (for example, isVisible). • The underscore character ( _ ) is acceptable anywhere in a name, but by convention it is used only to separate words in constants (because constants are all caps by convention and thus cannot be case-delimited). (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Scope (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variable Initialization • //integers • byte largestByte = Byte.MAX_VALUE; • short largestShort = Short.MAX_VALUE; • int largestInteger = Integer.MAX_VALUE; • long largestLong = Long.MAX_VALUE; • //real numbers • float largestFloat = Float.MAX_VALUE; • double largestDouble = Double.MAX_VALUE; • //other primitive types • char aChar = 'S'; • boolean aBoolean = true; (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Final Variables • You can declare a variable in any scope to be final • The value of a final variable cannot change after it has been initialized final int aFinalVar = 0; (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Operators • An operator performs a function on one, two, or three operands (unary operators, binary operators, a ternary operator) operator op //prefix notation op operator //postfix notation op1 operator op2 //infix notation op1 ? op2 : op3 //infix notation (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Relational and Conditional Operators (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Conditional Operators (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Shortcut Assignment Operators (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Other Operators (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Expressions, Statements, and Blocks • Variables and operators, which were discussed in the previous two sections, are the basic building blocks of programs • You combine literals, variables, and operators to form expressions — segments of code that perform computations and return values (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Expressions • Definition: An expression is a series of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Operator Precedence (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Assignment expressions Any use of ++ or -- Method invocations Object creation expressions aValue = 8933.234; aValue++; System.out.println(aValue); Integer integerObject = new Integer(4); Statements (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Blocks • if (Character.isUpperCase(aChar)) {System.out.println("The character " + aChar+ " is uppercase.");} else {System.out.println("The character " + aChar+ " is lowercase.");} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Control Flow Statements (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
while(expression) { statement} while (c != 'g') {copyToMe.append(c);c = copyFromMe.charAt(++i);} do { statement(s)} while (expression); do {copyToMe.append(c);c = copyFromMe.charAt(++i);} while (c != 'g'); The while and do-while Statements (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
The for Statement for (initialization; termination; increment) { statement(s)} for ( ; ; ) { //infinite loop ...} for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " ");} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Iterating over Collections and Arrays with Enhanced for public class ForEachDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; for (int element : arrayOfInts) { System.out.print(element + " "); } System.out.println(); }} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Iterating over Collections and Arrays with Enhanced forcnt’ • //This is ugly. Avoid it by using enhanced for!void cancelAll(Collection<TimerTask> c) { for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); ) i.next().cancel();} • //This is much prettier.void cancelAll(Collection<TimerTask> c) { for (TimerTask t : c) t.cancel();} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
The if statement if (boolean expression) { statement(s)} else { statement(s)} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
The switch statement switch (integer expression) { case integer expression: statement(s) break; ... default: statement(s) break;} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
The try, catch, and finally statements to handle exceptions try { statement(s)} catch (exceptiontype name) { statement(s)} catch (exceptiontype name) { statement(s)} finally { statement(s)} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Branching Statements • statementName: someJavaStatement; • break; • break label; • continue; • continue label; • return; • return value; (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006