JAVA BASICS :
variables and Data Types
Introduction to Variables
What is a Variable?: A variable is a container for
storing data values, declared with a data type
defining the kind of data it can hold.
Introduction to Variables
What is a Variable?: A variable is a container for
storing data values, declared with a data type
defining the kind of data it can hold.
Declaring Variables: Specify the data type followed
by the variable name. Example: int myNumber;
Introduction to Variables
What is a Variable?: A variable is a container for
storing data values, declared with a data type
defining the kind of data it can hold.
Declaring Variables: Specify the data type followed
by the variable name. Example: int myNumber;
Initializing Variables: Assign a value at declaration
or later. Example: int myNumber = 10; and
myNumber = 20;
Variable Naming
Importance: Clear and consistent variable names
make code easier to read and understand.
Variable Naming
Importance: Clear and consistent variable names
make code easier to read and understand.
Rules:
1. Start with a letter, $, or _.
2. Casesensitive.
3. No spaces or special characters.
4. No Java keywords.
Variable Naming
Importance: Clear and consistent variable names
make code easier to read and understand.
Rules:
1. Start with a letter, $, or _.
2. Casesensitive.
3. No spaces or special characters.
4. No Java keywords.
Best Practices:
1. Be descriptive.
2. Use camelCase.
3. Keep it short and readable.
Variable Naming
Importance: Clear and consistent variable names
make code easier to read and understand.
Rules:
1. Start with a letter, $, or _.
2. Casesensitive.
3. No spaces or special characters.
4. No Java keywords.
Best Practices:
1. Be descriptive.
2. Use camelCase.
3. Keep it short and readable.
Common Mistakes:
1. Starting with a number.
2. Using spaces.
3. Using special characters (except $ and _).
Data Types
What are Data Types?: Data types classify different types
of information and define the operations that can be
performed on that data. 3
Data Types
What are Data Types?: Data types classify different types
of information and define the operations that can be
performed on that data. 3
Primitive Data Types: Basic data types predefined by Java.
Examples:
- byte: ‘byte age = 30;’
- short: ‘short year = 2024;’
- int: ‘int salary = 50000;’
- long: ‘long population = 7800000000L;’
- float: ‘float temperature = 36.6f;’
- double: ‘double pi = 3.141592653589793;’
- char: ‘char initial = ’A’;’
- boolean: ‘boolean isJavaFun = true;’
Data Types
What are Data Types?: Data types classify different types of
information and define the operations that can be
performed on that data.
3
Primitive Data Types: Basic data types predefined by Java.
Examples:
- byte: ‘byte age = 30;’
- short: ‘short year = 2024;’
- int: ‘int salary = 50000;’
- long: ‘long population = 7800000000L;’
- float: ‘float temperature = 36.6f;’
- double: ‘double pi = 3.141592653589793;’
- char: ‘char initial = ’A’;’
- boolean: ‘boolean isJavaFun = true;’
Reference Data Types: Used to reference objects. Examples:
- Classes
- Interfaces
- Arrays
- String: ‘String s = “Hello”;’
Type Casting
Introduction to Type Casting: Type casting
involves converting one data type to another.
Type Casting
Introduction to Type Casting: Type casting
involves converting one data type to another.
Widening Conversion (Implicit Casting): It is
automatic with no data loss when assigning a
smaller to a larger type.
Type Casting
Introduction to Type Casting: Type casting
involves converting one data type to another.
Widening Conversion (Implicit Casting): It is
automatic with no data loss when assigning a
smaller to a larger type.
Narrowing Conversion (Explicit Casting): It
requires explicit casting and may result in data
loss when assigning a larger to a smaller type.