Introduction to Programming Writing Java Beginning Java Programs.
2- Java Programming Introduction
-
Upload
hundaol-tee -
Category
Documents
-
view
234 -
download
0
Transcript of 2- Java Programming Introduction
-
7/29/2019 2- Java Programming Introduction
1/24
Java Programming
Introduction
Muhammad Haris
Lecturer GIS Center, PUCIT
-
7/29/2019 2- Java Programming Introduction
2/24
Muhammad Haris - Lecturer GISCenter PUCIT 2
A Simple Java Program
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {System.out.println("Welcome to Java!");
}
}
-
7/29/2019 2- Java Programming Introduction
3/24
Muhammad Haris - Lecturer GISCenter PUCIT 3
Creating and Editing Using NotePad
-
7/29/2019 2- Java Programming Introduction
4/24
Muhammad Haris - Lecturer GISCenter PUCIT 4
Creating, Compiling, and Running
Programs
Source Code
Create/Modify Source Code
Compile Source Code
i.e., javac Welcome.java
Bytecode
Run Byteode
i.e., java Welcome
Result
If compilation errors
If runtime errors or incorrect result
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");}
}
Method Welcome()
0 aload_0
Method void main(java.lang.String[])
0 getstatic #2
3 ldc #3 5 invokevirtual #4
Saved on the disk
stored on the disk
Source code (developed by the programmer)
Byte code (generated by the compiler for JVMto read and interpret, not for you to understand)
-
7/29/2019 2- Java Programming Introduction
5/24
Muhammad Haris - Lecturer GISCenter PUCIT 5
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Enter main method
-
7/29/2019 2- Java Programming Introduction
6/24
Muhammad Haris - Lecturer GISCenter PUCIT 6
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Execute statement
-
7/29/2019 2- Java Programming Introduction
7/24
Muhammad Haris - Lecturer GISCenter PUCIT 7
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
print a message to the
console
-
7/29/2019 2- Java Programming Introduction
8/24
Muhammad Haris - Lecturer GISCenter PUCIT 8
Compiling and Running Java from
the Command Window
1. Set path to JDK bin directory
set path=c:\Program Files\java\jdk1.5.0\bin
2. Compile
javac Welcome.java
3. Run
java Welcome
-
7/29/2019 2- Java Programming Introduction
9/24
Muhammad Haris - Lecturer GISCenter PUCIT 9
Demonstration
-
7/29/2019 2- Java Programming Introduction
10/24
Muhammad Haris - Lecturer GISCenter PUCIT 10
Anatomy of a Java Program
Comments Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args)
{
System.out.println("Welcome to
Java!");
}
}
-
7/29/2019 2- Java Programming Introduction
11/24
Muhammad Haris - Lecturer GISCenter PUCIT 11
Comments
In Java, comments are preceded by two slashes (//) in
a line, or enclosed between /* and */ in one or multiple
lines. When the compiler sees //, it ignores all text after
// in the same line. When it sees /*, it scans for the next*/ and ignores any text between /* and */.
-
7/29/2019 2- Java Programming Introduction
12/24
Muhammad Haris - Lecturer GISCenter PUCIT 12
Reserved Words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used
for other purposes in the program.
For example, when the compiler sees the word class, it
understands that the word after class is the name for
the class. Other reserved words in the program are
public, static, and void. Their use will be introducedlater in the book.
-
7/29/2019 2- Java Programming Introduction
13/24
Muhammad Haris - Lecturer GISCenter PUCIT 13
Modifiers
Java uses certain reserved words calledmodifiers that specify the properties of the data,methods, and classes and how they can beused.
Examples of modifiers are public and static.Other modifiers are private, final, abstract, andprotected.
A public data, method, or class can be accessed
by other programs. A private data or methodcannot be accessed by other programs.
-
7/29/2019 2- Java Programming Introduction
14/24
Muhammad Haris - Lecturer GISCenter PUCIT 14
Statements
A statement represents an action or a sequence ofactions. The statement
System.out.println("Welcome to Java!")
in the program is a statement to display the greeting"Welcome to Java!" Every statement in Java ends with
a semicolon (;).
-
7/29/2019 2- Java Programming Introduction
15/24
Muhammad Haris - Lecturer GISCenter PUCIT 15
Blocks
A pair of braces in a program forms a block that groups
components of a program.
public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Class block
Method block
-
7/29/2019 2- Java Programming Introduction
16/24
Muhammad Haris - Lecturer GISCenter PUCIT 16
Classes
The class is the essential Java construct.A class is a template or blueprint for objects.
Note: The mystery of the class will continue to be unveiledthroughout this course. For now, though, understand that a program
is defined by using one or more classes.
-
7/29/2019 2- Java Programming Introduction
17/24
Muhammad Haris - Lecturer GISCenter PUCIT 17
Methods
What is System.out.println? It is a method: acollection of statements that performs a sequenceof operations to display a message on the console.
It can be used even without fully understanding thedetails of how it works.
-
7/29/2019 2- Java Programming Introduction
18/24
Muhammad Haris - Lecturer GISCenter PUCIT 18
Method
It is used by invoking a statement with a string argument.System.out.println("Welcome to Java!");
The string argument is enclosed within
Parentheses( ). In this case, the argument is
"Welcome to Java!"
You can call the same println method with a differentargument to print a different message.
System.out.println("Welcome to GIS!");
-
7/29/2019 2- Java Programming Introduction
19/24
Muhammad Haris - Lecturer GISCenter PUCIT 19
main Method
The main method provides the control of program flow.
The Java interpreter executes the application by
invoking the main method.
The main method looks like this:
public static void main(String[] args) {
// Statements;
}
-
7/29/2019 2- Java Programming Introduction
20/24
Muhammad Haris - Lecturer GISCenter PUCIT 20
Displaying Text in a Message Dialog
Box
you can use the showMessageDialog method in
the JOptionPane class.
JOptionPane is one of the many predefinedclasses in the Java system, which can be reused
rather than reinventing the wheel.
-
7/29/2019 2- Java Programming Introduction
21/24
Muhammad Haris - Lecturer GISCenter PUCIT 21
The showMessageDialog Method
JOptionPane.showMessageDialog(null,"Welcome to Java!",
Display Message",
JOptionPane.INFORMATION_MESSAGE));
-
7/29/2019 2- Java Programming Introduction
22/24
Complete Program
import javax.swing.JOptionPane;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");JOptionPane.showMessageDialog(null, "Welcome to Java!", "Display
Message",JOptionPane.INFORMATION_MESSAGE);
}
}
Muhammad Haris - Lecturer GISCenter PUCIT 22
-
7/29/2019 2- Java Programming Introduction
23/24
2 Ways of Input Output
Input:
System.out.println("Enter your name");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
Output:System.out.println("Welcome " + input);
Input:
String input = JOptionPane.showInputDialog("Enter your name");
Output:
JOptionPane.showMessageDialog (null, "Welcome " + input );
Muhammad Haris - Lecturer GISCenter PUCIT 23
-
7/29/2019 2- Java Programming Introduction
24/24
Muhammad Haris - Lecturer GISCenter PUCIT 24
Assignment
Read Next Slide
Variables and Identifiers