Unit-2 (Data)
Unit-2 (Data)
In Java, data types are used to define the type of data that a variable can hold. Java is a statically typed language,
which means that you need to declare the data type of a variable before using it. Java has two categories of data
types: primitive data types and reference data types.
Integral Types:
Floating-Point Types:
Character Type:
Boolean Type:
In Java, primitive data types are passed by value, meaning the actual value is passed. For reference data types, the
reference to the memory location is passed.
Understanding and selecting appropriate data types is essential for efficient memory usage and maintaining
program correctness.
These keywords form the foundation of Java programming, and a solid understanding of their usage is crucial for
writing effective and maintainable Java code.
Java Identifiers:
An identifier is a name given to a variable, method, class, or other program elements. Identifiers must follow certain
rules:
1. Must begin with a letter (a to z or A to Z), underscore (_), or dollar sign ($).
2. Subsequent characters can be letters, digits (0 to 9), underscores, or dollar signs.
3. Java is case-sensitive, so uppercase and lowercase letters are distinct.
4. Cannot be a reserved word or keyword.
int myVariable;
String firstName;
void calculateTotalAmount();
Java Constants:
Constants are variables whose values do not change during the execution of a program. In Java, constants are
typically declared using the final keyword. This keyword indicates that the variable cannot be reassigned once a
value is assigned to it.
Here, PI , MAX_SIZE , and GREETING are constants, and their values cannot be modified. It is a common convention to
name constants in uppercase letters with underscores separating words for better readability.
Using constants is beneficial for code maintainability and understanding because it allows you to give meaningful
names to values that are used multiple times in the code. If you need to change the value, you only need to update
it in one place.
It's worth noting that while the final keyword is commonly used to create constants, it is not the only way. Enums
and interfaces with constant fields can also be used to define constants in Java.
Example:
final double PI = 3.14159;
final int MAX_SIZE = 100;
// Using constants in code
double circleArea = PI * radius * radius;
Java Variable:
A variable in Java is a named storage location that holds a value of a specific data type. Variables allow you to store
and manipulate data within a program. The value of a variable can change during the execution of the program. In
Java, variables must be declared with a specific data type before they can be used.
Variable declaration is the process of specifying the type and name of a variable. The general syntax for declaring a
variable in Java is:
Here, data_type represents the type of the variable (e.g., int, double , String), and variable_name is the name given to the
variable. For example:
javaCopy code
int age; double salary; String name;
javaCopy code
int count = 0 ; double pi = 3.14 ; String greeting = "Hello, Java!" ;
The scope of a variable refers to the region of the program where the variable can be used or accessed. In Java, the
scope of a variable is determined by where it is declared:
1. Local Variables:
• Declared inside a method, constructor, or a block of code.
• Exist only within the block of code where they are declared.
• Not accessible outside the block.
2. Instance Variables (Non-Static Fields):
• Declared within a class, outside any method or block.
• Exist as part of the instance of the class.
• Each instance of the class has its own copy of the instance variable.
3. Class Variables (Static Fields):
• Declared with the static keyword within a class, outside any method or block.
• Shared among all instances of the class.
• There is only one copy of the class variable shared by all instances.
4. Method Parameters:
• Variables passed into a method as arguments.
• Have scope within the method.
• Exist only during the execution of the method.
Understanding variable scope is crucial for writing correct and maintainable code. It helps prevent naming conflicts
and ensures that variables are used in the appropriate context.
In this example, PI, MAX_SIZE , and GREETING are symbolic constants. They are declared with the final keyword,
indicating that their values cannot be changed. These constants are then used in the main method to perform
calculations and output messages.
Using symbolic constants instead of literal values directly in your code has several advantages:
1. Readability: Constants with meaningful names make the code more readable and self-explanatory.
2. Maintainability: If the value of a constant needs to be changed, you only need to update it in one place.
3. Prevention of Magic Numbers: Magic numbers (hard-coded numerical values) can be replaced with named
constants, making the code more understandable.
By convention, names of symbolic constants are written in uppercase letters with underscores separating words. This
helps distinguish them from regular variables and makes their purpose clear.