Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.
-
Upload
julian-welch -
Category
Documents
-
view
225 -
download
0
Transcript of Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.
![Page 1: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/1.jpg)
Chapter 2: Java Fundamentals
Spring 2006-2007Lory Al Moakar
![Page 2: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/2.jpg)
Outline 2.1 The Parts of a Java Program 2.2 The print and println Methods, and the Java Standard Class
Library 2.3 Variables and Literals 2.4 Primitive Data Types 2.5 Arithmetic Operators 2.6 Combined Assignment Operators 2.7 Conversion Between Primitive Types 2.8 Creating Named Constants with final 2.9 The String Class 2.10 Scope 2.11 Comments 2.12 Programming Style 2.13 Reading Keyboard Input 2.14 Dialog Boxes 2.15 Common Errors to Avoid
![Page 3: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/3.jpg)
2.1 The Parts of a Java Program
// This is a simple Java program.
public class Simple{ public static void main(String[] args) { System.out.println("Programming is great
fun!"); }}
Comment for the reader
Name of the class (program)
Main part of the program
![Page 4: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/4.jpg)
Comments
Are ignored by the compiler Are used to clarify the purpose of the
program or line of code
![Page 5: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/5.jpg)
Class Every program in Java has to be a class Always a class has a name( Simple in this
example ) Your file has to be named this name
followed by .java ( Simple.java is the name of this file )
Important: Java is case sensitive so pay attention to the letters and the case they are in. (ex: simple is different from Simple)
![Page 6: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/6.jpg)
Main part of the program Usually it starts with a method header:public static void main ( String args[] ) After the { you write the commands
that you want the program to execute Every { you open it should be closed }
at some point in your program Every line in the main part of the
program has to end in ;
![Page 7: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/7.jpg)
Main part of the program
System.out.println("Programming is great fun!");
This statement prints Programming is great fun! It prints whatever you put between “
and “
![Page 8: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/8.jpg)
Special characters
// Double slash Beginning of a comment
( ) Opening and closing parentheses
Used in method headers
{ } Opening and closing braces
Encloses a group of statements
“ “ Quotation marks
Encloses a string of characters
; Semicolon Ends a complete statement
![Page 9: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/9.jpg)
print and println System.out.print or System.out.println System.out.print displays the text
between “ “ exactly as it is System.out.println same as
System.out.print except that after it displays the text on the screen it move the cursor to the beginning of the next line
![Page 10: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/10.jpg)
Exercise in class download the tool DrJava Write the following in DrJavapublic class printTest {
public static void main (String args[]){
System.out.print(“Hello” );System.out.print(“There”);
}}
![Page 11: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/11.jpg)
Exercise in class Save it as PrintTest.java Compile and run You should get
Hello There Now change print to println Save, compile and run You should get
Hello There
![Page 12: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/12.jpg)
Escape Sequences Allow you to control the way output is
displayed Are characters that have a special meaning
when put within a String Literal Example:
System.out.print( “Hi \n CS7” );Displays:
HiCS7
![Page 13: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/13.jpg)
Escape Sequences An escape
sequence starts with a backslash and is followed by a control character
Here is a list:
Moves the cursor to
\n a new line
\t the next tab stop
\b one character back
\r To the beginning of the line
\\ Prints a backslash
\’ Prints a single quote
\” Prints a double quotation mark
![Page 14: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/14.jpg)
Example// Another well adjusted printing program
public class Tabs{ public static void main(String[] args) { System.out.print("These are our top sellers:\n"); System.out.print("\tComputer games\n\tCoffee\n "); System.out.println("\tAspirin"); }}
Output:These are our top sellers:
Computer gamesCoffeeAspirin
![Page 15: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/15.jpg)
2.3 Variables and Literals
Variables allow you to store and work with data in the computer memory
Each variable has a type and a name
![Page 16: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/16.jpg)
Variables
A variable has to be declared, and initialized
Variable Declaration:type identifier;
Variable Initialization:identifier = literal;
![Page 17: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/17.jpg)
Literals
String Literal is enclosed between quotation marks ex: “This is me”
Integer Literal is not enclosed in quotation marks and has only numbers ex: 1,53, 965747
![Page 18: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/18.jpg)
Identifiers
An identifier is a programmer-defined name that represents some element of the program. Ex: variable names & class names
![Page 19: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/19.jpg)
Rules to follow when naming identifiers The first character must be one of the
letters a-z, A-Z, an underscore, or a dollar sign
After the first character, you may use one of the letters a-z, A-Z, an underscore, a dollar sign or a digit 0-9
Uppercase and lowercase characters are different
Identifiers cannot include spaces, points, commas or punctuation
An identifier cannot be one of the keywords
![Page 20: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/20.jpg)
Keywordsabstract default if private this
boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch continue
goto package synchronized
![Page 21: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/21.jpg)
Good practices when naming identifiers
Good descriptive names Use uppercase letters when having
two or more words to form an identifier
Do not use single lettered identifiers
![Page 22: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/22.jpg)
Printing a variable
2 ways: On its own:
System.out.println( identifier); Combined with text:System.out.println( “The result is: “+
identifier );
![Page 23: Chapter 2: Java Fundamentals Spring 2006-2007 Lory Al Moakar.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ef65503460f94c0a084/html5/thumbnails/23.jpg)
Example// This program has a variable.
public class Variable{ public static void main(String[] args) { int value;
value = 5; System.out.print("The value is "); System.out.println(value); }}