0% found this document useful (0 votes)
24 views23 pages

Java Week2a

Uploaded by

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

Java Week2a

Uploaded by

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

COURSE: INF811D

OBJECT ORIENTED PROGRAMMING - JAVA


Lecture II

Prepared by Linda Otoo


OUTLINE
• Data Types in Java
• Importance of Data types in Java
• Types of Data types in Java
• Overview of Identifiers in Java
• Buzz /Keywords in Java
• Variables in Java
• Rules governing the use of variables in Java
• Variable declarations
• Variable initializations
• Strings used as unique DataPrepared
Typesbyin Java
Linda Otoo
Data Types, Identifiers and Variables in Java
• Data Types
Data type is said to be a classification that orders the type of
value a variable can contain and the kind of operations that
can be performed on the variable without any compilation
error.
NB: This defines the storage space of the variable, informs
the compiler on how to interpret the variable or value of the
variable and specifies how the end-user can interact with the
variable.
• This is powerful programming construct assists the
compiler to distinguish between “abcd” and “1234”, etc.
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
• Types of Data Types in Java
• In Java Data types are of two main categories and they are the
Primitive Data types and Non-Primitive Data types which are
further grouped as numeric Data types and text Data types.
• Primitive Data types are predefined data types that convey
specific meaning to the Java compiler. Examples are int, short,
byte, long, float, double, char, and Boolean.
• Numeric Data Types are the short, byte, int, long, float, double,
and boolean.
• Text Data Types are char and String.
• Non-primitive data types are user defined data types and are
not pre-defined to the Java compiler. E.g. String.
• String is a special data type that we will be studied
Prepared in detail.
by Linda Otoo
Data Types, Identifiers and Variables in Java
Types of Data Types in Java
1. Byte
The byte data type is a primitive data type in Java that represents integer values. It is the smallest
integer data type in terms of storage size, occupying 8 bits (or 1 byte) of memory. It can hold integer
values ranging from -128 to 127, allowing for the representation of both positive and negative numbers
within a limited range. It is commonly used when working with small integers or when memory
efficiency is a concern.
2. Short
The short data type in Java is a primitive data type that represents integer values. It is larger than the
byte data type and smaller than the int data type in terms of storage size. It occupies 16 bits (or 2 bytes)
of memory and can hold integer values ranging from -32,768 to 32,767. It is commonly used when
working with integer values that fall within the range supported by this data type, and is often used in
scenarios where the memory consumption needs to be minimized.
3. Integers
The int data type is a primitive data type in Java that represents integer values. It occupies 32 bits (or 4
bytes) of memory and can hold integer values ranging from -2,147,483,648 to 2,147,483,647. It is the
default choice for working with whole numbers in Java unless there is a specific need for a larger or
smaller range of values. It is used in a wide range of applications and calculations that involve counting,
indexing, or representing quantities.
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
Types of Data Types in Java

4. Long
The long data type in Java is a primitive data type that represents long integer values. It occupies 64 bits (or 8
bytes) of memory and can hold integer values ranging from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807. It is useful in situations where you need to work with very large numbers, such as
when dealing with timestamps, large counts, or representing values with significant precision. To declare and
initialize a long variable, you can use the long keyword. Dear Student, kindly note that the Suffix “L” is
appended to the value assigned to the long variable to indicate that the value is a long. Otherwise the value
would be treated as an integer and if it exceeds the range of integer, a compilation error would occur.

5. Floating Point (Float)


The float data type in Java is a primitive data type that represents single-precision floating-point numbers. It
occupies 32 bits (or 4 bytes) of memory and can hold values ranging from 1.4 x 10-45 to 3.4 x 1038. To declare
and initialize a float variable, you can use the float keyword. It is important to be cautious when using float for
precise calculations, as it has limited precision and can introduce rounding errors. It is commonly used in
scenarios where memory efficiency is crucial, such as in graphics programming or when dealing with large
arrays of floating-point numbers. Kindly note again that the suffix “f” is appended to the value assigned to a
float value. Otherwise, the value would be treated as a double, and if it exceeds the range of float, a
compilation error would occur.
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
Types of Data Types in Java

