Java Week2a
Java Week2a
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.
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.
boolean A data type that can hold True and False values only
char A data type that can hold unsigned 16-bit Unicode characters
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
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
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.