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

Lecture 2

The lecture covers the history and fundamentals of Java programming, highlighting its creator, James Gosling, and its object-oriented nature. It discusses the Java Development Kit (JDK), Java Runtime Environment (JRE), and Integrated Development Environment (IDE) as essential tools for Java development. Additionally, it addresses lexical issues, data types, and the structure of a Java program, providing a comprehensive overview for students in the Object Oriented Programming course.

Uploaded by

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

Lecture 2

The lecture covers the history and fundamentals of Java programming, highlighting its creator, James Gosling, and its object-oriented nature. It discusses the Java Development Kit (JDK), Java Runtime Environment (JRE), and Integrated Development Environment (IDE) as essential tools for Java development. Additionally, it addresses lexical issues, data types, and the structure of a Java program, providing a comprehensive overview for students in the Object Oriented Programming course.

Uploaded by

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

Lecture: 2 (Java Programming)

Course

Object Oriented
Programming (OOP)

Instructor

Mr. Sarmad Shafique


Lecturer
Department of Computer Science
National University of Modern Languages
History of Java

2
History of Java
➢ James Gosling is a famous
Canadian software developer
who has been with Sun
Microsystems since 1984 and
is considered as father of
Java programming language.
➢ Gosling did the original
design of Java and
implemented its original
compiler and virtual
machine.

3
History of Java

4
What is Java?
General Purpose and Powerful Object-Oriented Language
programming language.
Based on C/C++
Used for developing software that run on mobile, desktop and
servers.
Machine Independent (Write Once Run Anywhere (WORA))
Java is known for its platform independence, robustness, and
wide adoption in various domains, including web development,
mobile app development, enterprise software, and more.

5
Editions of Java
Java Standard Edition (SE):
Develop applications that run on desktop.
Java Enterprise Edition (EE):
Develop server-side applications.
Java Micro Edition (ME):
Develop applications for mobile edition.
We will use Java SE which is foundation of all other editions.

6
Java Programming Environment
The Java programming
environment encompasses a
set of tools, libraries, and
resources that developers
use to write, compile, test,
and run Java applications.
Here are the key
components of the Java
programming environment:

7
JDK (Java Development Kit)
Set of software tools and libraries
that allow developers to create,
compile, and run Java applications.
Includes everything needed for
Java development
From writing code to testing and
deploying applications.
JDK provides a comprehensive
development environment for Java
programmers and is a fundamental
component for Java development.

8
JDK (Java Development Kit)
Here are the key components and functionalities of the JDK:
Java Compiler (javac):
The JDK includes the Java compiler (javac), which is responsible for translating human-
readable Java source code into bytecode.
Bytecode is a low-level, platform-independent representation of the code that can be executed
by the Java Virtual Machine (JVM).
Java Virtual Machine (JVM):
The JDK provides a JVM for running Java applications on a specific platform. Responsible for
interpreting bytecode, managing memory, and executing Java programs.
It ensures platform independence, allowing Java applications to run on any system with a
compatible JVM.
Java Standard Libraries:
Development Tools:
Header Files and Native Libraries:

9
JRE (Java Runtime Environment)
Provides the necessary runtime environment for executing Java
applications.
Contains a set of essential components, including the Java Virtual
Machine (JVM), libraries, and runtime utilities, that allow Java programs
to run on a specific platform.
Key components and functionalities of the JRE:
Java Virtual Machine (JVM):
Java Standard Libraries:
Java Executable:
Etc.

10
IDE (Integrated Development Environment)
A program that allow us to:
Write | Source Code
Compile | Machine Code
Debug | Tools to find error
Build | Files that can be executed by JVM
Run | Execute our program
Development is faster and easier
Popular Java IDEs:
NetBeans, Eclipse, etc.

11
Lexical Issues in Java
In Java, lexical issues refer to problems related to the
lexical structure or syntax of the programming language.
These issues are encountered during the initial stages of
compilation, where the source code is translated into
tokens or lexemes.
Lexical issues can prevent the code from being correctly
parsed and compiled.
We will see some common lexical issues in Java:
Whitespace, Identifiers, Literals, Comments, Separators, Java
Keywords

12
Lexical Issues: Whitespace
In Java, whitespace refers to characters that are used
for formatting and separating elements in your code.
Do not produce any visible output when the code is
executed.
Whitespace characters are essential for improving the
readability and organization of your Java code.
The main whitespace characters in Java include:
Spaces, Tabs, Line Breaks etc.

13
Lexical Issues: Identifiers
An identifier is a name given to a program element, such as a variable, method, class,
package, or label.
Identifiers serve as labels that help you identify and refer to various elements in your
Java code.
Identifiers play a crucial role in writing readable and maintainable Java programs.
Rules:
Identifiers can consist of letters, digits, underscores (_), and dollar signs ($). They must start with a
letter, underscore, or dollar sign. Java is case-sensitive, so uppercase and lowercase letters are distinct.
You cannot use reserved keywords as identifiers. Keywords have predefined meanings in Java and serve
specific purposes (e.g., class, public, int, if, etc.). Attempting to use a keyword as an identifier will result
in a compilation error.
Examples of Valid Identifiers:
myVariable, _count, $value, MyClass, calculateTotal
Examples of Invalid Identifiers:
2ndPlace (starts with a digit)
class (a reserved keyword)
my-variable (contains a hyphen, which is not allowed)
@name (contains an unsupported special character)
14
Lexical Issues: Identifiers (Continued)
Class Names: Start with an uppercase letter and use PascalCase (capitalizing each
word). For example, MyClass, PersonDetails.

