1 / 26

Chapter 1

Chapter 1. Introduction to JAVA. Why Learn JAVA?. Java is one of the fastest growing programming language in the world. Java is a modern object-oriented programming language. Java is secure, robust, and portable. Enables the construction of virus-free, tamper free systems (secure)

tallis
Download Presentation

Chapter 1

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. Chapter 1 Introduction to JAVA

  2. Why Learn JAVA? • Java is one of the fastest growing programming language in the world. • Java is a modern object-oriented programming language. • Java is secure, robust, and portable. • Enables the construction of virus-free, tamper free systems (secure) • Supports the development of programs that do not overwrite memory (robust) • Yields programs that can be run on different types of computers without change (portable)

  3. Why Learn JAVA? • Java supports the use of advanced programming concepts such as threads. • Thread – a process that can run concurrently with other processes. • Java bears a superficial resemblance to C++, which is currently the world’s most popular industrial strength programming language.

  4. The JAVA Virtual Machine • Compilers usually translate a higher-level language into the machine language of a particular type of computer. • However, the Java compiler translates Java not into machine language, but into a pseudo-machine language called Java byte code. • Byte code - is the machine language for an imaginary Java computer. To run Java byte code on a particular computer, you must install a Java virtual machine (JVM) on that computer. • A JVM is a program that behaves like a computer. Such a program is interpreter.

  5. User Interface Styles • There are two types of user interfaces a programmer can develop. • Terminal I/O interface – A text based DOS console window. • Graphical User Interface (GUI) – A windowed based environment with text and graphics displayed in the window. • We will focus on Terminal I/O in this class.

  6. JAVA Class Libraries • Java Programs • Classes contain methods, which perform tasks • Consist of pieces called classes • Class libraries • Rich collection of predefined classes, which you can use • Two parts of learning Java • Learning the language itself, so you can create your own classes • Learning how to use the existing classes in the libraries

  7. Basics of a Typical Java Environment • Java Systems • Consist of environment, language, Class libraries • Java programs have five phases - Edit • Use an editor to type Java program • Notepad, Jbuilder, Visual J++, Jcreator, BlueJ, Eclipse • .java extension - Compile • Translates program into bytecodes, understood by Java interpreter • Creates .class file, containing bytecodes (MyProgram.class)

  8. Basics of a Typical Java Environment • Java programs have five phases (continued) - Loading • Class loader transfers .class file into memory • Applications - run on user's machine • Applets - loaded into Web browser, temporary • Classes loaded and executed by interpreter with java command • HTML documents can refer to Java Applets, which are loaded into web browsers. • appletviewer is a minimal browser, can only interpret applets

  9. Basics of a Typical Java Environment • Java programs have five phases (continued) - Verify • Bytecode verifier makes sure bytecodes are valid and do not violate security • Java must be secure - Java programs transferred over networks, possible to damage files (viruses) - Execute • Computer (controlled by CPU) interprets program one bytecode at a time • Performs actions specified in program • Program may not work on first try • Make changes in edit phase and repeat

  10. Example Program Java program //Example Program Welcome.java //A first program in Java public class Welcome { public static void main(String [ ] args { System.out.println(“Welcome to Java Programming!”); } } Program Output Welcome to Java Programming!

  11. Commenting Code //Example Program Welcome.java • // indicates the remainder of the line is a comment • Comments are ignored by the compiler • Use comments to document and describe code • Commenting code is GOOD PROGRAMMING STYLE • Can also use multiple line comments: /* ... */ ex. /* This is a multiple line comment. It can be split over many lines */

  12. Beginning a Program public class Welcome { -Begins a class definition for class Welcome • Every Java program has at least one user-defined class • class keyword immediately followed by class name • Keyword: words reserved for use by Java • Naming classes: capitalize every word • SampleClassName

  13. Beginning a Program public class Welcome { • Identifier Names • Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) • Does not begin with a digit • Contains no spaces • Examples: Welcome1, $value, _value, button7 • 7button is invalid • Case sensitive (capitalization matters) • a1 and A1 are different

  14. Beginning a Program public class Welcome { • Saving files • File name is class name with .java extension • Welcome.java • Braces • Left brace starts every class • Right brace ends every class public static void main(String [] args) { • Part of every Java application • Applications begin executing at main • Parenthesis indicate main is a method • Java applications contain one or more methods

  15. A Simple Program: Printing a Line of Text public static void main(String [] args) { • Exactly one method must be called main • Methods can perform tasks and return information • void means main returns no information • For now, mimic main's first line • Left brace begins body of method definition

  16. A Simple Program: Printing a Line of Text System.out.println(“Welcome to Java Programming!”); • Instructs computer to perform an action • Prints string of characters between double quotes • String - series characters inside double quotes • White spaces in strings are not ignored by compiler • System.out - standard output object • Allows java to print to command window (i.e., MS-DOS prompt) • Method System.out.println displays a line of text • Argument inside parenthesis • Entire line known as a statement • All statements must end with a semicolon ;

  17. A Simple Program: Printing a Line of Text } • Ends method definition } • Ends class definition • Some programmers add comments to keep track of ending braces • The last two lines could be rewritten as: } //end of method main() } //end of class welcome • Remember that the compiler ignores comments

  18. A Simple Program: Printing a Line of Text • Compiling a program • Click compile button. Located on toolbar by an upside down arrow • If there are no errors, file Welcome.class is created • Contains Java bytecodes that represent application • Bytecodes passed to Java interpreter • Executing a program • Click Execute button. Located on toolbar by an arrow pointing to the right. F5 will also Execute • Launches interpreter to load .class file for class Welcome • .class extension omitted from command • Interpreter calls method main

  19. A Simple Program: Printing a Line of Text • Other methods • System.out.println • Positions cursor on new line after displaying argument • System.out.print • Keeps cursor on same line after displaying argument

  20. System.out.print keeps the cursor on the same line, so System.out.println continues on the same line. A Simple Program: Printing a Line of Text // Example Welcome.java // Printing a line with multiple statementes public class Welcome { public static void main(String [ ] args) { System.out.print(“Welcome to “); System.out.println(“Java Programming!”); } } Program Output Welcome to Java Programming!

  21. A Simple Program: Printing a Line of Text • Escape characters • Backslash ( \ ) • Indicates that special characters are to be output • Backslash combined with a character makes an escape sequence • \n - newline • \t - tab • Usage • Can use in System.out.println or System.out.print to create new lines • System.out.println( "Welcome\nto\nJava\nProgramming!" );

  22. A Simple Program: Printing a Line of Text // Example Welcome.java // Printing multiple lines with a single statement public class Welcome { public static void main(String [ ] args) { System.out.println(“Welcome\nto\nJava\nProgramming!”); } } Program Output Welcome to Java Programming! Notice how a new line is output for each \n escape sequence.

  23. Template for Java File //Name //Project Name and # //Date public class Welcome { public static void main(String [ ] args) { Code that makes your program work } } This section contains statements that complete the program.

  24. Programming Style • One thing to remember when developing applications is that typically programs have a long life and are usually maintained by many people other than their original authors. Therefore, the developer should take into account the following items: • Layout • Readability • Indentation • Name usage

  25. Example Program with Readability import TerminalIO.KeyboardReader; public class Convert { public static void main(String [] args) { KeyboardReader reader = new KeyboardReader(); double fahrenheit; double celsius; System.out.print("Enter degrees Fahrenheit: "); fahrenheit = reader.readDouble(); celsius = (fahrenheit - 32.0) * 5.0 / 9.0; System.out.print("The equivalent in Celsius is "); System.out.println(celsius); reader.pause(); } }

  26. Example Program without Readability import TerminalIO.KeyboardReader; public class Convert {public static void main(String [] args) { KeyboardReader reader = new KeyboardReader();double fahrenheit; double celsius;System.out.print("Enter degrees Fahrenheit: "); fahrenheit = reader.readDouble(); celsius = (fahrenheit - 32.0) * 5.0 / 9.0; System. out.print ("The equivalent in Celsius is "); System.out .println(celsius); reader. pause(); } }

More Related