๐น Compiler and Interpreter in Java
When we write a Java program, it first needs to be converted into a format that the computer can understand. This is done in two steps:
1. Compiler (javac)
- The Java compiler translates the source code (files with .java extension) into an intermediate format called bytecode (files with .class extension).
- Compilation happens before the program runs.
2. Interpreter (JVM โ Java Virtual Machine)
- The JVM reads the bytecode and translates it into machine-specific instructions line by line.
- This happens at runtime.
- JVM also uses Just-In-Time (JIT) Compiler which improves performance by converting frequently used bytecode into machine code.
๐ This way, Java combines the advantages of compilation and interpretation.
๐น Features of Java
1: Simple
- Easy to learn and use.
- Clean syntax (similar to C/C++ but easier).
- Avoids complex features like pointers and operator overloading.
2: Object-Oriented
- Everything is based on objects and classes.
- Supports inheritance, encapsulation, polymorphism, and abstraction.
- Example: Student, Car, BankAccount.
3: Platform Independent
- "Write once, run anywhere."
- Programs are compiled into bytecode which runs on any system with JVM.
4: Secure
- No direct memory access.
- Strong error handling and bytecode verification.
5: Robust
- Automatic garbage collection.
- Strong memory management.
6: Portable
- Java code runs on any system without modification.
7: Multithreaded
- Supports multiple tasks running at the same time.
8: Distributed
- Supports distributed applications using RMI and other technologies.
9: High Performance
- Faster than traditional interpreted languages due to JIT compiler.
10: Dynamic
- Can load classes and objects at runtime.
- Supports runtime polymorphism.
๐น Identifiers in Java
Identifiers are the names we give to classes, variables, methods, and packages.
โ Rules for Identifiers:
- Can contain: letters (AโZ, aโz), digits (0โ9), underscore (_), dollar sign (\$).
- Cannot start with a digit.
- No special symbols allowed (@, #, space, etc.).
- Case-sensitive (Age and age are different).
- Cannot use reserved keywords (class, int, etc.).
- No length limit, but should be meaningful.
โ Example:
java
class Student { // "Student" is a class identifier
int age; // "age" is a variable identifier
void study() { // "study" is a method identifier
System.out.println("Studying...");
}
}
๐น Declaration and Initialization
Declaration
Reserving memory for a variable.
java
int age; // declaring integer variable
double salary; // declaring double variable
String name; // declaring string variable
Initialization
Assigning a value to a variable.
java
age = 25;
salary = 50000.75;
name = "Manoj";
Declaration + Initialization Together
java
int age = 25;
double salary = 50000.75;
String name = "Manoj";
- base class Employee should contain fields name , employeeId and a method calculate salary() which returns the salary of the employee . the sub class Manager should inherit from Employee. the sub class developer should inherit from employee . . in main() creat objats of manager and seveloper, and display their salaries.
PROGRAM::
class Employee {
String name;
int EmployeeId;
double calculateSalary()
{
return 0;
}
}
class Manager extends Employee {
double calculateSalary() {
return 30000; // fixed salary
}
}
class Developer extends Employee {
double calculateSalary() {
return 25000; // fixed salary
}
}
public class Company {
public static void main(String[] args) {
Manager m = new Manager();
m.name = "Manoj";
m.EmployeeId = 101;
System.out.println("Manager " +m.name + " " + "EmployeeId: " + m.EmployeeId + " "+ "Salary: " + " " + m.calculateSalary());
Developer d = new Developer();
d.name = "kiruba";
d.EmployeeId = 102;
System.out.println("Developer " + d.name + " " + "EmployeeId: " + d.EmployeeId + " "+ "Salary: " + d.calculateSalary());
}
}
OUTPUT::
Manager Manoj EmployeeId: 101 Salary: 30000.0
Developer kiruba EmployeeId: 102 Salary: 25000.0
Top comments (0)