260 likes | 387 Views
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)
E N D
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) • 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)
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.
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.
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.
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
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)
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
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
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!
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 */
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
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
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
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
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 ;
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
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
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
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!
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!" );
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.
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.
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
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(); } }
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(); } }