Download First Program - Department of Computer and Information Science

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

Structured programming wikipedia , lookup

Go (programming language) wikipedia , lookup

Class (computer programming) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Java syntax wikipedia , lookup

Comment (computer programming) wikipedia , lookup

Scala (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Name mangling wikipedia , lookup

Java (programming language) wikipedia , lookup

Java performance wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Department of Computer and Information Science,
School of Science, IUPUI
Introduction to Java
- First Program
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: [email protected]
Dale Roberts
Java Program Structure
In the Java programming language:
A program is made up of one or more classes
A class contains one or more methods
A method contains program statements
Program statements can reference local or instance
variables. There is no concept of global variable.
These terms will be explored in detail
throughout the course
A Java application always contains a method
called main
Dale Roberts
1-2
Java Program Structure
//
comments about the class
public class MyProgram
{
class header
class body
Comments can be placed almost anywhere
}
© 2004 Pearson
Addison-Wesley. All
rights reserved
Dale Roberts
1-3
Java Program Structure
//
comments about the class
public class MyProgram
{
//
comments about the method
public static void main (String[] args)
{
method body
}
}
© 2004 Pearson
Addison-Wesley. All
rights reserved
Dale Roberts
1-4
method header
Comments
Comments in a program are called inline documentation
They should be included to explain the purpose of the
program and describe processing steps
They do not affect how a program works
Java comments can take three forms:
// this comment runs to the end of the line
/*
this comment runs to the terminating
symbol, even across line breaks
/** this is a javadoc comment
© 2004 Pearson
Addison-Wesley. All
rights reserved
Dale Roberts
1-5
*/
*/
Identifiers
Identifiers are the words a programmer uses in a
program
An identifier can be made up of letters, digits, the
underscore character ( _ ), and the dollar sign
Identifiers cannot begin with a digit
Java is case sensitive - Total, total, and TOTAL
are different identifiers
By convention, programmers use different case styles
for different types of identifiers, such as
title case for class names - Lincoln
upper case for constants - MAXIMUM
© 2004 Pearson
Addison-Wesley. All
rights reserved
Dale Roberts
1-6
First Program in Java: Printing a Line of Text
Application
Executes when you use the java command to launch the
Java Virtual Machine (JVM)
Sample program
Displays a line of text
Illustrates several important Java language features
7
Dale Roberts
Outline
Welcome1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
// Fig. 2.1: Welcome1.java
// Text-printing program.
public class Welcome1
{
// main method begins execution of Java application
public static void main( String args[] )
{
System.out.println( "Welcome to Java Programming!" );
} // end method main
} // end clazss Welcome1
Welcome to Java Programming!
Dale Roberts
8
First Program in Java: Printing a Line of Text (Cont.)
1
// Fig. 2.1: Welcome1.java
Comments start with: //
Comments ignored during program execution
Document and describe code
Provides code readability
Traditional comments: /* ... */
/* This is a traditional
comment. It can be
split over many lines */
2
// Text-printing program.
Another line of comments
Note: line numbers not part of program, added for reference
9
Dale Roberts
First Program in Java: Printing a Line of Text (Cont.)
4
public class Welcome1
Saving files
File name must be class name with .java extension
Welcome1.java
5
{
Left brace {
Begins body of every class
Right brace ends declarations (line 13)
10
Dale Roberts
First Program in Java: Printing a Line of Text (Cont.)
7
public static void main( String args[] )
Part of every Java application
Applications begin executing at main
Parentheses indicate main is a method (Ch. 3 and 6)
Java applications contain one or more methods
Exactly one method must be called main
Methods can perform tasks and return information
void means main returns no information
For now, mimic main's first line
8
{
Left brace begins body of method declaration
Ended by right brace } (line 11)
11
Dale Roberts
2.2 First Program in Java: Printing a Line of Text (Cont.)
9
System.out.println( "Welcome to Java Programming!"
Instructs
computer to perform an action
);
Prints string of characters
String – series of characters inside double quotes
White-spaces in strings are not ignored by compiler
System.out
Standard output object
Print to command window (i.e., MS-DOS prompt)
Method System.out.println
Displays line of text
This line known as a statement
Statements must end with semicolon ;
12
Dale Roberts
First Program in Java: Printing a Line of Text (Cont.)
11
} // end method main
Ends method declaration
13
} // end class Welcome1
Ends class declaration
Can add comments to keep track of ending braces
13
Dale Roberts
First Program in Java: Printing a Line of Text (Cont.)
Compiling a program
Open a command prompt window, go to directory where
program is stored
Type javac Welcome1.java
If no syntax errors, Welcome1.class created
Has bytecodes that represent application
Bytecodes passed to JVM
14
Dale Roberts
First Program in Java: Printing a Line of Text (Cont.)
Executing a program
Type java Welcome1
Launches JVM
JVM loads .class file for class Welcome1
.class extension omitted from command
JVM calls method main
15
Dale Roberts
Executing Welcome1 in a Microsoft Windows XP
Command Prompt window.
You type this command to execute
the application
The program outputs
Welcome to Java Programming!
Dale Roberts
16
Displaying Text with printf
System.out.printf
Feature added in Java SE 5.0
Displays formatted data
9
10
System.out.printf( "%s\n%s\n",
"Welcome to", "Java Programming!" );
Format string
Fixed text
Format specifier – placeholder for a value
Format specifier %s – placeholder for a string
17
Dale Roberts
Acknowledgements
http://java.sun.com/docs/books/tutorial/getStarted/TOC.html
Pearson Education, Lewis and Loftus.
Deitel, Java How to Program
http://www.cs.wustl.edu/~plezbert/contcom/thesis/node6.html
http://www.cs.usfca.edu/~parrt/course/652/lectures-Spring2004/language.impl.overview.pdf
http://ei.cs.vt.edu/~history/Youmans.Java.html
Dale Roberts