L1: Introduction to Java
History of Java
Java is a high-level programming language created by Sun Microsystems (now owned by Oracle) and first released in 1996. It was designed to be versatile and secure, quickly gaining popularity for its ability to run on various devices, from computers to electronics. Java avoids common memory and security issues found in other languages by automating many processes.
Basic Elements of Java Programs
Classes and Methods
- Java programs are built using classes, which act as blueprints containing the code structure.
- Within classes, methods (similar to functions) group statements that perform specific actions.
- Every Java program must have at least one class and one method. The entry point for a Java application is the
main
method:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
- File names must match the class name and end with the
.java
extension.
Basic Commands
System.out.println()
is used to print output to the terminal.- Proper indentation improves code readability.
- The terminal is an application built into the operating system, allowing users to interact with the computer using textual commands and output.
Comments
- Comments explain the code but are ignored during execution. Use
//
for single-line comments and/* ... */
for multi-line comments.
- Comments explain the code but are ignored during execution. Use
Other Key Points
- Statements are grouped together using methods, which must be enclosed in classes.
- Every Java program must have at least one class and one method.
- The
main
method is the entry point of an executable Java program. - Java follows CamelCase for class names (e.g.,
HelloWorld
) and does not allow spaces. public static void main(String[] args)
is automatically executed when the program runs.
Executing Java Programs
Computers can only execute machine code. To run a Java program, the source code (written in .java
files) must be translated into machine code. This is done using Java’s compiler and interpreter.
Compiled Language vs. Interpreted Language
Compiler
- Translates source code into machine code before execution, producing a
.class
file containing bytecode. - Advantage: Faster execution since the translation happens once.
- Translates source code into machine code before execution, producing a
Interpreter
- Translates code line-by-line during execution, offering flexibility but generally slower performance.
- Java combines both: it compiles code into bytecode, which the Java Virtual Machine (JVM) interprets, allowing for platform independence.
Java’s Hybrid Approach
- Java’s compiler generates an intermediate form of code called bytecode, stored in
.class
files. - The JVM acts as the interpreter, converting bytecode into machine code specific to the platform it’s running on. This “write once, run anywhere” capability is one of Java’s key features.
The Java Development Kit (JDK)
To compile and run Java programs, use:
javac Filename.java
to compile the source code into bytecode.java Filename
to run the compiled code with the JVM.