Download presentation
Presentation is loading. Please wait.
Published byEvan Floyd Modified over 9 years ago
1
Chapter 1 Introduction to JAVA
2
Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing programming language in the world. Java is a modern object-oriented programming language. Java is a modern object-oriented programming language. Java is secure, robust, and portable. Java is secure, robust, and portable. Enables the construction of virus-free, tamper free systems (secure) Enables the construction of virus-free, tamper free systems (secure) Supports the development of programs that do not overwrite memory (robust) 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) 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. Java supports the use of advanced programming concepts such as threads. Thread – a process that can run concurrently with other processes. 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. 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. 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. 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. 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. 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. There are two types of user interfaces a programmer can develop. Terminal I/O interface – A text based DOS console window. 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. 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. We will focus on Terminal I/O in this class.
6
JAVA Class Libraries Java Programs Java Programs Classes contain methods, which perform tasks Classes contain methods, which perform tasks Consist of pieces called classes Consist of pieces called classes Class libraries Class libraries Rich collection of predefined classes, which you can use Rich collection of predefined classes, which you can use Two parts of learning Java Two parts of learning Java Learning the language itself, so you can create your own classes Learning the language itself, so you can create your own classes Learning how to use the existing classes in the libraries Learning how to use the existing classes in the libraries
7
Basics of a Typical Java Environment Java Systems Java Systems Consist of environment, language, Class libraries Consist of environment, language, Class libraries Java programs have five phases Java programs have five phases - Edit Use an editor to type Java program Use an editor to type Java program Notepad, Jbuilder, Visual J++, Jcreator, BlueJ, Eclipse Notepad, Jbuilder, Visual J++, Jcreator, BlueJ, Eclipse.java extension.java extension - Compile Translates program into bytecodes, understood by Java interpreter Translates program into bytecodes, understood by Java interpreter Creates.class file, containing bytecodes (MyProgram.class) Creates.class file, containing bytecodes (MyProgram.class)
8
Basics of a Typical Java Environment Java programs have five phases (continued) Java programs have five phases (continued) - Loading Class loader transfers.class file into memory Class loader transfers.class file into memory Applications - run on user's machine Applications - run on user's machine Applets - loaded into Web browser, temporary Applets - loaded into Web browser, temporary Classes loaded and executed by interpreter with java command Classes loaded and executed by interpreter with java command HTML documents can refer to Java Applets, which are loaded into web browsers. HTML documents can refer to Java Applets, which are loaded into web browsers. appletviewer is a minimal browser, can only interpret applets appletviewer is a minimal browser, can only interpret applets
9
Basics of a Typical Java Environment Java programs have five phases (continued) Java programs have five phases (continued) - Verify Bytecode verifier makes sure bytecodes are valid and do not violate security 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) 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 Computer (controlled by CPU) interprets program one bytecode at a time Performs actions specified in program Performs actions specified in program Program may not work on first try Program may not work on first try Make changes in edit phase and repeat 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 // indicates the remainder of the line is a comment Comments are ignored by the compiler Comments are ignored by the compiler Use comments to document and describe code Use comments to document and describe code Commenting code is GOOD PROGRAMMING STYLE Commenting code is GOOD PROGRAMMING STYLE Can also use multiple line comments: /*... */ Can also use multiple line comments: /*... */ ex. /* This is a multiple line comment. It can line comment. It can be split over many lines */ be split over many lines */
12
public class Welcome { -Begins a class definition for class Welcome Every Java program has at least one user- defined class Every Java program has at least one user- defined class class keyword immediately followed by class name class keyword immediately followed by class name Keyword: words reserved for use by Java Keyword: words reserved for use by Java Naming classes: capitalize every word Naming classes: capitalize every word SampleClassName SampleClassName Beginning a Program
13
public class Welcome { Identifier Names Identifier Names Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit Does not begin with a digit Contains no spaces Contains no spaces Examples: Welcome1, $value, _value, button7 Examples: Welcome1, $value, _value, button7 7button is invalid 7button is invalid Case sensitive (capitalization matters) Case sensitive (capitalization matters) a1 and A1 are different a1 and A1 are different
14
Beginning a Program public class Welcome { Saving files Saving files File name is class name with.java extension File name is class name with.java extension Welcome.java Welcome.java Braces Braces Left brace starts every class Left brace starts every class Right brace ends every class Right brace ends every class public static void main(String [] args) { Part of every Java application Part of every Java application Applications begin executing at main Applications begin executing at main Parenthesis indicate main is a method Parenthesis indicate main is a method Java applications contain one or more methods 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 Exactly one method must be called main Methods can perform tasks and return information Methods can perform tasks and return information void means main returns no information void means main returns no information For now, mimic main's first line For now, mimic main's first line Left brace begins body of method definition 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 Instructs computer to perform an action Prints string of characters between double quotes Prints string of characters between double quotes String - series characters inside double quotes String - series characters inside double quotes White spaces in strings are not ignored by compiler White spaces in strings are not ignored by compiler System.out - standard output object System.out - standard output object Allows java to print to command window (i.e., MS-DOS prompt) Allows java to print to command window (i.e., MS-DOS prompt) Method System.out.println displays a line of text Method System.out.println displays a line of text Argument inside parenthesis Argument inside parenthesis Entire line known as a statement Entire line known as a statement All statements must end with a semicolon ; All statements must end with a semicolon ;
17
A Simple Program: Printing a Line of Text } Ends method definition Ends method definition } Ends class definition Ends class definition Some programmers add comments to keep track of ending braces Some programmers add comments to keep track of ending braces The last two lines could be rewritten as: The last two lines could be rewritten as: } //end of method main() } //end of class welcome Remember that the compiler ignores comments Remember that the compiler ignores comments
18
A Simple Program: Printing a Line of Text Compiling a program Compiling a program Click compile button. Located on toolbar by an upside down arrow Click compile button. Located on toolbar by an upside down arrow If there are no errors, file Welcome.class is created If there are no errors, file Welcome.class is created Contains Java bytecodes that represent application Contains Java bytecodes that represent application Bytecodes passed to Java interpreter Bytecodes passed to Java interpreter Executing a program Executing a program Click Execute button. Located on toolbar by an arrow pointing to the right. F5 will also Execute 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 Launches interpreter to load.class file for class Welcome.class extension omitted from command.class extension omitted from command Interpreter calls method main Interpreter calls method main
19
A Simple Program: Printing a Line of Text Other methods Other methods System.out.println System.out.println Positions cursor on new line after displaying argument Positions cursor on new line after displaying argument System.out.print System.out.print Keeps cursor on same line after displaying argument Keeps cursor on same line after displaying argument
20
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! System.out.print keeps the cursor on the same line, so System.out.println continues on the same line.
21
A Simple Program: Printing a Line of Text Escape characters Escape characters Backslash ( \ ) Backslash ( \ ) Indicates that special characters are to be output Indicates that special characters are to be output Backslash combined with a character makes an escape sequence Backslash combined with a character makes an escape sequence \n - newline \n - newline \t - tab \t - tab Usage Usage Can use in System.out.println or System.out.print to create new lines Can use in System.out.println or System.out.print to create new lines System.out.println( "Welcome\nto\nJava\nProgramming!" ); System.out.println( "Welcome\nto\nJava\nProgramming!" );
22
A Simple Program: Printing a Line of Text Notice how a new line is output for each \n escape sequence. // 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!
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: 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 Layout Readability Readability Indentation Indentation Name usage Name usage
25
Example Program with Readability import TerminalIO.KeyboardReader; public class Convert { public static void main(String [] args) { KeyboardReader reader = new KeyboardReader(); KeyboardReader reader = new KeyboardReader(); double fahrenheit; double fahrenheit; double celsius; double celsius; System.out.print("Enter degrees Fahrenheit: "); System.out.print("Enter degrees Fahrenheit: "); fahrenheit = reader.readDouble(); fahrenheit = reader.readDouble(); celsius = (fahrenheit - 32.0) * 5.0 / 9.0; celsius = (fahrenheit - 32.0) * 5.0 / 9.0; System.out.print("The equivalent in Celsius is "); System.out.print("The equivalent in Celsius is "); System.out.println(celsius); System.out.println(celsius); reader.pause(); reader.pause(); }}
26
Example Program without Readability import TerminalIO.KeyboardReader; public class Convert {public static void main(String [] args) { KeyboardReader reader = new KeyboardReader();double KeyboardReader reader = new KeyboardReader();double fahrenheit; fahrenheit; double celsius;System.out.print("Enter degrees Fahrenheit: "); double celsius;System.out.print("Enter degrees Fahrenheit: "); fahrenheit fahrenheit=reader.readDouble(); celsius = (fahrenheit celsius = (fahrenheit - 32.0) * 5.0 / 9.0; - 32.0) * 5.0 / 9.0; System.out.print System.out.print ("The equivalent in Celsius is "); System.out System.out.println(celsius); reader.pause(); reader.pause(); }}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.