Programming in Java Laboratory (2)
Programming in Java Laboratory (2)
(LPCIT-109)
SUBMITTED IN PARTIAL FULFILLMENT OF THE REǪUIREMENTS FOR
THE AWARD OF THE DEGREE OF
BACHELOR OF TECHNOLOGY
(INFORMATION TECHNOLOGY)
Introduction to datatypes:
In Java, data types specify the type of data that can be stored in a variable. Java is a
statically-typed language, which means that all variables must be declared with a
data type. There are two main categories of data types in Java:
1. Primitive Data Types- Primitive data types are the most basic data types
available in Java. They are predefined by the language and named by a reserved
keyword. They represent single values and occupy a fixed amount of memory.
2. Non-Primitive Data Types- Non-primitive data types, also known as reference
data types, are types created by the programmer and are not defined by Java. They
refer to objects and hold memory addresses where the data is stored. These types
are more complex than primitive data types and can be used to store collections of
data.
PRIMITIVE NUMERIC DATATYPES:
1. byte:
The byte data type is an 8-bit signed two’s complement integer. The byte data type
is useful for saving memory in large arrays.
Syntax:
byte byteVar;
Size: 1 byte (8 bits)
CODE:
OUTPUT:
2. short
The short data type is a 16-bit signed two’s complement integer. Similar to byte,
use a short to save memory in large arrays, in situations where the memory savings
actually matters.
Syntax:
short shortVar;
Size: 2 bytes (16 bits)
CODE:
OUTPUT:
3. int
It is a 32-bit signed two’s complement integer.
Syntax:
int intVar;
Size: 4 bytes ( 32 bits )
CODE:
OUTPUT:
4. long
The long data type is a 64-bit two’s complement integer.
Syntax:
long longVar;
Size: 8 bytes ( 64 bits )
CODE:
OUTPUT:
5. float
The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float
(instead of double) if you need to save memory in large arrays of floating-point
numbers.
Syntax:
float floatVar;
Size: 4 bytes ( 32 bits )
CODE:
OUTPUT:
6. double
The double data type is a double-precision 64-bit IEEE 754 floating-point. For
decimal values, this data type is generally the default choice.
Syntax:
double doubleVar;
Size: 8 bytes ( 64 bits )
CODE:
OUTPUT:
PRIMITIVE NON-NUMERIC DATATYPES:
1. boolean
A boolean variable is defined using the keyword boolean.
Syntax:
boolean variableName;
Size: 1 bytes ( 8 bits )
CODE:
OUTPUT:
2. char
The char data type is a single 16-bit Unicode character with the size of 2 bytes (16
bits).
Syntax:
char charVar;
Size: 2 bytes ( 16 bits )
CODE:
OUTPUT: