Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Department of Computer Sciences

CSC 402: Computer Programming III (OOP)

Adeniyi A.E.
2: Fundamentals of Java PL
2. Fundamentals of Java PL
•By the end of this topic, you will be able to:
i. understand the Java programming platform
ii.describe the fundamentals of Java
iii.develop programs using Java
2. Fundamentals of Java PL
•What is Java:
• It is a programming language and a computing platform
• Java is a general-purpose, multi-platform, object-oriented, and network-centric
programming language.

•Features of Java
• Easy-to-use PL
• Write once run many times i.e., platform-independent
• It is a multithreaded language
• It provides memory management capability – has garbage collector
• It is network-centric, provides distributed computing features
2. Fundamentals of Java PL
•History of Java:
• It was developed by James Gosling in Sun Microsystem

• It was initially called Oak

• Oak was design to program portable devices.

• The name was changed to Java in 1995 due to trademark issue and
modified it to take advantage of the emerging www.

• In 2009, Oracle took ownership of Java when it acquired Sun Microsystem


2. Fundamentals of Java PL
Java Versions Release Date
JDK Alpha and Beta 1995
JDK 1.0 23rd Jan 1996
JDK 1.1 19th Feb 1997
J2SE 1.2 8th Dec 1998
J2SE 1.3 8th May 2000
J2SE 1.4 6th Feb 2002 Lates version: Java SE 17
Release date: 14th Sep, 2021
J2SE 5.0 30th Sep 2004
Java SE 6 11th Dec 2006
Java SE 7 28th July 2011
Java SE 8 18th Mar 2014
Java SE 9 21st Sep 2017
Java SE 10 20th Mar 2018
JAVA SE 11 25th Sep 2018
JAVA SE 12 19th Mar 2019
JAVA SE 13 17th Sep 2019
JAVA SE 14 17th Mar 2020
JAVA SE 15 15th Sep 2020 (latest Java Version)
2. Fundamentals of Java PL
•Computing Platform:

• This implies that to run a program in HLL on different computing platforms,


different compilers are needed
2. Fundamentals of Java PL
•Java Platform:

• Thus, it is platform-independent –write once, run many times


2. Fundamentals of Java PL
•Java Platform:
• JDK – Java Development Kit:
• contains tools (compiler, debugger, application launcher, appletviewer, etc) for
writing Programs and JRE to execute them.
• Programmers use this to develop Java programs.

• JVM – Java Virtual Machine:


• Is part of the JRE that converts Java bytecode into machine code for execution
• Contains numerous libraries, tools and frameworks such as JIT compiler

• JRE – Java Runtime Environment


• Contains class libraries and the JVM
• Is the environment for simply running Java programs
• All machines/devices that want to run Java programs must have the JRE installed.
2. Fundamentals of Java PL
•Java Program development and execution:

Source Java Byte Machine


Code Compiler: Code code

.java jdk .class virtual


machine
2. Fundamentals of Java PL
•Character set:
• the set of characters recognized by the language.
• Java recognizes the following set of character sets.
Alphabets: Unicode system: Alphabets, digits and other
special characters in almost all human languages
Escape Sequence: multi-character starting with a backslash and
denote actions. E.g.:
\n implies newline
\b implies backspace
\t implies tab
2. Fundamentals of Java PL
•Variable
• A variable is an entity whose value can be changed in the cause of program execution
• It is serves as the name for memory location used for storing values
• In Java, variables must be declared before use
• Variable declaration involves:
• Data type, Name and optionally access modifier and initial value

• Example:
int x;
double y = 11.5;
private float z = 3.5f;

• Variables of the same data types and access modifier can be declared using a single line
statement. E.g.:
double p, q, r;
2. Fundamentals of Java PL
• Constant
• A constant is an entity whose value cannot be changed or altered during the course of
program execution
• In Java, a constant is declared using the keyword: final
• Example:
final double PI = 3.14159;

• Keywords/Reserved words:
• They are words with re-defined meaning and can only be used for the purposes for which they
are defined.

