1 / 19

Java Fundamentals

A Review. Java Fundamentals. Common Programming Constructs. Software Development Cycle: design, implement, test, debug, document Large Problems are broken down into smaller modules to be solved using flowcharting or pseudo-code, then coded, & re-assembled to solve the complete problem.

moe
Download Presentation

Java Fundamentals

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. A Review Java Fundamentals

  2. Common Programming Constructs • Software Development Cycle: design, implement, test, debug, document • Large Problems are broken down into smaller modules to be solved using flowcharting or pseudo-code, then coded, & re-assembled to solve the complete problem. • High-level languages require a compiler to translate source code into machine code ( or byte code in the case of Java). • Byte code is interpreted by the Java Virtual Machine and run. a review of lessons learned so far… ( 2 steps forward - 1 step back)

  3. Program command execution is linear, thus, programming statements must be written in order (linear) as well. • Programming languages contain keywords/reserved words that can only be used as their intended purpose as determined by the language. • Languages utilize a specific syntax & punctuation. • Primitive data types are “built in” to the language, and allow for data storage & retrieval. • Storage space must be allocated for data, before it can be used to store data. • Programming languages allow for user defined “identifiers” of storage space (variables & constants), methods, and classes (in object-oriented languages like Java). a review of lessons learned so far… ( 2 steps forward - 1 step back)

  4. The API extends/expands the language with a library of classes/methods for common programming tasks. • Common Mathematical Operators are: • +, -, *, /, %, ++ (increment), -- (decrement) & - (unary minus) • = (The Assignment Operater, assigns the rval to the thelval ) • Mathematical Expressions are processed with operator precedence & associativity. • Programmers use comments to note explanation of coding methods & identifier purposes for future reference. • Good Programming Practice is to write “readable by humans” code (indent, ws, comments). a review of lessons learned so far… ( 2 steps forward - 1 step back)

  5. Types of errors You mean there are more than one? Compile-Time: syntax errors caught by the compiler Run-Time: errors caused by exceptions resulting in the program terminating abnormally (ex. attempting to divide by zero (0) - undefined command - program crashes) Logic: program compiles & runs, but, produces inaccurate results (always test program with data to verify accurate results)

  6. Common Coding Errors Compiler Gotcha: Mismatched braces, quotation marks, or parenthesis Possible Error Message: Errors.java:13: reached end of file while parsing public class Errors{ Enter them in pairs & label closing public static void main(String args[ ]) { //body of method } //closing main method } //closing class header

  7. Common Coding Errors Compiler Gotcha: misspelling a keyword Error Message: Errors.java:6: cannot find symbol Type Accurately. Remember keywords are l.c.

  8. Common Coding Errors Compiler Gotcha: Using a keyword as a variable name Errors.java:6: not a statementErrors.java:6: ';' expectedErrors.java:6: not a statement Sampling of most commonly accidently used keywords (and replacement choice): class (use group) final ( use endResult) return (use returnValue) false ( use negative) true (use affirmative) new (use newValue) null ( use emptyValue)

  9. Common Coding Errors Compiler Gotcha: misspelling variable names & switching use of l.c. & u.c. Errors.java:7: cannot find symbol Be consistent. Follow convention when naming variables. List all variable declarations at the beginning of a method - FIRST! (then you can refer to them as a list when you need to use them, and… you will not improperly use or accidently reuse a variable name)

  10. Common Coding Errors Compiler Gotcha: putting blank space in a multi-word variable name Error Messages: Errors.java:6: ';' expectedErrors.java:6: not a statement Just Don’t Do It! To reinforce this habit, never use blank spaces when you name any computer file.

  11. Common Coding Errors Compiler Gotcha: forgetting to end the statement? Error Message: Errors.java:6: ';' expected Use it! ; (semi-colon)

  12. Common Coding Errors Compiler Gotcha: mis-matched data types & literals Error Message: Errors.java:6: possible loss of precision Remember float literals are treated as double values. float total = 5.0; // wrong float total = 5.0F; // correct

  13. Common Coding Errors Compiler Gotcha: using commas (,) or currency ($) symbols in numeric data types Error Message: Errors.java:6: ';' expected DON’T USE $ OR , IN DATA! float totalExpense = $5,000.00F; Correct: float totalExpense = 5000.00F;

  14. Common Coding Errors unintentionally performing integer division (loss of remainder) Error Message: NONE! Watch out for this one! public class Errors { public static void main(String args[ ]) {int x =5;int y =2;int z; z= x/y;System.out.println(z); //output: 2 } //closing main method }

  15. Common Coding Errors forgetting the rules for operator precedence Error Message: NONE! Watch out for this one! x = 2+5*4; // x = 22 x = (2+5)*4; // x = 28 y = 7-2+3; // y = 8; y = 7 – (2+3); // y = 2; When operators within an expression have the same precedence, sharing an operand, they work according to their associativity. When in doubt, use (parenthesis)!

  16. Common Coding Errors Compiler Gotcha: placing a space between combined operators Error Message: Errors.java:9: illegal start of expression z + = y; //Don’t z += y; //Do

  17. Common Coding Errors Compiler Gotcha: incompatible data types public class Errors { public static void main(String args[ ]) { float y =2.75F;intz =1; z+= y; //Don’t (z:3) } //closing main method } public class Errors { public static void main(String args[ ]) { float y =2.75F;float z =1.0F; //Do z+= y; //(z:3.75) } //closing main method }

  18. Common Coding Errors Compiler Gotcha: forgetting to terminate a multi-line comment Error Message: Errors.java:3: unclosed comment /* start multi-line comment blah, blah, blah… */

  19. Common Coding Errors You must import the Scanner Object! Compiler Gotcha: forgetting to use the import statement Errors.java:11: cannot find symbol import java.util.Scanner;public class Errors { public static void main(String args[ ]) { Scanner keyboard = new Scanner(System.in); } //closing main method } //closing class definition For this statemnt to work…

More Related