2.2 Variable Types
Java is a strongly typed language, which means you must specify in advance what kind of data you will store. The type of variable determines how the value is stored and interpreted depending on the kind of data.
1. Kinds of Values and Data Types
The values mainly dealt with in programming can be divided into 'characters' and 'numbers', and numbers are further divided into 'integers' and 'real numbers (floating-point)'.
graph LR
A[Kinds of Values] --> B[Characters]
A --> C[Numbers]
B --> D['A', '1', "ABC"]
C --> E[Integers (123, -4)]
C --> F[Real Numbers (3.14, 0.0)]
Primitive Types vs Reference Types
Java's data types are broadly classified into 8 primitive types and all other types being reference types.
- Primitive Type: Stores the actual value (Data) natively (e.g.,
int,double). - Reference Type: Stores the literal memory address of the object where a certain value is stored continuously (e.g.,
String).
💡 Glimpsing the String Class
Among the 8 basic primitive types, only the char type uniquely exists to store precisely a single solitary letter. However, in actual practical operations, effectively handling long consecutive sentences—specifically Strings—is infinitely more common. Although strictly speaking String natively is ** NOT**a built-in primitive type in Java, but rather a robust 'Class (Reference Type)', it is actively utilized so universally that the compiler intentionally fundamentally treats it flawlessly with special privileges, casually allowing direct literal value assignment identically to true primitive types.
String name = "Java"; // Utilize robust double-quotes ("") strictly to reliably store full strings
String version = "17";
// Using the fundamental addition operator (+) essentially effortlessly concatenates raw strings seamlessly seamlessly with various other variables.
System.out.println(name + " " + version); // Gracefully outputs: Java 17
One of the characteristics of Java is that although it is an object-oriented language, it maximizes execution performance by treating primitive types specially.
2. 8 Primitive Types
There are a total of 8 primitive types, grouped into 4 categories: logical, character, integer, and floating-point types.
- Logical(boolean): Takes only two values,
trueandfalse, and is used in conditional expressions. - Character(char): Used to store exactly one character.
- Integer(byte, short, int, long): Mainly stores integers, and
intis the default data type. - Floating-point(float, double): Represents real numbers, and
doubleis the representative data type.
Size of Storage Space Based on Data Type
This indicates how much storage space is occupied when a value is allocated in memory according to the size of the data type.
- 1 byte:
boolean,byte - 2 bytes:
char,short - 4 bytes:
int,float - 8 bytes:
long,double
3. Literals
In programming, a literal refers to a fixed value itself. That is, a literal means the value itself. We often call it a 'Constant', but since constants in programming are distinguishable from this, we refer to it specifically as a literal.
int year = 2024; // 2024 is a literal
final int MAX_NUM = 100; // 100 is a literal, MAX_NUM is a constant
Proactively Declaring Constants
Categorically speaking, fundamentally, a rigid variable that is structurally explicitly mandated to absolutely never change natively after dynamically storing its initial value exactly once until the entire program officially identically terminates is firmly termed a 'Constant'. It is aggressively established dynamically by fundamentally attaching the unyielding final keyword reliably in exact front of the designated variable type, and typically, universally following an implicit ironclad development convention, it is rigorously named identically utilizing exclusively Capitalized uppercase letters natively.
final double PI = 3.14159;
// PI = 3.14; // Fatal Error! The dedicated constant's explicitly secured value can never fundamentally be forcefully altered.
Universal Numeric Underscore (_) Notation Pattern
Historically starting confidently precisely from Java version 7, universally when raw numeric figures are inconveniently exceptionally long and terribly tedious to manually successfully read visually, modern developers are completely empowered to inherently elegantly insert distinct underscores (_) effortlessly effectively exactly in the middle of numerical constructs drastically to universally enhance raw code graphical readability cleanly.
long myBankBalance = 1_000_000_000L; // Dynamically structurally represents precisely exactly 1 Billion robustly
// Upon explicit program output execution natively, purely the genuine numerical constructs accurately rigorously structurally completely excluding the inserted visual underscores are processed normally seamlessly.