Download ppt - AD Book Enterprises

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

Programming language wikipedia , lookup

Library (computing) wikipedia , lookup

Go (programming language) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

String literal wikipedia , lookup

String (computer science) wikipedia , lookup

Java syntax wikipedia , lookup

Structured programming wikipedia , lookup

Scala (programming language) wikipedia , lookup

C++ wikipedia , lookup

Class (computer programming) wikipedia , lookup

Name mangling wikipedia , lookup

C Sharp syntax wikipedia , lookup

Object-oriented programming wikipedia , lookup

Java (programming language) wikipedia , lookup

Java performance wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
1/13 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
• // Fig. 2.1: Welcome.java
// A first program in Java
public class Welcome1 {
public static void main ( String args[] )
{
System.out.println ( “Welcome!” );
}
}
Comments
Must start with // for a
single-line comment
•
// Fig. 2.1: Welcome.java
// A first program in Java
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
•
// Fig. 2.1: Welcome.java
// A first program in Java
main is the name of the
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
Next Time: Welcome4.java
• Identifiers
• Import statements
• Getting out of the MSDOS window: dialog
boxes