Notes on Variables and Data Types in Java
Introduction
Programs contain instructions and data.
A variable is a way to access and store data.
Data can be of different types such as integer, string, or floating point.
Data Types
Int: Integer type.
String: Sequence of characters.
Float: Floating point number.
Importance of Data Types
Specify the range of data.
Determine the memory allocation for the variable.
In Java, data types must be specified before using a variable (statically typed).
Statically Typed vs Dynamically Typed
Statically Typed: Languages like Java, C, and C++ require variable declaration before use.
Dynamically Typed: Languages like Python determine the data type at runtime.
Variable Naming
Allowed characters: letters (a-z, A-Z), digits (0-9), underscore (_), and dollar sign ($).
Rules:
Cannot begin with a digit.
Cannot use keywords (e.g., else, do, for, while).
Conventions:
Camel Case: First word in lowercase, subsequent words start with an uppercase letter.
- Example:
nameOfPlayer
,ageOfPlayer
,currentYear
.
- Example:
Constants: Uppercase letters separated by underscores.
- Example:
MAX_AGE
,MAX_PLAYERS
.
- Example:
Types of Variables
Primitive Types: Basic types like int, char, float, double.
Non-Primitive Types: Objects of classes (e.g., String, Array).
Primitive Data Types
Boolean
Values:
true
,false
.Example variables:
isValid
,isMarried
,flag
.
Byte
Size: 8 bits.
Range: -128 to 127.
Example: Marks of a student.
Short
Size: 16 bits.
Range: -32,768 to 32,767.
Int
Size: 32 bits.
Range: -2^31 to 2^31-1.
Example: General integer values.
Long
Size: 64 bits.
Range: -2^63 to 2^63-1.
Example: Very large integer values.
Float
Size: 32 bits.
Used for decimal values.
Example:
weight
,price
.
Double
Size: 64 bits.
Used for precise decimal values.
Char
Size: 16 bits.
Range: 0 to 2^16-1.
Used for storing characters.
Example:
gender
,grade
.
public class Main {
public static void main(String[] args) {
boolean isValid = true;
byte marks = 90;
float pi = 3.14f;
float div = 15.0f / 4.0f;
long views = 10000000000L;
char gender = 'M';
}
}
Example Program
Interview Questions
What are primitive data types in Java?
- Primitive data types are the most basic data types built into the language, such as int, char, float, double, boolean, byte, short, and long.
How do statically typed languages differ from dynamically typed languages?
- Statically typed languages require the data type of a variable to be declared before use, while dynamically typed languages determine the data type at runtime.
What is the significance of memory allocation in variable declaration in Java?
- Memory allocation determines how much space is allocated for the variable's data, which affects performance and resource usage.
Explain camel case naming convention with an example.
- Camel case involves starting with a lowercase letter for the first word and capitalizing the first letter of each subsequent word. Example:
currentYear
.
- Camel case involves starting with a lowercase letter for the first word and capitalizing the first letter of each subsequent word. Example:
What are the rules for naming variables in Java?
- Variable names can contain letters, digits, underscores, and dollar signs but cannot start with a digit or use keywords.
Give an example of a constant variable declaration in Java.
- Example:
final int MAX_AGE = 200;
. Constants are written in uppercase letters separated by underscores.
- Example:
What is the range of values for a byte data type in Java?
- The range is from -128 to 127.
Why are char data types 16 bits in Java?
- Java uses 16-bit characters to support Unicode, allowing representation of characters from various languages.