Download Lecture2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 2: Java
Fundamentals
Spring 2006-2007
Lory Al Moakar
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
2.1 The Parts of a Java Program
// This is a simple Java program.
Comment for
the reader
Name of the class
public class Simple
(program)
{
public static void main(String[] args)
{
System.out.println("Programming is great
fun!");
}
}
Main part of the
program
Comments
 Are ignored by the compiler
 Are used to clarify the purpose of the
program or line of code
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)
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 ;
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 “
Special characters
//
Double slash
Beginning of a comment
()
Opening and
closing
parentheses
Opening and
closing braces
Quotation
marks
Used in method headers
Semicolon
Ends a complete
statement
{}
““
;
Encloses a group of
statements
Encloses a string of
characters
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
Exercise in class
 download the tool DrJava
 Write the following in DrJava
public class printTest {
public static void main (String args[])
{
System.out.print(“Hello” );
System.out.print(“There”);
}
}
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
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:
Hi
CS7
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
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 games
Coffee
Aspirin
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
Variables
 A variable has to be declared, and
initialized
 Variable Declaration:
type identifier;
 Variable Initialization:
identifier = literal;
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
Identifiers
 An identifier is a programmer-defined
name that represents some element
of the program. Ex: variable names &
class names
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
Keywords
abstract
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
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
Printing a variable
 2 ways:
 On its own:
System.out.println( identifier);
 Combined with text:
System.out.println( “The result is: “+
identifier );
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);