Dale Roberts, Lecturer Department of Computer and Information Science IUPUI
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI...
Transcript of Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI...
Dale Roberts
Introduction to JavaIntroduction to Java
- First Program- First Program
Dale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected]
Department of Computer and Information Science,School of Science, IUPUI
Dale Roberts1-2
Java Program StructureJava Program Structure
In the Java programming language:In the Java programming language:A program is made up of one or more A program is made up of one or more classesclasses
A class contains one or more A class contains one or more methodsmethods
A method contains program A method contains program statementsstatements
Program statements can reference local or instance Program statements can reference local or instance variables. There is no concept of global variable.variables. There is no concept of global variable.
These terms will be explored in detail These terms will be explored in detail throughout the coursethroughout the course
A Java application always contains a method A Java application always contains a method called called mainmain
Dale Roberts
© 2004 Pearson Addison-Wesley. All rights reserved 1-3
Java Program StructureJava Program Structure
public class MyProgram
{
}
// comments about the class
class header
class body
Comments can be placed almost anywhere
Dale Roberts
© 2004 Pearson Addison-Wesley. All rights reserved 1-4
Java Program StructureJava Program Structure
public class MyProgram
{
}
// comments about the class
public static void main (String[] args)
{
}
// comments about the method
method headermethod body
Dale Roberts
© 2004 Pearson Addison-Wesley. All rights reserved 1-5
CommentsComments
Comments in a program are called Comments in a program are called inline documentationinline documentation
They should be included to explain the purpose of the They should be included to explain the purpose of the program and describe processing stepsprogram and describe processing steps
They do not affect how a program worksThey do not affect how a program works
Java comments can take three forms: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 */
Dale Roberts
© 2004 Pearson Addison-Wesley. All rights reserved 1-6
IdentifiersIdentifiers
IdentifiersIdentifiers are the words a programmer uses in a are the words a programmer uses in a programprogram
An identifier can be made up of letters, digits, the An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar signunderscore character ( _ ), and the dollar sign
Identifiers cannot begin with a digitIdentifiers cannot begin with a digit
Java is Java is case sensitivecase sensitive - - Total, total, Total, total, andand TOTAL TOTAL are different identifiersare different identifiers
By convention, programmers use different case styles By convention, programmers use different case styles for different types of identifiers, such asfor different types of identifiers, such as
title case title case for class names - for class names - LincolnLincoln
upper caseupper case for constants - for constants - MAXIMUMMAXIMUM
Dale Roberts7
First Program in Java: Printing a Line of TextFirst Program in Java: Printing a Line of Text
ApplicationApplicationExecutes when you use the Executes when you use the javajava command to launch the command to launch the Java Virtual Machine (JVM)Java Virtual Machine (JVM)
Sample programSample programDisplays a line of textDisplays a line of text
Illustrates several important Java language featuresIllustrates several important Java language features
Dale Roberts
OutlineOutline
1 // Fig. 2.1: Welcome1.java 2 // Text-printing program. 3 4 public class Welcome1 5 { 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.println( "Welcome to Java Programming!" ); 10 11 } // end method main 12 13 } // end clazss Welcome1
Welcome to Java Programming!
Welcome1.javaWelcome1.java
8
Dale Roberts9
First Program in Java: Printing a Line of Text (Cont.)First Program in Java: Printing a Line of Text (Cont.)
Comments start with: Comments start with: ////Comments ignored during program executionComments ignored during program executionDocument and describe codeDocument and describe codeProvides code readabilityProvides code readability
Traditional comments: Traditional comments: /* ... *//* ... *//* This is a traditional/* This is a traditional comment. It can be comment. It can be split over many lines */ split over many lines */
Another line of commentsAnother line of commentsNote: line numbers not part of program, added for referenceNote: line numbers not part of program, added for reference
1 // Fig. 2.1: Welcome1.java
2 // Text-printing program.
Dale Roberts10
First Program in Java: Printing a Line of Text (Cont.)First Program in Java: Printing a Line of Text (Cont.)
Saving filesSaving filesFile name must be class name with File name must be class name with .java.java extension extension
Welcome1.javaWelcome1.java
Left brace Left brace {{Begins body of every classBegins body of every class
Right brace ends declarations (line 13)Right brace ends declarations (line 13)
4 public class Welcome1
5 {
Dale Roberts11
First Program in Java: Printing a Line of Text (Cont.)First Program in Java: Printing a Line of Text (Cont.)
Part of every Java applicationPart of every Java applicationApplications begin executing at Applications begin executing at mainmain
Parentheses indicate Parentheses indicate mainmain is a method (Ch. 3 and 6) is a method (Ch. 3 and 6)Java applications contain one or more methodsJava applications contain one or more methods
Exactly one method must be called Exactly one method must be called mainmain
Methods can perform tasks and return informationMethods can perform tasks and return informationvoidvoid means means mainmain returns no information returns no informationFor now, mimic For now, mimic mainmain's first line's first line
Left brace begins body of method declarationLeft brace begins body of method declarationEnded by right brace Ended by right brace }} (line 11) (line 11)
7 public static void main( String args[] )
8 {
Dale Roberts12
2.2 First Program in Java: Printing a Line of Text (Cont.)2.2 First Program in Java: Printing a Line of Text (Cont.)
Instructs computer to perform an actionInstructs computer to perform an actionPrints string of characters Prints string of characters
String – series of characters inside double quotesString – series of characters inside double quotesWhite-spaces in strings are not ignored by compilerWhite-spaces in strings are not ignored by compiler
System.outSystem.outStandard output objectStandard output objectPrint to command window (i.e., MS-DOS prompt)Print to command window (i.e., MS-DOS prompt)
Method Method System.out.printlnSystem.out.println Displays line of textDisplays line of text
This line known as a statementThis line known as a statementStatements must end with semicolon Statements must end with semicolon ;;
9 System.out.println( "Welcome to Java Programming!" );
Dale Roberts13
First Program in Java: Printing a Line of Text (Cont.)First Program in Java: Printing a Line of Text (Cont.)
Ends method declarationEnds method declaration
Ends class declarationEnds class declaration
Can add comments to keep track of ending bracesCan add comments to keep track of ending braces
11 } // end method main
13 } // end class Welcome1
Dale Roberts14
First Program in Java: Printing a Line of Text (Cont.)First Program in Java: Printing a Line of Text (Cont.)
Compiling a programCompiling a programOpen a command prompt window, go to directory where Open a command prompt window, go to directory where program is storedprogram is stored
Type Type javacjavac Welcome1.javaWelcome1.java
If no syntax errors, If no syntax errors, Welcome1.classWelcome1.class created createdHas bytecodes that represent applicationHas bytecodes that represent application
Bytecodes passed to JVMBytecodes passed to JVM
Dale Roberts15
First Program in Java: Printing a Line of Text (Cont.)First Program in Java: Printing a Line of Text (Cont.)
Executing a programExecuting a programTypeType javajava Welcome1Welcome1
Launches JVMLaunches JVM
JVM loads JVM loads .class.class file for class file for class Welcome1Welcome1
.class.class extension omitted from command extension omitted from command
JVM calls method JVM calls method mainmain
Dale Roberts
Executing Welcome1 in a Microsoft Windows XP Executing Welcome1 in a Microsoft Windows XP Command Prompt window.Command Prompt window.
16
You type this command to execute the application
The program outputs
Welcome to Java Programming!
Dale Roberts17
Displaying Text with Displaying Text with printfprintf
System.out.printfSystem.out.printfFeature added in Java SE 5.0Feature added in Java SE 5.0
Displays formatted dataDisplays formatted data
Format stringFormat stringFixed textFixed text
Format specifier – placeholder for a valueFormat specifier – placeholder for a value
Format specifier Format specifier %s%s – – placeholder for a stringplaceholder for a string
9 System.out.printf( "%s\n%s\n", 10 "Welcome to", "Java Programming!" );
Dale Roberts
AcknowledgementsAcknowledgementshttp://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-Spring-2004/language.impl.overview.pdf
http://ei.cs.vt.edu/~history/Youmans.Java.html