6. Double
The double data type in Java is a primitive data type that represents double-precision floating-
point numbers. It occupies 64 bits (or 8 bytes) of memory and can hold floating-point values
ranging from approximately 4.9 x 10-324 to 1.8 x 10308. To declare and initialize a double
variable, you can use the double keyword. The double data type is widely used in various
applications, including scientific calculations, financial calculations, and any scenario where high
precision is required.
7. Boolean
The boolean data type is used in conditional statements to control the flow of the program based
on certain conditions. It can only hold two values: true or false, and cannot be assigned any other
value. It is a fundamental part of Java and plays a crucial role in controlling the flow and logic of
programs based on conditions and boolean expressions.

Prepared by Linda Otoo


Data Types, Identifiers and Variables in Java
Types of Data Types in Java
8. Char
The char data type is a primitive data type in Java that represents a single character. It occupies
16 bits (or 2 bytes) of memory and can hold Unicode characters. To declare and initialize a char
variable, you can use single quotes ('') around the character and escape sequences to represent
special characters. It is often used when dealing with text, parsing input, or performing character-
based operations. It also supports Unicode, making Java well-suited for internationalization and
localization purposes. The char data type is a fundamental part of Java and is essential when
working with individual characters or performing character-based operations.
9. Reference Data type (Non-primitive Data Type)
A reference data type (also known as a non-primitive data type) is a type that refers to objects
rather than directly storing their values. They include classes, interfaces, arrays, and strings.
Reference data types allow you to work with complex data structures and enable features such as
inheritance, polymorphism, and dynamic memory allocation. Understanding reference data types
is essential for building more advanced and flexible Java programs.
NB: Dear Student, kindly be informed that each one of these reference Data type will be
studied in details in our subsequent units and sessions.
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
Identifiers in Java
An Identifier is said to be a unique character symbol or name(s) given to an
entity such as a variable, function, array and object in computer programming
to uniquely identify it during the execution of the program.
NB: Any entity to be used in a Java programme must have a unique name just
as any object found in the world is with a name.

Any Entity to be used must first be declared.


An Identifier in Java is usually a name(s) made up of both letters and numbers
and either short or descriptive with either or the underscore (_) or $ as a first
character.
Even though identifiers are names given to entities in programming we just
don’t give any name.
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
Rules for declaring Identifiers
Examples of Valid Identifiers
Total_Sum
_total
Sum1
Length
AreaOfCircle
$perimeter
Examples of Invalid identifiers
5total
@total
Total\!
Area Of Circle
Float
*Paul
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
Keywords / Buzz words in Java
Keywords are reserved words with special meaning already known to Java
compiler and are always written in lower cases (small letters).
Keywords are also names already known to the compiler with a special
meaning and cannot be used by the user or programmer to identify any entity
in the programme.

Prepared by Linda Otoo


Data Types, Identifiers and Variables in Java
Keywords / Buzz words in Java
Keyword Usage
abstract Specifies that a class or method will be implemented later, in a subclass

assert Assert describes a predicate placed in a Java program to indicate that


the developer thinks that the predicate is always true at that place.

boolean A data type that can hold True and False values only

break A control statement for breaking out of loops.

byte A data type that can hold 8-bit data values


case Used in switch statements to mark blocks of text

catch Catches exceptions generated by try statements

char A data type that can hold unsigned 16-bit Unicode characters

class Declares a new class


continue Sends control back outside a loop
default Specifies the default block of code in a switch statement

do Starts a do-while loop


double A data type that can hold 64-bit floating-point numbers

else Indicates alternative branches in an if statement

enum A Java keyword is used to declare an enumerated type. Enumerations


extend the base class.

Prepared by Linda Otoo


Data Types, Identifiers and Variables in Java
Keywords / Buzz words in Java
extends Indicates that a class is derived from another class or interface

final Indicates that a variable holds a constant value or that a method will
not be overridden
finally Indicates a block of code in a try-catch structure that will always be
executed
float A data type that holds a 32-bit floating-point number

for Used to start a for loop


if Tests a true/false expression and branches accordingly

implements Specifies that a class implements an interface

import References other classes


instanceof Indicates whether an object is an instance of a specific class or
implements an interface
int A data type that can hold a 32-bit signed integer

interface Declares an interface


long A data type that holds a 64-bit integer
native Specifies that a method is implemented with native (platform-
specific) code
new Creates new objects
null This indicates that a reference does not refer to anything

Prepared by Linda Otoo


Data Types, Identifiers and Variables in Java
Keywords / Buzz words in Java
package Declares a Java package
private An access specifier indicating that a method or variable may be
accessed only in the class it’s declared in

protected An access specifier indicating that a method or variable may only be


