Day1 Notes
Day1 Notes
a. What is Java?
Java data types are divided into Primitive Data Types and Non-Primitive Data Types.
Primitive data types are predefined by the language and stored directly in memory. They are
lightweight and simple.
int is commonly used for integers unless memory optimization is required.
Use float for decimal numbers when memory is a concern, but double is more precise.
char holds a single character enclosed in single quotes (e.g., 'A').
boolean is used for true/false values in conditional statements.
Non-primitive (or reference) data types are created by the programmer and include objects,
arrays, and strings.
Variables:
A variable is a container that holds data that can be changed during program execution. In Java,
variables have a type, which determines what kind of data they can hold.
a. Local Variables
Declared inside a method, constructor, or block.
Only accessible within the block where they are defined.
Must be initialized before use.
b. Instance Variables
c. Static Variables
Reads the user's input from the console as a String and stores it in the variable name.
nextLine() is a method in the Scanner class that reads an entire line of input, including
spaces.
scanner.close(); :
This is a good programming practice, although not strictly necessary for small programs.
Introduction to Arrays in Java
An array is a collection of elements, all of the same data type, stored in contiguous memory
locations. Arrays are used to store multiple values in a single variable instead of declaring
separate variables for each value.
Features of Arrays:
1. Fixed Size: The size of an array is fixed at the time of its declaration.
2. Indexed: Elements are accessed using indices, starting from 0 to n-1 (where n is the size of the
array).
3. Homogeneous Elements: All elements in an array must be of the same data type.
4. Random Access: You can access any element directly using its index.
Syntax:
OR