• Java keywords:
abstrtact, assert, Boolean, break, byte, case, catch, char, class, const,
continue, default, do, double, else, enum, extends, final, finally, float,
for, goto, if, implements, import, instanceof, int, interface, long, native,
new, package, private, protected, public, return, short, static, strictfp,
super, switch, synchronise, this, throw, throws, transient, try, void,
volatile, while
2. Fundamentals of Java PL
• Identifier:
• Identifiers are the symbolic names given to program entities
• Rules and conventions for specifying Identifiers in Java:
• Only alphanumeric characters ([a-z] [A-Z] [0-9]), $ and _ are allowed in
identifiers
• Must not start with number
• Must not contain whitespace
• They are case sensitive e.g.: total, Total, toTal, totaL are 4 different
identifiers
• Reserved keywords cannot be used as identifiers
• Example:
Valid identifiers: NomansLand90, $Wink1NTomBoy, Four_HorseMen$2021,
_AngryMonk404, final_result_value, Employee, EMP12
Invalid identifiers: wiseWizard#1994, 3000Buckets, @Employee,
Four HorseMen$2021, 36-StudentPro9,
final result value
2. Fundamentals of Java PL
• Identifier:
• Identifiers are the symbolic names given to program entities
• Conventions for specifying Identifiers in Java:
• Identifiers for constants should be in UPPER CASE
• Class names should be in Sentence case
• Identifiers with multiple words should use the camel case
• Objects and variables should be in lower case
2. Fundamentals of Java PL
• Data Types:

Class, Interface and


Enumeration
2. Fundamentals of Java PL
• Data Types:
Data Type Default Value Default size Range
boolean false 1 bit true and false
char '\u0000' 2 byte '\u0000' (or 0) to '\uffff' (or 65,535)
byte 0 1 byte -128 to 127
short 0 2 byte -32,768 to 32,767
int 0 4 byte - 2,147,483,648 (-2^31) to 2,147,483,647
(2^31 -1)
long 0L 8 byte -9,223,372,036,854,775,808(-2^63) to
9,223,372,036,854,775,807(2^63 -1)
float 0.0f 4 byte unlimited
double 0.0d 8 byte unlimited
2. Fundamentals of Java PL
• Literals
Data Types:
Default values

Integer literals • An integer number that can be assigned to integer variable.


Appending L or l to the number signifies the long data type
• 3 number systems: decimal (0-9), binary (0 and 1) and hexadecimal
(0-9, A-F)
• Prefixes 0x and 0b indicates hexadecimal and binary number
respectively

Float-point literal A floating-point literal is of type float if it ends with the letter F or f;


otherwise, its type is double, and it can optionally end with the
letter D or d.

Character and string Character literals are enclosed in single quotes


literals String literals are enclosed in double quotes
2. Fundamentals of Java PL
• Expression:
An Expression is a construct that consist of variables, constants,
method invocation and operators which can be evaluated to yield a
single value
• Arithmetic expression
• Assignment expression
• Logical expression
• Relational expression
2. Fundamentals of Java PL
• Operators:
Operator Type Category Operators
Unary postfix ++, -- (expr++ expr--)
prefix ++, -- (++expr --expr )
+, - (+expr -expr)
! (!expr)
Arithmetic multiplicative *, /, %
additive +, -
Shift shift <<, >>, >>>
Relational comparison <, >, <=, >=, instanceof
equality ==, !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND && -short circuits when the 1st operand is f
logical OR || -short circuits when the 1st operand is t
Ternary ternary ?:
Assignment assignment =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=,
>>>=
2. Fundamentals of Java PL
• Statement:
A statement is a program construct that cause execution and
changes values
In Java, it is simply an expression terminated with a semi colon (;)
Categories:
• Null statement: nothing terminated with ;
• Simple statement: a single expression terminated with ;
• Compound/Block statement: zero, one or more simple
statement enclosed in a pair of curly braces ({…})
• Control statement: determines the order of execution
2. Fundamentals of Java PL
• Comments:
• A comment is an explanatory note added to a program but is not
executed.
• Comments are useful for the following:
• To enhance code readability and thus, maintenance
• To prevent parts of code from executing especially during debugging
Java Comments:
• Single line comment: begins the comment line with //
• Multi line comment: comment spans through more than a line
and is enclose in /* and */
• Documentation comment: this is the comment used in
generating documentation API for Java packages, classes and
methods. It is enclosed in /** and */

You might also like