Method Names: Start with a lowercase letter and use camelCase (capitalizing the first
letter of each subsequent word). For example, calculateTotal, getUserInfo.

Variable Names: Start with a lowercase letter and use camelCase. For example,
myVariable, itemCount.

Constants: Use uppercase letters with underscores to separate words. For example,
MAX_VALUE, PI.

15
Lexical Issues: Literals
In Java, literals are constants or fixed values that are directly represented in your
source code.
Java supports various types of literals, depending on the type of data they represent.

Left to right, the first literal specifies an integer, the next is a floating-point value, the
third is a character constant, and the last is a string.

16
Lexical Issues: Comments
In Java, comments are non-executable text within your source code that provide
explanations, or notes about the code.
Comments are not executed by the Java compiler or the Java Virtual Machine (JVM).
They are solely for human readers and developers to understand and document the
code.
Java supports three types of comments:
Single-Line Comments: Single-line comments begin with // and continue to the end of the line. These
comments are used for short explanations or annotations on a single line.

Multi-Line Comments: Multi-line comments are enclosed between /* and */.

17
Lexical Issues: Comments (Continued)
Documentation Comments:
Documentation comments are
special comments that start
with /** and ends with a */
are often used for generating
documentation using tools like
Javadoc. They can include
information about classes,
methods, fields, and their
usage.

18
Lexical Issues: Separators
In Java, separators are special characters or symbols used to delimit, separate, or
terminate various elements within the source code.
These separators are used to define the structure and syntax of Java programs.
Common separators in Java include:
Semicolon ( ; ): The semicolon is used to terminate statements in Java. Each Java statement must end with a
semicolon. It separates individual statements, allowing the compiler to distinguish the end of one statement
from the beginning of the next.
Comma ( , ): The comma is used to separate items in a list, such as method arguments, variable declarations,
or elements in an array. It is often used to group related items together.
Period ( . ): The period is used to access members (fields or methods) of a class or object. It is known as the
member access operator or dot operator.
Colon ( : ): The colon has various uses in Java, including in the for-each loop, the conditional (ternary)
operator, and the case label in switch statements.
Double Colon ( :: ): The double colon is used in Java for method references, a feature introduced in Java 8 as
part of functional programming.
Parentheses ( ( and ) ): Parentheses are used to enclose method arguments, control structures, and
expressions. They define the scope and order of operations in expressions.
Braces ( { and }) : Braces are used to define code blocks, such as method bodies, loops, and conditional
statements. They enclose a group of statements to create a single compound statement.
19
Lexical Issues: Java Keywords
There are 61 keywords currently defined in
the Java language.
These keywords, combined with the syntax
of the operators and separators, form the
foundation of the Java language.
Cannot be used as identifiers, meaning that
they cannot be used as names for a
variable, class, or method. (exceptions to
this rule are the context-sensitive keywords
added by JDK 9 to support modules.)
In JDK 9, an underscore by itself is
considered a keyword in order to prevent its
use as the name of something in your
program.

20
Data Types in Java
The Primitive Types:
Java defines eight primitive types of data:
byte, short, int, long, char, float, double, and boolean.
These can be put in four groups:
Integers: This group includes byte, short, int, and long, which are for whole-valued signed
numbers.
Floating-point numbers: This group includes float and double, which represent numbers with
fractional precision.
Characters: This group includes char, which represents symbols in a character set, like letters
and numbers.
Boolean: This group includes boolean, which is a special type for representing true/false values.

21
Data Types in Java
Reference Data Types:
Reference data types are used to store references (memory addresses) to
objects or instances of classes.
Don't directly hold the data but instead point to the location in memory
where the data is stored.
Common reference data types include:
• Classes: Objects of user-defined classes are reference data types.
• Interfaces: Interfaces are reference types and can be used to reference objects of
classes that implement those interfaces.
• Arrays: Arrays are reference data types that can hold multiple elements of the same
data type.

22
Structure of Java Program
Import Statements (if needed): At the beginning of your Java program, you can include import statements to bring in classes from other
packages or libraries that your program will use. These statements are not always required.

Class Definition: The program begins with the definition of a Java class. The class acts as a blueprint for objects, defining their attributes
and behavior.

Instance Variables (Attributes): Within the class, you can declare instance variables (also called attributes) that represent the state of
objects created from the class.

Constructors: Constructors are special methods with the same name as the class. They initialize the objects created from the class. You can
have multiple constructors, including overloaded constructors with different parameter lists. Constructors are called when you create an object
using the new keyword.

Methods (Functions): Inside the class, you can define methods that represent the behavior or actions that objects of this class can perform.
These methods can have parameters and return values.

Main Method: The main method is the entry point of the Java program. It's a special method that allows your program to be executed. It
takes an array of strings (args) as a parameter, which can be used to pass command-line arguments to the program.

23
Thank You
Any Questions?

24

You might also like