02-IntroJavaProg 1
02-IntroJavaProg 1
02-IntroJavaProg 1
2. Introduction to java
programming _ 1
1
Creating, Compiling, and Running
Programs
Create/Modify Source Code
Result
2
If runtime errors or incorrect result
Parts of a Java Program
A Java source code file contains one or more
Java classes.
If more than one class is in a source code file,
only one of them may be public.
The public class and the filename of the
source code file must match.
ex: A class named HelloApp must be in a file named
HelloApp.java
Each Java class can be separated into parts.
3
Parts of a Java Program
Example: HelloApp.java
To compile the example:
javac HelloApp.java
Notice the .java file extension is needed.
created.
To run the example:
java HelloApp
Notice there is no file extension here.
4
Analyzing the Example
This is a Java comment. It is
ignored by the compiler.
// This is my first Java program.
This is the class header
public class HelloApp
for the class HelloApp
{
5
Analyzing the Example
6
Analyzing the Example
9
Comments on several lines
10
Key Words
Words that have a specific meaning to the
compiler
Key words in the sample program are:
•public •void •boolean •private
•class •int •continue •protected
•static •double •return •package
(See Appendix A, “Java Keywords” from your textbook)
14
Statements
represents an action or a sequence of actions.
Example of statement:
System.out.println("Welcome to Java!")
is a statement to display the greeting
"Welcome to Java!"
Every statement in Java ends with a semicolon (;).
15
Blocks
17
Classes
Example 1:
public class ClassA {
public static void main (String[] args) {
System.out.println ("Try your best");
}
}
18
Classes
class ClassB {
public static void main (String[] args) {
ClassA obj1 = new ClassA();
System.out.println (“Your age: “ + (2009 - obj1.getYearBorn()));
System.out.println (“My message: “ + obj1.methodA());
} 19
}
Methods
21
main Method
24
import
Full library class names include the package
name. For example:
java.awt.Color
javax.swing.JButton
import statements at the top of the source file
let you refer to library classes by their short
names: Fully-qualified
import javax.swing.JButton; name
...
JButton go = new JButton("Go");
25
import (cont’d)
You can import names for all the classes in a
package by using a wildcard .*:
import java.awt.*; Imports all classes
from awt, awt.event,
import java.awt.event.*; and swing packages
import javax.swing.*;
java.lang is imported automatically into all
classes; defines System, Math, Object,
String, and other commonly used classes.
26
Package
package book;
import cert.*; // Import all classes in the cert
package class Goo {
public static void main(String[] args) {
Sludge o = new Sludge();
o.testIt();
} 28
}
Source File Declaration Rules
A source code file can have only one public
class.
If the source file contains a public class, the
filename must match the public class name.
A file can have only one package statement, but
multiple imports.
The package statement (if any) must be the first
(non-comment) line in a source file.
The import statements (if any) must come after
the package and before the class declaration.
29
Source File Declaration Rules (cont.)
30