Skip to content

Information Technology · Ch 2 — Introduction to Programming

Programming Fundamentals — Data Types, Variables and Constants

2

Programming Fundamentals — Data Types, Variables and Constants

Programs work with data — numbers, characters and true/false values. To store and use data, Java needs to know what kind of value it is dealing with, so every piece of data has a data type.

Primitive data types

Java's built-in (primitive) types are the basic building blocks:

TypeStoresExample value
intwhole numbers2025, -7
longvery large whole numbers9000000000L
floatdecimal numbers (single precision)3.14f
doubledecimal numbers (double precision)3.14159
chara single character in single quotes'A', '9'
booleana truth valuetrue, false

Text made of many characters uses the type String (written in double quotes, e.g. "Odisha").

Variables

A variable is a named box in memory that holds a value which can change while the program runs. You must declare a variable (state its type and name) before using it, and you may initialise it (give it a first value):

int marks;              // declaration
marks = 85;             // assignment
double price = 249.50;  // declaration + initialisation together
char grade = 'A';
boolean passed = true;
String name = "Rina";
``` …
Definition 1Data type

A classification that tells the computer what kind of value is being stored (whole number, decimal, character, truth value, text) and h …

Definition 2Variable

A named memory location whose stored value can change during program execution; it must be declared with …

Definition 3Constant (final)

A named value that cannot be changed after it is set, declared with the ke …

Definition 4Literal

A fixed value written directly in the code, such as 85, 3.14, 'A' o …

Definition 5Type casting

Converting a value from one data type to another; widening is automatic, narrowing must be written explic …