Corporate Training Study
Material
Java Data Types,
Variables, Keywords & Identifiers
Session Goals
By the end of this
session, participants will be able to:
1.
Understand Java Data Types
o
Differentiate between primitive and non-primitive
data types.
o
Know the memory allocation and usage
of each data type.
2.
Declare and Use Variables in Java
o
Understand variable declaration,
initialization, and scope.
o
Use instance, static, and local
variables effectively.
3.
Perform Type Conversion and Type
Casting
o
Identify the difference between implicit
and explicit type conversion.
o
Use casting to change data types
where needed.
4.
Understand Keywords in Java
o
Recognize reserved words and their
purpose in Java.
o
Avoid using keywords as identifiers.
5.
Learn Java Identifiers and Naming
Conventions
o
Identify valid and invalid
identifiers.
o
Follow best practices for naming
variables, methods, and classes.
6.
Write and Execute Java Programs Using
Variables and Data Types
o
Implement basic operations using Java
variables.
o
Use arithmetic operations, boolean
conditions, and string manipulations.
1. Introduction to Java
Data Types, Variables, Keywords & Identifiers
In Java, data types
define the type of data that variables can hold. Variables store data in
memory, while keywords and identifiers define Java syntax rules and naming
conventions.
Key Concepts Covered:
1.
Data Types (Primitive &
Non-Primitive)
2.
Variables & Variable Declaration
3.
Type Conversion & Type Casting
4.
Keywords & Identifiers
2. Data Types in Java
Java has two types of
data types:
1. Primitive Data Types
(8 types)
Primitive data types are
the most basic data types in Java that store simple values. Java has 8
primitive data types:
Data Type |
Size |
Default Value |
Description |
byte |
1 byte |
0 |
Stores whole numbers
(-128 to 127) |
short |
2 bytes |
0 |
Stores whole numbers
(-32,768 to 32,767) |
int |
4 bytes |
0 |
Stores whole numbers
(-2 billion to 2 billion) |
long |
8 bytes |
0L |
Stores large whole
numbers (-9 quintillion to 9 quintillion) |
float |
4 bytes |
0.0f |
Stores decimal numbers
with 7 precision digits |
double |
8 bytes |
0.0d |
Stores decimal numbers
with 15 precision digits |
char |
2 bytes |
'\u0000' |
Stores a single
character (Unicode) |
boolean |
1 bit |
false |
Stores true or false
values |
Example: Using Primitive
Data Types in Java
public class DataTypesExample {
public static void main(String[] args) {
int age = 25;
double salary = 50000.75;
char grade = 'A';
boolean isJavaFun = true;
System.out.println("Age: " +
age);
System.out.println("Salary: "
+ salary);
System.out.println("Grade: "
+ grade);
System.out.println("Is Java Fun?
" + isJavaFun);
}
}
Output:
Age: 25
Salary: 50000.75
Grade: A
Is Java Fun? true
2. Non-Primitive Data
Types
Non-primitive data types
store objects and reference types. These include:
Type |
Description |
String |
Stores a sequence of
characters |
Arrays |
Stores multiple values
of the same type |
Classes |
Defines a blueprint for
objects |
Interfaces |
Defines a contract for
classes |
Example: Using
Non-Primitive Data Types
public class NonPrimitiveExample {
public static void main(String[] args) {
String name = "John Doe";
int[] numbers = {10, 20, 30};
System.out.println("Name: " +
name);
System.out.println("First Number:
" + numbers[0]);
}
}
Output:
Name: John Doe
First Number: 10
3. Variables in Java
Variables are containers
that store data in memory.
Types of Variables:
1.
Local Variables
– Declared inside methods and accessible only within that method.
2.
Instance Variables
– Declared inside a class but outside a method.
3.
Static Variables
– Declared using the static keyword and shared across all instances of a class.
Example of Variable Types
public class VariableExample {
int instanceVar = 50; // Instance variable
static int staticVar = 100; // Static
variable
public void display() {
int localVar = 25; // Local variable
System.out.println("Local
Variable: " + localVar);
}
public static void main(String[] args) {
VariableExample obj = new VariableExample();
obj.display();
System.out.println("Instance
Variable: " + obj.instanceVar);
System.out.println("Static
Variable: " + staticVar);
}
}
Output:
Local Variable: 25
Instance Variable: 50
Static Variable: 100
4. Type Conversion &
Type Casting
Java supports automatic
and manual type conversions.
1. Implicit Type
Conversion (Widening)
Automatically converts
smaller data types to larger ones.
int num = 10;
double newNum = num; //
int → double (automatic)
System.out.println(newNum);
// Output: 10.0
2. Explicit Type
Conversion (Narrowing)
Manually converts larger
data types to smaller ones.
double pi = 3.14;
int newPi = (int) pi; //
double → int (manual)
System.out.println(newPi);
// Output: 3
5. Keywords in Java
Java has 50 reserved
keywords that have special meanings. Some common ones:
Keyword |
Description |
class |
Declares a class |
public |
Access specifier for
public access |
static |
Defines a static method
or variable |
void |
Specifies no return
value |
new |
Allocates memory for an
object |
this |
Refers to the current
instance of a class |
Example Using Keywords:
public class KeywordExample {
public static void main(String[] args) {
int number = 100;
System.out.println("Number: "
+ number);
}
}
6. Identifiers in Java
Identifiers are names
given to variables, methods, classes, etc.
Rules for Naming
Identifiers
✅ Must begin with a letter, _ (underscore),
or $
✅ Cannot be a keyword
✅ Case-sensitive (MyVar
and myVar are different)
✅ No spaces or special
characters
Valid Identifiers:
✔ myVariable, _counter, $totalSum
Invalid Identifiers:
❌ 1stVariable (Cannot start with a number)
❌ class (Reserved keyword)
❌ my variable (Spaces not
allowed)
7. Summary
✔ Java has primitive (byte, int, float,
char, etc.) and non-primitive (String, Array, etc.) data types.
✔ Variables store
values, and they can be local, instance, or static.
✔ Type conversion
can be implicit (automatic) or explicit (manual).
✔ Keywords are reserved
words in Java.
✔ Identifiers follow
naming rules and cannot be keywords.
8. Hands-On Assignment
Write a Java program
that:
- Declares
a variable of each primitive data type.
- Converts
int to double and double to int.
- Uses
keywords like public, static, and void.
- Creates
an identifier that follows Java naming rules.
public class Assignment {
public static void main(String[] args) {
int myInt = 100;
double myDouble = myInt; // Implicit
conversion
int newInt = (int) myDouble; //
Explicit conversion
System.out.println("Original
Integer: " + myInt);
System.out.println("Converted to
Double: " + myDouble);
System.out.println("Converted back
to Integer: " + newInt);
}
}
Expected Output:
Original Integer: 100
Converted to Double: 100.0
Converted back to Integer:
100
Conclusion
Assignment 1: Declare and
Print Different Data Types
Task:
- Declare
variables of all primitive data types in Java.
- Assign
values to them.
- Print
their values to the console.
Solution:
public class DataTypesExample {
public static void main(String[] args) {
// Primitive Data Types
byte byteVar = 120;
short shortVar = 3000;
int intVar = 25000;
long longVar = 9876543210L;
float floatVar = 3.14f;
double
doubleVar = 123.456;
char charVar = 'A';
boolean boolVar = true;
// Printing Values
System.out.println("Byte Value:
" + byteVar);
System.out.println("Short Value:
" + shortVar);
System.out.println("Integer Value:
" + intVar);
System.out.println("Long Value:
" + longVar);
System.out.println("Float Value:
" + floatVar);
System.out.println("Double Value:
" + doubleVar);
System.out.println("Character
Value: " + charVar);
System.out.println("Boolean Value:
" + boolVar);
}
}
Example Output:
Byte Value: 120
Short Value: 3000
Integer Value: 25000
Long Value: 9876543210
Float Value: 3.14
Double Value: 123.456
Character Value: A
Boolean Value: true
Assignment 2: Type
Conversion (Implicit and Explicit)
Task:
- Convert
an int to double (implicit conversion).
- Convert
a double to int (explicit conversion).
Solution:
public class TypeConversion {
public static void main(String[] args) {
int intValue = 50;
double doubleValue = intValue; //
Implicit Conversion
double anotherDouble = 99.99;
int anotherInt = (int) anotherDouble; //
Explicit Conversion
System.out.println("Integer Value: " + intValue);
System.out.println("Converted to
Double: " + doubleValue);
System.out.println("Double Value:
" + anotherDouble);
System.out.println("Converted to
Integer: " + anotherInt);
}
}
Example Output:
Integer Value: 50
Converted to Double: 50.0
Double Value: 99.99
Converted to Integer: 99
Assignment 3: Create and
Use Variables in a Java Program
Task:
- Create
a class Employee with instance variables.
- Assign
values and print them.
Solution:
class Employee {
String employeeName;
int employeeID;
double salary;
public Employee(String name, int id, double
sal) {
this.employeeName = name;
this.employeeID = id;
this.salary = sal;
}
public void displayDetails() {
System.out.println("Employee Name:
" + employeeName);
System.out.println("Employee ID:
" + employeeID);
System.out.println("Salary: "
+ salary);
}
public static void main(String[] args) {
Employee emp = new Employee("John
Doe", 101, 55000.75);
emp.displayDetails();
}
}
Example Output:
Employee Name: John Doe
Employee ID: 101
Salary: 55000.75
Assignment 4:
Understanding Local, Instance, and Static Variables
Task:
- Create
a class with local, instance, and static variables.
- Print
their values.
Solution:
public class VariableExample {
int instanceVar = 100; // Instance Variable
static int staticVar = 500; // Static
Variable
public void display() {
int localVar = 25; // Local Variable
System.out.println("Local
Variable: " + localVar);
System.out.println("Instance
Variable: " + instanceVar);
System.out.println("Static
Variable: " + staticVar);
}
public static void main(String[] args) {
VariableExample obj = new VariableExample();
obj.display();
}
}
Example Output:
Local Variable: 25
Instance Variable: 100
Static Variable: 500
Assignment 5: Identify
Valid and Invalid Identifiers
Task:
- List
valid and invalid identifiers.
- Try
compiling with invalid identifiers.
Solution:
public class IdentifierExample {
public static void main(String[] args) {
int myVar = 10; // Valid
int _count = 20; // Valid
int $total = 30; // Valid
int empID = 40; // Valid
int num1 = 50; // Valid
// int 1var = 60; // Invalid (Cannot start with number)
// int class = 70; // Invalid (class is
a keyword)
// int my variable = 80; // Invalid
(Cannot have space)
// int @data = 90; // Invalid (Cannot
contain special characters)
// int void = 100; // Invalid (void is
a keyword)
System.out.println("Valid identifiers used successfully!");
}
}
Example Output:
Valid Identifiers: myVar,
_count, $total, empID, num1
Invalid Identifiers: 1var,
class, my variable, @data, void
Assignment 6: Using Java
Keywords in a Program
Task:
- Write
a program using at least 5 Java keywords.
Solution:
public class KeywordExample {
public static void main(String[] args) {
final int number = 10; // final keyword
System.out.println("The number is:
" + number);
}
}
Example Output:
Assignment 7: Arithmetic
Operations Using Variables
Task:
- Perform
addition, subtraction, multiplication, division, and modulus using
variables.
Solution:
public class ArithmeticOperations {
public static void main(String[] args) {
int num1 = 20, num2 = 10;
System.out.println("Addition:
" + (num1 + num2));
System.out.println("Subtraction:
" + (num1 - num2));
System.out.println("Multiplication:
" + (num1 * num2));
System.out.println("Division:
" + (num1 / num2));
System.out.println("Modulus:
" + (num1 % num2));
}
}
Example Output:
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 5
Modulus: 0
Assignment 8: String
Manipulation Using Non-Primitive Data Type
Task:
- Perform
concatenation, find length, and convert to uppercase.
Solution:
public class StringManipulation {
public static void main(String[] args) {
String str = "Java
Programming";
System.out.println("Original
String: " + str);
System.out.println("Concatenated
String: " + str + " is fun!");
System.out.println("String Length:
" + str.length());
System.out.println("Uppercase
String: " + str.toUpperCase());
}
}
Example Output:
Original String: Java Programming
Concatenated String: Java
Programming is fun!
String Length: 18
Uppercase String: JAVA
PROGRAMMING
Assignment 9: Boolean
Data Type and Conditional Statement
Task:
- Use
boolean variable in an if-else statement.
Solution:
public class BooleanExample
{
public static void main(String[] args) {
boolean isJavaFun = true;
if (isJavaFun) {
System.out.println("Java is
Fun!");
} else {
System.out.println("Java is
Boring!");
}
}
}
Example Output:
Java is Fun: true
Enjoy Learning Java!
Assignment 10: Create a
Simple Calculator Using Variables
Task:
- Take
two numbers as input.
- Perform
arithmetic operations and print results.
Solution:
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second
number: ");
int num2 = sc.nextInt();
System.out.println("Addition: " + (num1 + num2));
System.out.println("Subtraction:
" + (num1 - num2));
System.out.println("Multiplication:
" + (num1 * num2));
System.out.println("Division:
" + (num1 / num2));
sc.close();
}
}
Example Output:
Enter first number: 10
Enter second number: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Expected Outcomes
✅ Participants will be able to write
Java programs with proper variable declarations and data types.
✅ They will
understand how Java stores and processes data efficiently.
✅ They will follow coding
standards and best practices for identifiers and naming conventions.