Lecture 1 Notes
Introduction to Java:
Java is a general purpose, object oriented, high level programming language developed
by Sun Microsystems (now part of Oracle).
It follows the WORA (Write Once, Run Anywhere) principle, meaning the same program
can run on multiple platforms without modification.
History of Java
1. Creators:
Java was initiated in June 1991 by a small group of engineers at Sun Microsystems
called the Green Team.
James Gosling, Mike Sheridan, and Patrick Naughton led the project.
2. Early Development:
● Initially designed for small, embedded systems in electronic appliances (e.g., set
top boxes).
● Java was first called Greentalk, and files had the '.gt' extension.
● Later, it was named Oak as part of the Green Project.
3. Why the Name "Oak"?
● The name Oak symbolized strength.
● It is also the national tree of several countries, including the USA, France,
Germany, and Romania.
4. From Oak to Java:
● In 1995, the language was renamed Java because the name Oak was already
trademarked by Oak Technologies.
● The team brainstormed several names like dynamic, Silk, jolt, DNA, but "Java"
stood out for being unique, cool, easy to say, and reflecting the technology's
lively nature.
5. Origin of the Name "Java":
● Java is an island in Indonesia known for producing the first coffee beans, called
Java coffee.
● James Gosling came up with the name while drinking coffee near his office.
Key Milestones in Java
1. First Release:
Java 1.0 (JDK 1.0) was released on January 23, 1996.
This version established Java as a powerful language for web and desktop applications.
2. Recognition: In 1995, Time Magazine listed Java as one of the Ten Best Products of
the year.
3. Evolution of Java:
Over the years, Java has evolved with new versions adding features and improvements.
Today, Java is widely used in:
Windows applications
Web applications (e.g., Spring, Hibernate)
Enterprise systems (e.g., banking software)
Mobile apps (especially Android)
Smart cards and IoT systems
Key Features:
1. Object Oriented: Supports concepts like classes, objects, inheritance, polymorphism,
and encapsulation.
2. Platform Independent: Runs on any platform via JVM.
3. Robust: Includes automatic memory management (garbage collection) and strong
exception handling mechanisms.
4. Secure: Provides a secure runtime environment with bytecode verification and a security
manager.
5. Multithreaded: Java supports multiple threads to handle several tasks simultaneously,
enhancing performance.
Intro to JDK, JRE, and JVM :
JDK (Java Development Kit):
● A toolkit for writing, compiling, and debugging Java programs.
● Includes the compiler (javac), debugger, libraries, and JRE.
● Essential for developers who want to build and test Java applications.
● Example tools: javac (compiles Java code), javadoc (creates documentation), and jar
(packages classes into JAR files).
JRE (Java Runtime Environment):
Provides the runtime environment, including libraries and the JVM, to run Java applications.
It does not include tools like the compiler. Useful for end users who only need to execute Java
programs.
JVM (Java Virtual Machine):
The JVM interprets bytecode generated by the compiler and executes it.
It provides memory management, garbage collection, and security features.
JVMs are platform specific; each OS has its own JVM implementation.
Java Environment Setup in VS Code
1. Install the JDK:
Download the latest JDK from https://www.oracle.com/java/technologies/downloads/ or
https://jdk.java.net/ .
During installation, set JAVA_HOME environment variable to the JDK path.
2. Install VS Code:
● Download and install Visual Studio Code from https://code.visualstudio.com/
● Go to Extensions (Ctrl+Shift+X) and install the Java Extension Pack. This
includes support for IntelliSense, debugging, and Maven/Gradle integration.
3. Verify Setup:
Open Command Prompt/Terminal and run:
a. java -version
b. javac -version
If installed correctly, these commands will display the installed Java version.
4. Create and Run Java Program in VS Code:
● Open VS Code, create a new folder, and open it in the editor.
● Create a new file named HelloWorld.java.
Write your first Java program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save the file and open the integrated terminal in VS Code.
Run the following commands in terminal to execute the code:
javac HelloWorld.java // Compiles the code
java HelloWorld // Runs the program
If everything is correct, it will print: Hello, World!
The Structure of a Basic Java Program :
// Package declaration (optional)
package com.example;
// Import statements (optional)
import java.util.Scanner;
// Class Declaration
public class HelloWorld {
// Main method: Entry point of the program
public static void main(String[] args) {
// Statements go here
System.out.println("Hello, World!");
}
}
1. Package Declaration: Declares the package (optional). Packages group related classes.
2. Import Statements: Import builtin or user defined classes (optional).
3. Class Declaration: All Java code is written inside classes. The class name should match
the filename.
4. Main Method:
Every Java program starts from the main() method:
public static void main(String[] args) { ... }
public: Accessible everywhere.
static: Belongs to the class, not an instance.
void: Returns no value.
String[] args: Command Line arguments.
5. Statements and Indentation in Java
Statements: A statement is a single instruction or action. Every statement ends with a
semicolon (;).
Example:
int x = 5;
System.out.println(x);
Indentation:
● Java encourages consistent indentation (4 spaces per level is standard).
● Good indentation makes the code readable but does not affect the program’s
execution.
System.out.print() and System.out.println():
System.out.print() and System.out.println() are used to display output on the console.
Difference:
System.out.print(): Prints text without a newline, meaning the next output will appear on the
same line.
System.out.print("Hello ");
System.out.print("World!");
// Output: Hello World!
System.out.println(): Prints text with a newline. After printing, the cursor moves to the next line.
System.out.println("Hello");
System.out.println("World!");
// Output:
// Hello
// World!
Escape Sequences:
Escape sequences allow special characters to be used in strings.
\n: Newline – moves the cursor to the next line.
System.out.println("Hello\nWorld!");
// Output:
// Hello
// World!
\t: Tab – adds a horizontal tab space.
System.out.println("Java\tProgramming");
// Output: Java Programming
Main Function:
Every Java program starts execution from the main() function.
Syntax:
public static void main(String[] args) {
// Code to execute
}
public: Accessible from anywhere.
static: Belongs to the class, so it can be called without creating an object.
void: Does not return any value.
String[] args: Takes command line arguments (optional).
Comments in Java:
Comments are used to describe the code and make it more readable. They are ignored by the
compiler.
Types of Comments:
1. Singleline comment:
// This is a single line comment
System.out.println("Hello");
2. Multiline comment:
/*
This is a multiline comment
explaining the code.
*/
System.out.println("World!");
Introduction to Variables:
Variables store data that can be used and modified throughout the program.
Syntax:
dataType variableName = value;
Example:
int age = 25; // Stores an integer
double salary = 50000.50; // Stores a decimal value
String name = "Alice"; // Stores a string
Rules for naming variables:
● Must start with a letter, $, or _.
● Cannot use reserved keywords (like int, class).
● Can not have space between characters or words
● Can not start with digits
● Case Sensitive (e.g., age and Age are different).
Data Types:
Data types specify the type of data that a variable can hold. They determine the size and type of
values that can be stored in a variable.
Java data types are mainly classified into two categories:
1. Primitive Data Types:
These are the basic, built-in types provided by the Java language. They are not objects and
store actual values.
There are 8 primitive data types in Java:
Data Type Size Description Example
byte 1 byte Stores whole numbers from -128 to 127 byte a = 100;
short 2 bytes Whole numbers from -32,768 to 32,767 short s = 20000;
int 4 bytes Integer values int num = 500;
long 8 bytes Larger range of integers long big = 100000L;
float 4 bytes Decimal numbers (single precision) float f = 5.75f;
double 8 bytes Decimal numbers (double precision) double d = 19.99;
char 2 bytes A single character (Unicode) char c = 'A';
boolean 1 bit Only two values: true or false boolean flag = true;
Note:
● Primitive data types are faster and more memory-efficient.
● They do not support methods (e.g., int doesn't have methods like .length()).
2. Non-Primitive Data Types:
Also known as reference types. These refer to objects and store references (addresses) rather
than actual data.
Common Non-Primitive Types:
String:
● Used to store a sequence of characters.
● Strings are objects in Java and belong to the java.lang.String class.
● They have built-in methods (e.g., .length(), .toUpperCase()).
Example:
String name = "Alice";
System.out.println(name.length()); // Output: 5
Note: Strings are immutable, meaning their value cannot be changed once created.
Arrays:
● Used to store multiple values of the same data type.
● Arrays have fixed size.
● The index starts from 0.
Example:
int[] numbers = {10, 20, 30};
System.out.println(numbers[1]); // Output: 20
Note:
● You can create arrays of primitive or non-primitive types.
● Arrays are objects in Java.
Other Non-Primitive Types:
● Classes: User-defined types that can have fields and methods.
● Interfaces: Similar to classes but used for abstraction.
● Enums: A special class to define constants.
● Wrapper Classes: Convert primitives into objects (e.g., Integer, Double).
Comparison: Primitive vs Non-Primitive:
Feature Primitive Types Non-Primitive Types
Store actual values Yes No (they store references)
Memory efficiency High Lower
Support methods No Yes
Can be null No Yes
Examples int, char, float String, Array, Class