String
- stores text, such as “Hello”. String values are surrounded by double quotes
int
- stores integers (whole numbers), without decimals, such as 123 or -123
float
- stores floating point numbers, with decimals, such as 19.99 or -19.99
char
- stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
boolean
- stores values with two states: true or false
Declaring a variable
type variableName = value;
- Here, ‘=’ is used to assign values to the variables
Identifiers
- All Java variables must be identified with unique names
- Mostly can be short names but preferred descriptive names for understandability and to maintain code
[! Rules for variables]
Names can contain letters, digits, underscores, and dollar signs
Names must begin with a letter
Names should start with a lowercase letter and it cannot contain whitespace
Names can also begin with $ and _ (but we will not use it in this tutorial)
Names are case sensitive (“myVar” and “myvar” are different variables)
Reserved words (like Java keywords, such as
int
orboolean
) cannot be used as names