Islamic Emirate of Afghanistan
Ministry of Higher Education
Herat University
Computer Science Faculty
Introduction to Practical Computer Science 1 (Semester 1)
First Java Program
Lecture 02
Lecturer: Mohammed Hamed Amiry
[Link].2015@[Link]
8/21/2025 1
Content…
How many types of computer Language do we have?
What is software translator?
How many types of Software translator do we have?
What is different between Interpreter and Compiler?
What is bytecode?
What is JVM?
Introduction…
Power of Java Programming Language
• Platform Independence: Java compilers do not produce native object
code for a particular platform but rather 'byte code’ instructions for the
Java Virtual Machine(JVM). It means they can be run on any operating
system with any type of processor as long as the Java interpreter is
available on that system: Windows, Mac OS, Linux, whatever.
• Java is easy to learn.
• The java has API (Application Programming Interface).
• Java programs can be run on every systems a JVM exists for
without being recompiled.
• It is faster than a real interpreter language
Structure of a Java Source file
A source file contains source code that is written in the Java
programming language.
• Java Source file consist of pieces called Classes.
• Classes consist of pieces called Methods that perform tasks and return
information when they complete their tasks.
• A method contains Statements.
How to write and run a Java Program?
[Link] a source file
• A source file contains code, written in the Java programming
language, that programmers can understand. You can use any
text editor to create and edit source files. Save your source file.
$ [Link]
[Link] the source file into a .class file
• The Java programming language compiler (javac) takes your
source file and translates its text into instructions that the Java
virtual machine can understand. Start the Java Compiler with the
command:
$ javac [Link]
[Link] the program
• uses the Java virtual machine to run your application.
$ java filename
How to write and run a Java Program?
• Writing .java file.
• Compile .java file ->.class file
• Execute .class file ->JVM
A First Program in Java:
Printing a Line of Text
• Open you editor (notepad).
• Open a new file and save it [Link].
• Type in the following the text and save it again.
Comments in java (Line 1 and 2)
• We insert comments to document programs and improve their readability.
• The Java compiler ignores comments, so they do not cause the computer to
perform any action when the program is run.
• Types of java comments:
• end-of-line comment
• begins with //, it terminates at the end of the line
• traditional comments
• begin and end with delimiters, /* and */.
• The compiler ignores all text between the delimiters.
• Javadoc comments
• These are delimited by /** and */.
• The compiler ignores all text between the delimiters.
Blank Lines (Line 3)
• Blank lines, space characters and tabs make programs easier to read.
• Together, they’re known as white space (or whitespace).
• The compiler ignores white space.
Declaring a Class (Line 4)
• Every Java program consists of at least one class that you (the programmer)
define.
• The class keyword introduces a class declaration and is immediately
followed by the class name (Welcome1).
• Keywords (sometimes called reserved words) are reserved for use by Java
and are always spelled with all lowercase letters.
• Every class we define begins with the public keyword. For now, we simply
require public.
• You’ll learn more about public and non-public classes later.
Filename for a public Class
• A public class must be placed in a file that has a filename of the form
[Link], so class Welcome1 is stored in the file [Link].
• If the class is not start with public keyword, the it is not important to save
java filename same as the class name.
Class Names and Identifiers
• By convention, class names begin with a capital letter and capitalize the first
letter of each word they include (e.g., SampleClassName).
• A class name is an identifier(a series of characters consisting of letters, digits,
underscores (_) and dollar signs ($) that does not begin with a digit and does
not contain spaces).
• Some valid identifiers are Welcome1, $value, _value, m_inputField1 and
button7. The name 7button is not a valid identifier because it begins with a
digit, and the name input field is not a valid identifier because it contains a
space.
• Normally, an identifier that does not begin with a capital letter is not a class
name.
• Java is case sensitive—uppercase and lowercase letters are distinct—so value
and Value are different (but both valid) identifiers.
Class Body (Line 5)
• { } The left and right braces indicates the body of a class definition or a
method definition.
• A left brace (as in line 5), {, begins the body of every class declaration.
• A corresponding right brace (at line 11), }, must end each class declaration.
Lines 6–10 are indented.
Good Programming Habit: Get into the habit of adding the right brace as
soon as you enter the left brace (then fill in between).
Declaring a Method (Line 7)
public static void main(String[] args)
• is the starting point of every Java application.
• The parentheses after the identifier main indicate that it’s a program building
block called a method.
• Java class declarations normally contain one or more methods.
• For a Java application, one of the methods must be called main and must be
defined as shown in line 7; otherwise, the Java Virtual Machine (JVM) will not
execute the application.
Performing Output (Line 9)
[Link]("Welcome to Java Programming!");
• instructs the computer to perform an action—namely, to display the
characters contained between the double quotation marks (the quotation marks
themselves are not displayed).
• Together, the quotation marks and the characters between them are a
string—also known as a character string or a string literal.
• White-space characters in strings are not ignored by the compiler.
• Strings cannot span multiple lines of code.
A First Program in Java:
Printing a Line of Text
[Link] sure that the class name is the same like your file name.
[Link] the program in your console >javac [Link]
Continue...
3. Run your program in your console: java Welcome1
Programming Errors
• Errors will occur when developing applications irrespective of the target
language.
• Programming errors are unavoidable, even for experienced programmers.
• Programming errors can be categorized into three types:
• syntax errors, runtime errors, and logic errors.
Programming Errors
Syntax Errors
Syntax errors:
• Errors that are detected by the compiler are called syntax errors or
compile errors.
• Syntax errors result from errors in code construction, such as mistyping a
keyword, omitting some necessary punctuation, or using an opening
brace without a corresponding closing brace.
• These errors are usually easy to detect because the compiler tells you
where they are and what caused them.
Programming Errors
Syntax Errors
Example of Syntax Error
Programming Errors
Runtime Errors
• Runtime errors are errors that cause a program to terminate
abnormally.
• They occur while a program is running if the environment detects
an operation that is impossible to carry out.
• Input mistakes typically cause runtime errors.
Programming Errors
Runtime Errors Examples
• An input error occurs when the program is waiting for the user to
enter a value, but the user enters a value that the program cannot
handle. For instance, if the program expects to read in a number,
but instead the user enters a string, this causes data-type errors to
occur in the program.
• Another example of runtime errors is division by zero. This
happens when the divisor is zero for integer divisions.
Programming Errors
Logic Errors
• Logic errors occur when a program does not perform the way it
was intended to.
• Errors of this kind occur for many different reasons.
Programming Errors
• In general, syntax errors are easy to find and easy to correct
because the compiler gives indications as to where the errors came
from and why they are wrong.
• Runtime errors are not difficult to find, either, since the reasons
and locations for the errors are displayed on the console when the
program aborts.
• Finding logic errors, on the other hand, can be very challenging.
Common Errors
• Missing a closing brace, missing a semicolon, missing quotation marks for strings,
and misspelling names are common errors for new programmers.
Summery
• Java is an high level language and object orientated programing
language
• Platform Independence, Applet Interface and API are Advantages of
java.
• A source file contains source code.
• Programming Errors will occur when developing applications irrespective of
the target language such as Syntax Errors, Runtime Errors
and Logic Errors.
For more study
• Chapter 2 (35 – 43) • Chapter 1 (34 – 44)
Next Lecture On
• Elementary Programming
Q &A SECTION
8/21/2025 30