Data & Variables
A Basic Java Program w/ Variables
The 6 Data Types
In Java Programming, there are many datatypes we can work with. We will be analyzing 6 different datatypes.
Integer : int
int
Integer helps us to represent integer values in our program.
The int
data type can store whole numbers from -2147483648 to 2147483647. We can use these values to do arithmetic operations.
Decimals : float
float
The float
data type allows us to represent decimal based numbers. The values must end with a letter f
. Example: float pi = 3.14f;
Longer Decimals : double
double
The double
data type also allow us to represent decimal based numbers. The big difference between double
and float
is that a double
variable is able to contain a larger value than float
.
Text : String
String
The String
data type is used to store a sequence of characters (text). String values must be surrounded by double quotes.
Single Character : char
char
The char
data type is used to store a single character. The character must be surrounded by single quotes, like 'A'
or 'c'
.
True & False Values : boolean
boolean
For this, Java has a boolean data type, which can only take the values true
or false
.
FUN FACT: boolean was named after a mathematician named George Boole
Variables
Variables are labeled containers that helps to hold data in programming. The variables have labels so that we can reference them in our code. We can also update variables, create variables, and manipulate variables to help our code have functionality.
Variable Formatting
All variables must have an explicit datatype stated, given a label name, and assigned a value. It must also have a semi-colon to end the line.
Variable Naming Conventions
start with lowercase letters
labels that have multiple words are either
camelCased
or useunder_scores
labels cannot be the same as the built-in keywords in Java
labels should not be ambiguous
String a = "Park"
is less descriptive thanString last_name = "Park"
Data Type and Their Storage Size
int
32 bits
float
32 bits
double
64 bits
char
16 bits
string
depends on String object
boolean
1 bit
Constants
A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program. (Tutorialspoint)
Java does not support constants, but we can use two keywords: static
and final
to mimic such behaviour.
static
variables are created at compile, only one copy of them is available.final
variables cannot be modified after its creation
In basic programs we don't really have a need for these types of data.
SOURCES
w3schools -- https://www.w3schools.com/java/java_data_types.asp
Last updated