1 / 35

Программирование на Java: Основы языка

Программирование на Java: Основы языка. Вячеслав Гребенюк ЦТДО, каф. ИИ, ХНУРЭ. Content. Language Basics.

anacoleman
Download Presentation

Программирование на Java: Основы языка

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Программирование на Java: Основы языка Вячеслав Гребенюк ЦТДО, каф. ИИ, ХНУРЭ

  2. Content (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  3. 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

  4. Variables • Definition: A variable is an item of data named by an identifier • Variable declaration:type name; (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  5. 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

  6. 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

  7. 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

  8. More about variables • Data Types • Variable Names • Scope • Variable Initialization • Final Variables (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  9. Data Types: primitive (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  10. Data Types: primitive cnt’ (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  11. Data types: other • Arrays, classes, and interfaces are reference types (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  12. 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

  13. 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

  14. Scope (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  15. 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

  16. 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

  17. 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

  18. Relational and Conditional Operators (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  19. Conditional Operators (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  20. Shortcut Assignment Operators (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  21. Other Operators (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  22. 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

  23. 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

  24. Operator Precedence (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  25. 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

  26. Blocks • if (Character.isUpperCase(aChar)) {System.out.println("The character " + aChar+ " is uppercase.");} else {System.out.println("The character " + aChar+ " is lowercase.");} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  27. Control Flow Statements (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  28. 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

  29. 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

  30. 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

  31. 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

  32. The if statement if (boolean expression) { statement(s)} else { statement(s)} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  33. The switch statement switch (integer expression) { case integer expression: statement(s) break; ... default: statement(s) break;} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

  34. 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

  35. Branching Statements • statementName: someJavaStatement; • break; • break label; • continue; • continue label; • return; • return value; (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006

More Related