1 / 38

Introduction to java

Introduction to java. Learning About Programming . Program Set of written instructions that tells computer what to do Machine language Most basic circuitry-level language Low-level programming language High-level programming language

tyanne
Download Presentation

Introduction to 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. Introduction to java

  2. Learning About Programming • Program • Set of written instructions that tells computer what to do • Machine language • Most basic circuitry-level language • Low-level programming language • High-level programming language • Allows you to use vocabulary of reasonable terms(read, write, add, etc) • Syntax • Rules of the language

  3. Program statements (aka commands) • Similar to English sentences • Carry out tasks of program • Compiler or interpreter • Translates language statements into machine code • Compiler translate the entire program before carrying out the statement (called executing) • Interpreter translate one program statement at a time, executing it as soon as it is translated • Debugging • Freeing program of all errors

  4. Syntax error • Misuse of language • Misspelled programming language word • Logic errors • Also called semantic errors • Incorrect order or procedure

  5. Procedural programming • Sets of operations executed in sequence • Variables • Named computer memory locations that hold values • Procedures • Individual operations grouped into logical units • Object-oriented programs • Create classes • Create objects from classes • Create applications that use those objects

  6. Object-oriented programming was used most frequently for two major types of applications • Computer simulations • Graphical user interfaces (GUIs) • Not all object-orientated programs written to use GUI • Object-oriented programming differs from traditional procedural programming • Basic concepts • Polymorphism • Inheritance • Encapsulation

  7. Understanding Classes, Objects, and Encapsulation • Class • Describes a group or collection of objects with common properties • Class definition • describes what attributes its objects will have & what those objects will be able to do • Instance • Class Attributes • Characteristics that define an object • They are the properties of the object • The values in its properties differentiate objects of the same class • The values of the properties is called the object’s state

  8. Class Method • Self-contained block of program code that carries out some action

  9. Objects • Specific, concrete instance of a class • When you create an object instance, you instantiate it • In OOP grammer: • object = noun • Attribute = adjective • Methods = verbs

  10. Example • Class - Automoblie • Class definition - describes what Automobile objects are like • Automobile attributes – make, model, year, color • Each automobile object possesses the same attributes, but not the same values for those attributes • Automobile methods – moveForward, moveBackward. carFill, carWash

  11. Encapsulation • The hiding of data and methods within an object • Keeps data and methods safe from inadvertent changes • Sometimes referred to as “black box” • a programmer can access & use the methods & data in the black box but can’t change them

  12. Understanding Inheritance and Polymorphism • Inheritance • Important feature of object-oriented programs • Ability to create classes that share the attributes and methods of existing classes but with more specific features • Helps you understand real-world objects • Polymorphism • Means “many forms” • Allows the same word or symbol to be interpreted correctly in different situations based on the context

  13. Features of the Java Programming Language • Java • Developed by Sun Microsystems • Object-oriented language • General-purpose • Advantages • Security features • Architecturally neutral

  14. Bytecode • Statements saved in file • Java compiler converts source code into binary program consisting of this • Java interpreter • Checks bytecode and communicates with operating system • Executes bytecode instructions line by line within Java virtual machine

  15. Java Program Types • Applets • Programs embedded in Web page • Java applications • Called Java stand-alone programs • Console applications • Support character output • Easiest to create • Windowed applications • Creates a GUI w/elements such as: • Menus • Toolbars • Dialog boxes Java Programming, Sixth Edition

  16. Understanding the Statementthat Produces the Output • Literal string • Will appear in output exactly as entered • Written between double quotation marks • Cannot be broken and put on multiple lines • Arguments • Pieces of information passed to a method • Method • Usually requires the information to perform its task or carry out its purpose • Systemclass • Refers to the standard input/output device for a system

  17. Print() vsprintln() • print() – insertion point remains on the same line as the output • println() – once the message is displayed, the insertion point appears on the following line

  18. Understanding a Class • Everything used within a Java program must be part of a class • Define Java class using any name or identifier • Requirements for identifiers • Must begin with: • A letter of the English alphabet • Anon-English letter (such as α or π) • An underscore • A dollar sign • Cannot begin with a digit

  19. Understanding a Class (cont'd.) • Can only contain: • Letters • Digits • Underscores • Dollar signs • Cannot be a Java reserved keyword • Cannot be true, false, or null • Access specifier • Defines how a class can be accessed & the other classes that have the right to use a class

  20. Understanding a Class (cont'd.)

  21. Understanding a Class (cont'd.)

  22. Program Style • For every opening curly brace ( { ) in a Java program, there must be a corresponding closing curly brace ( } ) • Placement of the opening and closing curly braces is not important to the compiler • Whitespace: • Optional • Used to organize your program code and make it easier to read • Cannot use it within any identifier or keyword

  23. Understanding the main() Method Static means this method works without instantiating an object of the cass.

  24. Understanding the main() Method • static • Reserved keyword • Means a method is accessible & usable even though no objects of the class exist • void • Use in main() method header • Does not indicate main()method is empty • Indicates main() method does not return any value when called • Doesn’t mean main()doesn’t produce output

  25. Adding Comments to a Java Class • Program comments • Nonexecuting statements added to a program for documentation • Used to leave notes for yourself or others • Should include the author, the date, the class name or function, and a brief description of the purpose of each method you create • Comment out a statement • Turn statement into a comment • Compiler does not translate, and the JVM does not execute its command

  26. Adding Comments to a Java Class (cont'd.) • Types of Java comments • Line comments • Start with two forward slashes (//) • Continues to the end of the current line • Does not require an ending symbol • Can appear on a line by itself or at the end (and to the right) of a line • Block comments • Start with forward slash and asterisk (/*) • End with asterisk and forward slash (*/) • Can appear on a line by itself, before code, or after code

  27. Adding Comments to a Java Class (cont'd.) • Types of Java comments (cont'd.) • Javadoc comments • Special case of block comments • Begin with slash and two asterisks (/**) • End with asterisk and forward slash (*/) • Used to generate documentation

  28. Adding Comments to a Java Class (cont'd.)

  29. Saving, Compiling, Running, and Modifying a Java Application • Saving a Java class • Save the class in a file with exactly the same name and .java extension • For public classes • Class name and filename must match exactly • Compiling a Java class • Compile source code into bytecode • Translate bytecode into executable statements

  30. Correcting Errors andFinding Help • First line of error message displays: • Name of file where error found • Line number • Nature of error • Next lines identify: • Symbol • Location • Compile-time error • Compiler detects violation of language rules • Refuses to translate class to machine code

  31. Correcting Errors andFinding Help (cont'd.) • Parsing • Process compiler uses to divide source code into meaningful portions • Logic error • Syntax correct but produces incorrect results when executed • Usually more difficult to find and resolve • Java API • Also called the Java class library • Prewritten Java classes

  32. Don’t Do It • File’s name must match name of class • Don’t confuse these terms: • Parentheses, braces, brackets, curly braces, square brackets, and angle brackets • Don’t forget to end a block comment • Don’t forget that Java is case sensitive • End every statement with semicolon • Do not end class or method headers with semicolon • Recompile when making changes

  33. Summary • Computer program • Set of instructions that tells a computer what to do • Object-oriented programs • Classes • Objects • Applications • Java virtual machine (JVM) • Standardized hypothetical computer • Everything in a Java program must be part of a class

  34. Summary (cont'd.) • Access specifier • Word that defines circumstances under which class can be accessed • All Java applications must have method named main() • Program comments • Nonexecuting statements • Add to file for documentation • javac • Compile command

  35. Summary (cont'd.) • java • Execute command • JOptionPane • GUI • Provides methods for creating dialogs

More Related