Information Technology · Ch 2 — Introduction to Programming
Programming Fundamentals — Data Types, Variables and Constants
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:
| Type | Stores | Example value |
|---|---|---|
int | whole numbers | 2025, -7 |
long | very large whole numbers | 9000000000L |
float | decimal numbers (single precision) | 3.14f |
double | decimal numbers (double precision) | 3.14159 |
char | a single character in single quotes | 'A', '9' |
boolean | a truth value | true, 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";
``` …
A classification that tells the computer what kind of value is being stored (whole number, decimal, character, truth value, text) and h …
A named memory location whose stored value can change during program execution; it must be declared with …
A named value that cannot be changed after it is set, declared with the ke …
A fixed value written directly in the code, such as 85, 3.14, 'A' o …
Converting a value from one data type to another; widening is automatic, narrowing must be written explic …