ICT 314 (3)
Object Oriented Programming Languages
Interacting with Java Programs
Dr. R. A. H. M. Rupasingha
© Amir Kirsh Senior Lecturer in Computer Science
Content • First Java program
• What are keywords?
• Java escape sequences
Java Commands for Compile and Run
Installing Java
• For executing any java program, you need to install the
Java Development Kit (JDK) if you don't have installed it,
download the JDK and install it.
3
Java Commands for
Compile and Run (Cont.)
JDK Versions
JDK 14 March, 17th 2020
September, 2023
4 JDK 21
Java Commands for Compile and Run (Cont.)
Setting the PATH variable
• If you are saving the java source file inside the jdk/bin
directory, path is not required to be set because all the
tools will be available in the current directory.
• But if you are having your java file outside the jdk/bin
folder, it is necessary to set path of JDK.
• Set path=“the path to the bin folder”
Eg: C:\Program Files\Java\jdk1.8.0_73\bin
5
Simple Program of Java
Creating a Source File
• Any text editor can be used to create a source file
E.g. Notepad
• Note: Java is case sensitive
Name of class Main method
class FirstJP Semi colon;
{ Indicate end
Beginning public static void main(String args[]) of a statement
{
System.out.println("Hello World…!");
}
End }
Print statement of Java
6
Dos Commands
• We can use dos OS for compile and run the program.
• CD – “change directory”
this command is used to change the current directory
Eg: the directory in which the user is currently working.
• Syntax of CD
CD [directory name/ directory name with path]
Eg: cd E:\15SSL1234
7
Java Commands for Compile and Run
• For compile
javac SoursefileName.java
Eg: javac FirstJP.java
• For Run
java ClassName
Eg: java FirstJP
8
Compile and Run
9
Compile and Run (Cont.)
Eg:
First - compile
10
Compile and Run (Cont.)
Eg:
Then - run
11
Compile and Run (Cont.)
Create/Modify Source Code
Source Code
Compile Source Code
i.e. javac Welcome.java
If compilation errors
Bytecode
Run Byteode
i.e. java Welcome
Result
If runtime errors or incorrect result
12
Error Types
• Syntax error / Compile error
- caught at compile time.
- compiler did not understand or compiler does not
allow
• Runtime error
- something “Bad” happens at runtime. Java breaks
these into Errors and Exceptions
• Logic error
- program compiles and runs, but does not do what
you intended or want
13
Structure of Java programs
public class <name> {
public static void main(String[] args) {
<statement>;
<statement>;
...
<statement>;
}
}
• Every executable Java program consists of a class
o that contains a method named main
o that contains the statements (commands) to be
executed
14
Structure of Java programs (Cont.)
• Each Java instruction must be followed by a semi-colon!
General format
Instruction1;
Instruction2;
Instruction3;
Examples
: :
int num = 0;
System.out.println(num);
: :
15
Understanding first java program
• Let's see what is the meaning of class, public, static,
void, main, String[], System.out.println().
o class keyword is used to declare a class in java. (We will
learn more about keyword later.)
Class names begin with a Capital letter.
o public keyword is an access modifier which represents
visibility, it means it is visible to all.
o static is a keyword, if we declare any method as static, it
is known as static method.
The core advantage of static method is that there is no need
to create object to invoke the static method.
The main method is executed by the JVM, so it doesn't
require to create object to invoke the main method.
16
So it saves memory.
Understanding first java program (Cont.)
• void is the return type of the method, it means it doesn't
return any value.
• main represents startup of the program. There must be a
main() method.
• String[] args is used for command line argument. (We
will learn it later.)
• System.out.println() is used print statement.
17
Main method
• Every Java application program must include the main()
method.
• This is the starting point for the interpreter to begin the
execution of the program.
• A Java application can have any number of classes but
only one of them must include a main method to initiate
the execution.
18
Java IDE
• A Java IDE (for Integrated Development Environment) is
a software application which enables users to more
easily write and debug Java programs.
• Many IDEs provide features like syntax highlighting and
code completion, which help the user to code more
easily.
• Eg:
NetBeans, Eclipse, IntellJ IDEA, JCreator, Jdeveloper,
BlueJ, jEdit
19
Documentation / Comments
• Multi-line comment
/* section comment
section comment */
Eg:
/* I don’t know how I wrote this next part; I was working
really late one night and it just sort of appeared.*/
• Comment for a single line
// line comment
Eg:
int vices = 7; // are there really only 7 vices?
20
Documentation / Comments (Cont.)
• Eg:
21
Content • First Java program
• What are keywords?
• Java escape sequences
What are Keywords?
• Keyword is a word that is reserved by a program
because the word has a special meaning an purpose in
any computer language.
• Keywords can be commands or parameters (values) .
• Every programming language has a set of keywords
that cannot be used as variable names.
• Keywords are sometimes called reserved names.
• Java keywords are all lower case.
23
What are Keywords? (Cont.)
• Eg: 51 keywords in Java
* goto and const are no longer used in the Java programming
language
24
What are Keywords? (Cont.)
Exercise:
Mark all the keywords in the following java program.
class Extra
{
int x=100;
public static void main (String args[])
{
System.out.println(“Hi”);
}
}
25
Content • First Java program
• What are keywords?
• Java escape sequences
Java Escape Sequences
Use of \n
• We use \n for enter one row when we display out put
in Java (take a new line).
• Eg: Create a program to display your name and
address in below format
Name: Your name
Age: Your Age
Address: Your address
27
Java Escape Sequences (Cont.)
class Extra
Answer {
public static void main(String args[])
{
System.out.println("Name: Your name\nAge: Your
Age\nAddress: Your address");
}
}
class Extra
{
public static void main(String args[])
{
System.out.println("Name: Your name");
System.out.println("Age: Your Age");
System.out.println("Address: Your address");
}
}
28
Java Escape Sequences (Cont.)
Use of \t
• We use \t for keep tabs when we display out put in
java.
• Eg: Crete a program to display details in below format.
Name Marks
Anil 20
Sunil 60
Nimal 70
29
Java Escape Sequences (Cont.)
Answer
class Extra2
{
public static void main(String args[])
{
System.out.println("Name\tMarks\nAnil\t20\nSunil\t
60\nNimal\t70");
}
}
30
Java Escape Sequences (Cont.)
More…
31
Class Activity
32
Exercise 01
• Write a program that produce a following output.
33
Exercise 02
• Write a program that produce a following output.
34
Exercise 03
• What println statements will generate the following
output?
35
Exercise 04
• Write a program that draws the
following figures one above
the other.
36
Summary
• First java program
o Java commands for compile and run
o Simple program of Java
o Dos commands
o Structure of Java programs
o Understanding first Java program
o Main method
o Java IDE
o Documentation/comments
• What are keywords?
• Java escape sequences
• Class activity
37
Questions?
End.
38