Lecture 1C
CS F213
Object Oriented Programming
10/01/24
BITS, Pilani Goa campus
Prof. Anita Agrawal
Introduction
to Java
Topics:
▪ Anatomy of a Java Program
▪ History of Java
▪ Salient Features of Java
▪ Stages of Execution of Java Program
▪ JVM vs. JRE vs. JDK
2 1/11/2024 2:44 PM Anita Agrawal
3
Java Program
▪ A Java program is defined as a collection of objects that
communicate via invoking each other’s methods.
1/11/2024 2:44 PM Anita Agrawal
4
Anatomy of A Java Program
A. Reserved words / Keywords
B. Classes
C. Package
D. Modifiers
E. Statements
F. Blocks
G. Methods
H. The main method
I. Comments
1/11/2024 2:44 PM Anita Agrawal
5
My first program
1/11/2024 2:44 PM Anita Agrawal
6
Breaking down the program
▪ “class" keyword is used to declare a new class.
▪ “Myfirstjavaprogram" is the identifier
▪ public static void main
▪ public:
» It is an example of an access specifier.
» Allows programmer to control the visibility of class
members
» Members may be accessed from outside the class in which
they are declared.
1/11/2024 2:44 PM Anita Agrawal
7
Breaking down the program
(Contd.)
main() be declared as public since it will be called
by outside it’s class
1/11/2024 2:44 PM Anita Agrawal
Breaking down the program (Contd.)
▪ static:
» Allows main() to be called without creating an object of the
class.
» It is necessary since main() is called by JVM before any
objects are made.
▪ void:
» Defines the return type, in this case it is void.
8 1/11/2024 2:44 PM Anita Agrawal
main
» The system locates and runs the main method for a class
when you run a program.
» Other methods get executed when called by the main
method.
9 1/11/2024 2:44 PM Anita Agrawal
10
▪ String args[ ]
– Declares a variable (object) named args
– It is an array of String class instances
– args[ ] receives any command line arguments during
program execution
1/11/2024 2:44 PM Anita Agrawal
11
System.out.println
▪ println: It is a method
▪ It is used by invoking a statement with a string argument.
▪ The string argument is enclosed within parentheses. In this
case, the argument is "This is my first java program!"
▪ You can call the same println method with a different
argument to print a different message.
1/11/2024 2:44 PM Anita Agrawal
12
Blocks
▪ A pair of braces in a program form a block that groups
components of a program.
1/11/2024 2:44 PM Anita Agrawal
13
Comments
//my first java program
class myfirstjavaprogram
{
public static void main(String args[])
{
System.out.println("this is my first java
program");
}
}
// This is a single line comment
/* These are multiple
line comments */
1/11/2024 2:44 PM Anita Agrawal
14
Lexical constraints
▪ Whitespace – Java is a free-form language (indentation is not
necessary but desirable).
» As long as there is one whitespace character between each token –
Space, tab, and newline.
▪ Identifiers – Used for class, method, and variable names.
▪ Literals – A constant value in Java is called literal
▪ Comments – /* */ and //
Anita Agrawal
Identifiers
▪ Combination of uppercase and lowercase letters,
and numbers
▪ Underscore_ and $ sign are allowed.
▪ Must begin with an alphabet, underscore or $
▪ Case-sensitive
▪ 3-15 characters
▪ Reserved keywords cannot be used
15 1/11/2024 2:44 PM Anita Agrawal
16
List of Separators
1/11/2024 2:44 PM Anita Agrawal
17
Reserved words/ Keywords in Java
1/11/2024 2:44 PM Anita Agrawal
18
Important points
▪ The name of the source file is very important.
▪ In previous example, the name of the file will be
myfirstjavaprogram.java
▪ The name of the class should match the name of file that holds
the program.
▪ A source file is officially called compilation unit.
1/11/2024 2:44 PM Anita Agrawal
19
▪ It is a text file that contains one or more class definitions.
▪ For Java compiler, source file must have .java extension.
▪ In Java, all code must reside inside a class
▪ Java is a case-sensitive programming language.
1/11/2024 2:44 PM Anita Agrawal
Next …………Steps of execution
20 1/11/2024 2:44 PM Anita Agrawal
THANKS!
Any questions?
21 1/11/2024 2:44 PM Anita Agrawal