Download Code Listing: Parts of a Java Program

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
Parts of a Java Program
SUMMARY OF CHAPTER 2: JAVA
FUNDAMENTS
STARTING OUT WITH JAVA: OBJECTS
2
Code Listing:
Parts of a Java
Program
Reminder: The names
of Java source files
end with .java
Closer examination of
program, line by line:
1.// marks the beginning
of a comment, the
compiler ignores
everything from the
double slashes to the end
of the line.
Purpose is to document
the code
Chapter 2: Parts of a Program
3
Code Listing:
Parts of a Java
Program
Marks beginning of a
“class definition”
public is an access
specifier
Line 2: Blank – Known
as “white space,” to
make the program
easier to read
Line 3: class header
(click to read more)
class is written in all
lower case, Java keyword,
beginning of a class
definition
Chapter 2: Parts of a Program
Simple –
name of the
class, usually
in Book title
- upper case
(user
defined)
4
Code Listing:
Parts of a Java
Program
Line 4.
The Left brace is like
the left cover of a
book, it is the
beginning of the
class definition
Chapter 2: Parts of a Program
Left brace –
open brace
5
Code Listing:
Parts of a Java
Program
Line 5: A method header
A group of one or more
programming statements that
has a name.
public static void main(String[]
args)
1.
public - methods are public
or private, public ones are
available from outside the
class
2.
static - all lower-case
3.
void – methods can be used
to return a value, if it does
NOT return a value it is void
4.
main – all lower case, every
Java application has to have a
least one main, but it can call
other classes and instantiate
objects from classes known to
the project (see import)
5.
Parameters
Chapter 2: Parts of a Program
every Java
application has to
have one main,
6
Code Listing:
Parts of a Java
Program
Line 6:
Another left brace, but
this marks the
beginning of the
method body
Chapter 2: Parts of a Program
7
Code Listing:
Parts of a Java
Program
Line 7 Java statement, ends
with a semicolon
System – group of classes
out – output class
println – method that prints
(outputs) a line of output and
advances the cursor to the next
line down
String literal – all the characters
within the quotation marks, put
into the output “stream” just as
it appears.
Line 8: a closing or right brace,
ends the main method
Chapter 2: Parts of a Program
8
Code Listing:
Parts of a Java
Program
Line 9: closes the class
Chapter 2: Parts of a Program
9
Code Listing:
Parts of a Java
Program
Summary of Simple
program,
•Comments (remarks
in Visual Basic) do not
end with a semicolon,
they are ignored by
the compiler.
•Class headers and
method headers
do not end with a
semicolon
•Use left and right
braces to begin or end
a class or method
definition.
Chapter 2: Parts of a Program
Review
10
 Java is case sensitive
 All Java programs must be stored





in a file with a name that ends
with the .java extension
Comments are ignored
A .java file can have many classes
but only one public class
Every Java application must have
a method called main
For every left brace there must be
a right
Statements end with semicolon
Chapter 2: Parts of a Program