accessed in the class it’s declared in (or a subclass of the class it’s
declared in or other classes in the same package)

public An access specifier used for classes, interfaces, methods, and variables
indicating that an item is accessible throughout the application (or
where the class that defines it is accessible)

return Sends control and possibly a return value back from a called method

short A data type that can hold a 16-bit integer


static Indicates that a variable or method is a class method (rather than being
limited to one particular object)

strictfp A Java keyword is used to restrict the precision and rounding of


floating-point calculations to ensure portability.

super Refers to a class’s base class (used in a method or class constructor)

Prepared by Linda Otoo


Data Types, Identifiers and Variables in Java
Keywords / Buzz words in Java
switch A statement that executes code based on a test value

synchronized Specifies critical sections or methods in multithreaded code

this Refers to the current object in a method or constructor

throw Creates an exception


throws Indicates what exceptions may be thrown by a method

transient Specifies that a variable is not part of an object’s persistent


state
try Starts a block of code that will be tested for exceptions

void Specifies that a method does not have a return value

volatile This indicates that a variable may change asynchronously

while Starts a while loop


sealed The sealed keyword is used to declare a class as “sealed,”
meaning it restricts which classes can extend it.

permits The permits keyword is used within a sealed class declaration


to specify the subclasses that are permitted to extend it.

Prepared by Linda Otoo


Data Types, Identifiers and Variables in Java
Variables in Java
A variable is said to be a named storage space or container in memory used
to store value(s).
It is an identified storage space in memory used to store value(s).
This storage space again determines the kind of operations that can be
performed on the value stored in the variable.
Variables are declared with a specific data type and this determines
the size of the variable, the range of values that can be stored in the named
storage and the set of operations that can be performed on the named
storage space.
A variable in computer programming stores a value and this value
can be manipulated during execution of the programme.
NB: All variables to be used in computer programming must be declared first
before it can be used.
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
Variables in Java
Variable Declaration Syntax

Data_Type VariableName;
i.e Data type Space followed by the variable name and the terminator ;
Examples are:
int age;
float cgpa;
char grade;
String Name;
In case you want do declare different variables with the same Data type and different identifier,
you can declare them on the same single line with the use of comma(s) to separate the
identifiers. Egs.
int age, numberOfstudents, numberOfcourses;
you can also declare them on different lines such as
int age;
Prepared by Linda Otoo
int numberOfstudents;
int numberOfcourses;
Data Types, Identifiers and Variables in Java
Variables in Java
The following are examples of declaring variables of various types.
byte myByte;
short myShort;
int myInt;
fong myLong;
float myFloat;
double myDouble;
char myChar;
boolean myBoolean;
any variable declared in Java without any initial assignment of a value may
be assigned a default value based on the primitive data type used in
declaring the variable and in some cases undefined values.
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
Variables in Java
The following are examples of declaring variables of various types.
byte myByte;
short myShort;
int myInt; VARIABLE DEFAULT VALUE
Byte 0
fong myLong; Short 0
Int 0
float myFloat; Long 0L
Float 0.0f
double myDouble; Double 0.0d
Char ‘\u0000’ equivalent to null
char myChar; character
Boolean false
boolean myBoolean;
any variable declared in Java without any initial assignment of a value may be
assigned a default value based on the primitive data type used in declaring
the variable and in some cases undefined values.
Prepared by Linda Otoo
Data Types, Identifiers and Variables in Java
Variable Initialization
Initialization of a variable is the process of assigning an initial value to the
variable declared.
This can be done with the use of the assignment operator (=) to assign value
on the right side of the operator to the variable at the left side of the
operator.
NB: Any variable declared without initialization might be assigned with an
unknown value by the compiler and this might create a problem in our
programmes
There are two major types of variable initialization in Java with different
approaches of initializations. They are the Static initialization of variables and
the Dynamic initialization of variables.

Prepared by Linda Otoo


Data Types, Identifiers and Variables in Java
Static initialization of variables in Java
Declaring a variable and initialize it
e.g:
int age; // variable declaration
age = 19; // variable initialization
Declaring and initializing a variable
e.g:
int age = 19;
float height = 5.6;
3. Declaring multiple variables simultaneously and initializing them
separately
e.g:
float height, weight; // declaration of multiple variables
height = 5.6; // initialization of height
Prepared by Linda Otoo
weight = 75.2; // initialization of weight
Questions and Answers
Thank You!!!

You might also like