0% found this document useful (0 votes)
34 views

CSC103-Programming Fundamentals: Introduction To Java Lecture-5

The document provides an outline of Lecture 5 of the CSC103 Programming Fundamentals course. It discusses topics like installing Java, features of Java, the anatomy of a Java program, and programming style. It also explains how to create, compile, and run a simple Java program that prints "Welcome to Java!".

Uploaded by

Gul khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

CSC103-Programming Fundamentals: Introduction To Java Lecture-5

The document provides an outline of Lecture 5 of the CSC103 Programming Fundamentals course. It discusses topics like installing Java, features of Java, the anatomy of a Java program, and programming style. It also explains how to create, compile, and run a simple Java program that prints "Welcome to Java!".

Uploaded by

Gul khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

CSC103-Programming Fundamentals

Introduction to Java
Lecture-5

Instructors
Dr. Manzoor Illahi Tamimi, Dr.Behjat Zuhaira, Ms. Saadia Maqbool and Mr. Rizwan Rashid
Lecture Outline
• About Java
• Installing Java
• Features of Java
• A Simple Java Program
• Anatomy of a Java Program
• Creating, Compiling, and Running Programs
• Programming Style and Documentation
• Programming Errors
About Java
• Java is a powerful and versatile programming
language for developing software running on
mobile devices, desktop computers, and
servers
• Developed by James Gosling in 1995
• Popular because of write a program once and
run it anywhere
JDK Editions

• Java Standard Edition (Java SE)


– J2SE can be used to develop client-side standalone
applications or applets.
• Java Enterprise Edition (Java EE)
– J2EE can be used to develop server-side applications such
as Java servlets, Java ServerPages, and Java ServerFaces.
• Java Micro Edition (Java ME)
– J2ME can be used to develop applications for mobile
devices such as cell phones.
JDK Versions
• History of JDK version
– https://en.wikipedia.org/wiki/Java_version_history
• Latest is Java SE 15
• Download from: https://
www.oracle.com/java/technologies/javase-jdk15
-downloads.html
Installing Java
• To check if Java is installed (open command
prompt and type following command)
java –version
• Setting Java Path
– Temporary Path
• Open the command prompt
• Copy the path of the JDK/bin directory
• Write in command prompt: set path=copied_path
– Permanent Path using Environment Variables
Settings
Environment Variable
• Go to "System Properties" (Can be found on Control Panel >
System and Security > System > Advanced System Settings)
• Click on the "Environment variables" button under the
"Advanced" tab
• Then, select the "Path" variable in System variables and click
on the "Edit" button
• Click on the "New" button and add the path where Java is
installed, followed by \bin. By default, Java is installed in C:\
Program Files\Java\jdk-15\bin
Then, click "OK", and save the settings
• At last, open Command Prompt (cmd.exe) and type java -
version to see if Java is running on your machine
Popular Java IDEs
• NetBeans
• Eclipse
Features of Java
• Simple
• Object-Oriented
• Portable
• Platform independent
• Secured
• Robust
• Architecture neutral
• Interpreted
• High Performance
• Multithreaded
• Distributed
• Dynamic
A Simple Java Program

Save this file as Welcome.java

To compile: javac Welcome.java


To execute: java Welcome
Anatomy of a Java Program
• Class name
• Main method
• Statements
• Statement terminator
• Reserved words
• Comments
• Blocks
Class Name
Every Java program must have at least one class.
Each class has a name. By convention, class names
start with an uppercase letter. In this example, the
class name is Welcome.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Main Method
Line 2 defines the main method. In order to run a
class, the class must contain a method named
main. The program is executed from the main
method.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Statement
A statement represents an action or a sequence of
actions. The statement
System.out.println("Welcome to Java!")
in the program is a statement to display the greeting
"Welcome to Java!“.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Statement Terminator
Every statement in Java ends with a semicolon (;).

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
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 this program are public,
static, and void.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Blocks
A pair of braces in a program forms a block that groups
components of a program.

p
ubl
iccla
ssTes
t{
pu
bli
cstat
icvoi
dmain
(St
ring
[]a
rgs
){ C
la
ssb
loc
k
Sys
tem
.ou
t.p
rin
tln
("W
elc
ometoJ
ava
!")
;Me
tho
dblo
ck
}
}
Special Symbols

Character Name Description

{} Opening and closing Denotes a block to enclose statements.


braces
() Opening and closing Used with methods.
parentheses
[] Opening and closing Denotes an array.
brackets
// Double slashes Precedes a comment line.

" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
Creating, Compiling, and
Running Programs
Compiling Java Source Code
You can port a source program to any machine with appropriate
compilers. The source program must be recompiled, however,
because the object program can only run on a specific machine.
Nowadays computers are networked to work together. Java was
designed to run object programs on any platform. With Java, you
write the program once, and compile the source program into a
special type of object code, known as bytecode. The bytecode can
then run on any computer with a Java Virtual Machine, as shown
below. Java Virtual Machine is a software that interprets Java
bytecode.
Programming Style and
Documentation
• Appropriate Comments
• Naming Conventions
• Proper Indentation and Spacing Lines
• Block Styles
Appropriate Comments
Include a summary at the beginning of the
program to explain what the program does, its key
features, its supporting data structures, and any
unique techniques it uses.

Include your name, class section, instructor, date,


and a brief description at the beginning of the
program.
Naming Conventions
• Choose meaningful and descriptive names
• Class names:
– Capitalize the first letter of each word in the name.
For example, the class name ComputeExpression.
Proper Indentation and Spacing
• Indentation
– Indent two spaces.

• Spacing
– Use blank line to separate segments of the code.
Block Styles
A block is a group of statements surrounded by braces. There are two popular
styles, next-line style and end-of-line style

N e
xt-lin
e p
ubl
iccla
ssTes
t
sty
le {
pu
bli
cstat
icvoi
dmain
(St
rin
g[]ar
gs)
{
Sys
tem
.ou
t.p
rin
tln
("B
loc
kStyl
es"
);
}
}

E n
d-of-lin
e
sty
le
p
ubl
iccla
ssTes
t{
pu
bli
cstat
icvoi
dmain
(St
rin
g[]ar
gs){
Sys
tem
.ou
t.p
rin
tln
("B
loc
kStyl
es"
);
}
}
Programming Errors
• Syntax Errors
– Detected by the compiler
• Runtime Errors
– Causes the program to abort
– System.out.println(1 / 0);
• Logic Errors
– Produces incorrect result
Summary
• Features of Java
• Parts of a simple Java Program
• How to write, compile and run a Java Program
• Program Documentation
• Program Errors
Next Lecture
• Simple Computation in Java Program
• Obtaining Input from Console
• Identifiers to name variables, constants,
methods, and classes
• Java Numeric Primitive Data types

You might also like