Download ppt

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

Falcon (programming language) wikipedia , lookup

String literal wikipedia , lookup

Go (programming language) wikipedia , lookup

Structured programming wikipedia , lookup

Java syntax wikipedia , lookup

Class (computer programming) wikipedia , lookup

C++ wikipedia , lookup

Scala (programming language) wikipedia , lookup

Name mangling wikipedia , lookup

Object-oriented programming wikipedia , lookup

C Sharp syntax wikipedia , lookup

Java (programming language) wikipedia , lookup

Java performance wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
1/16: Intro to Java, Languages, and
Environments
• Types of programming languages
– machine languages
– assembly languages
– high-level languages
• Java environment
– 2.0 platform
Machine Languages
• natural language of the computer
• numeric language
• hard to read (for humans)
• EX:
+1300042774
+1400593419
+1200274027
Assembly Languages
• abbreviations replace some machine language
• programs called assemblers translate assembly
language into machine code
• EX:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
High-Level Languages
• look more like human languages
• programs called compilers convert high-level
code into machine language
• structured & object-oriented
– structured: Pascal, C
– structured & object-oriented: Java, C++
Structured Programming
• Disciplined approach
• Clearer to read & debug than previous
programming styles
• Pascal, C
• Ratherthan de cidnbyrs lf hw2use language, you
agree to abide by certain conventions to help in
collaboration and usability.
Object-Oriented Programming
• A OOP program has objects that can
perform actions on other objects.
• EX:
– newspaper
• editor, researcher, writer, layout designer
– restaurant
• cook, maitre d’, waiter, dishwasher, customer
OOP Terminology
• Objects
– program construction w/ associated data
& actions
– EX: a flat head screwdriver
• Methods
– actions that objects can do
– EX: turn screws, pry, chisel
• Classes
– sets of similar objects -- all objects have same kinds
of data & same methods
– EX: lots of screwdrivers that look and act the same.
Java Environment
• Edit (we’ll use Symantec Visual Café)
• Compile (javac creates .class file from .java file)
• Load (java class loader puts .class file into the
computer’s memory)
• Verify (bytecode verifier to ensure security, etc.)
• Execute (interpreter begins running the
program)
Our First Java Program (pg. 57)
• // Fig. 2.1: Welcome.java
// A first program in Java
public class Welcome1 {
//main method begins execution of Java app
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
} //end method main
} //end class Welcome1
Comments
•
// Fig. 2.1: Welcome.java
// A first program in Java
Must start with // for a
single-line comment
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
White space: it doesn’t matter.
•
// Fig. 2.1: Welcome.java
// A first program in Java
White space is empty space
in the program. You can put
as many spaces between
words as you like.
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Class Definition
Defines new class called
Welcome1 that is public.
•
// Fig. 2.1: Welcome.java
// A first program in Java
The class name (by convention)
should be capitalized, and
MUST be the same as the name
of the file (case-sensitive).
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Braces
•
// Fig. 2.1: Welcome.java
// A first program in Java
Braces (squiggly parentheses)
must enclose everything about a
class. MUST be paired.
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Methods
•
// Fig. 2.1: Welcome.java
// A first program in Java
Methods are sets of
instructions, or actions to be
performed.
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Methods
•
// Fig. 2.1: Welcome.java
// A first program in Java
public refers to access
permissions.
void refers to what will be
returned from the method (the
output).
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Methods
main is the name of the
•
// Fig. 2.1: Welcome.java
// A first program in Java
method.
(String args[] )
defines the parameters for
the method.
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Method Definition Body
•
// Fig. 2.1: Welcome.java
// A first program in Java
Entire method body
MUST be surrounded
by braces.
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Method Definition
•
// Fig. 2.1: Welcome.java
// A first program in Java
System.out.println prints
the string “Welcome!” and then
moves the cursor down to the
next line.
println uses the string
“Welcome!” as its argument.
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
} println will print a string, then move to the next
}
line. The similar method print will print a string and
NOT move to the next line.
Method Definition
•
// Fig. 2.1: Welcome.java
// A first program in Java
A statement is an action;
a function. They work
like sentences.
All statements MUST
end with a semicolon.
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Second Java Program: pg. 42
• // Fig. 2.3: Welcome2.java
// Printing a line with multiple statements
public class Welcome2 {
public static void main ( String args [] )
{
System.out.print ( “Welcome to” );
System.out.print ( “ Java Programming!” );
}
}
Second Java Program: pg. 42
• // Fig. 2.3: Welcome2.java
// Printing a line with multiple statements
Class body
Class definition header
Method header
comments
public class Welcome2 {
public static void main ( String args [] )
Method name
{
System.out.print ( “Welcome to” );
System.out.print ( “ Java Programming!” );
}
}
statements
brackets
Escape Sequences
• Inline character sets that do basic things with
text.
• \n newline. Moves cursor to next line.
• \t tab.
Moves cursor to next “tab stop”
• \r return.
Moves cursor back to start of line.
• \\ backslash.
Prints a backslash character.
• \” double quote. Prints a double quote
character.
• \’ single quote. Prints a single quote
character.
Third Java Program: pg. 42
• // Fig. 2.4: Welcome3.java
comments
// Printing multiple lines with one statement
Class definition header
Class body
Method header
public class Welcome3 {
public static void main ( String args [] )
Method name
{
System.out.print ( “Welcome to\nJava!” );
}
}
statement
brackets
Exercise: Welcome1.java, 2,3,4
• To edit: Type in using Notepad
– Begin typing. Click File – Save As…
• Save on a diskette (the A drive).
• Save as type: “All Files (*.*)”
• Save with the correct name: Welcome1.java **note caps**
• To compile: Use MS-DOS window
–
–
–
–
Click Start – Run.
Type in CMD. An MS-DOS window will appear.
Go to the A drive ( like this: C:WIN> cd a: )
Type after the prompt (A:>)
c:/jdk1.3.1_01/bin/javac Welcome1.java
• To execute: Keep using the MS-DOS window
– Type after NO ERRORS ARE FOUND
c:/jdk1.3.1_01/bin/java Welcome1
Friday: Workshop
• Getting comfortable with the JDK
–
–
–
–
Welcome1.java
Welcome2.java
Welcome3.java
Welcome4.java
Monday: Welcome4.java
• Identifiers
• Import statements
• Getting out of the MSDOS window: dialog
boxes