Variables in Java
Objectives
- Distinguish between primitive and reference variables
- Declare and use primitive variables
- Declare, create, and use reference variables
- Know sizes of primitive types
- Understand automatic promotion of primitive variables
- Understand type casting of primitive variables
Declaring a Variable
- Before using any variable in a Java program, you must declare that variable.
- Syntax:
type name1, name2, ..., nameN;
- Examples:
double sum, avg; int i, j; String s;
Assigning a Value to Variable
- Storing a value in a variable is called assigning or assignment.
- Syntax:
name = value;
- Examples:
double price; String s; price = 12.95; s = "Amount:";
Initializing a Variable
- It is possible to both declare and assign a value to a variable on a single line.
- This is called initializing or initialization.
- Syntax:
type name = value;
- Examples:
double price = 12.95; int i = 0, j = 2; String s = "Amount due:";
Primitive or Reference
- Variables may be categorized by their type as either primitive or reference.
- Primitive variables store only one value at a time and have no methods. There are eight primitive variable types in Java:
boolean
char
byte
short
int
long
float
double
- Reference variables refer to arrays or objects which may store many values at once and may have methods. All variable types that are not primitive types are reference types, such as:
Object
String
StringBuilder
Integer
Long
Float
TreeSet
Primitive Types
Type | Purpose | Size (bytes) | Examples |
---|---|---|---|
boolean | Store true or false | 1 | boolean b = true; |
char | Store a single unicode character | 2 | char c = 'A'; |
byte | Store a number within this range: [−27, 27) = [−128, 128) = [−128, 127] | 1 | byte b = 12; |
short | Store a number within this range: [−215, 215) = [−32,768, 32,768) = [−32,768, 32,767] | 2 | short s = -25000; |
int | Store a number within this range: [−231, 231) = [−2,147,483,648, 2,147,483,647] | 4 | int i = 4013; |
long | Store a number within this range: [−263, 263) = [−9,223,372,036,854,775,808, 9,223,372,036,854,775,807] | 8 | long n = 3; |
float | A floating-point number is a number with digits after
the decimal separator.
Store a floating-point number with approximately
7 decimal digits of precision within the following range:
[−3.4028235×1038, 3.4028235×1038]
float can store exact integers within the
following range without approximation:
[−16,777,216, 16,777,216]Integers that are more negative or more positive than the
previous range are approximated in a float . |
4 | float f = 4.15f; |
double | More precise than
float . Store a
floating-point number with approximately 15 decimal digits
of precision within the following range:
[−1.7976931348623157×10308, 1.7976931348623157×10308]double can store exact integers within the
following range without approximation:
[−9,007,199,254,740,992, 9,007,199,254,740,992]Integers that are more negative or more positive than the
previous range are approximated in a double . |
8 | double d = 76.178032; |
Automatic Promotion of Primitive Variables
- Generally speaking, smaller primitive variable types can be promoted to larger variable types.
- boolean cannot be promoted to any other primitive type.
- Possible promotions:
char ↓ byte → short → int → long → float → double - Example:
int i = 7; float f = i; // Automatic promotion from int to float.
Type Casting Primitive Variables
- Larger variable types cannot be promoted to smaller variable types.
- Converting from larger to smaller is a “demotion” and may result in lost data (possible loss of precision).
- The programmer must explicitly write a type cast to cause the computer to convert from a larger variable to a smaller one.
- Examples:
double d = 6.7; int i = d; // Wrong! Results in a compile error. double d = 6.7; int i = (int)d; // Type cast from double to int. // After the type cast and assignment, // d will store the value 6.7 and // i will store the value 6