Unit 1
Unit 1
SY. B.Tech
History of Java
Developers: Java was developed by James Gosling, Mike Sheridan, and Patrick
Naughton.
Start Date: The project began in 1991 and was originally called "The Green
Project".
History of Java
• The language was initially named Oak, after an oak tree that stood outside
James Gosling's office.
• Later, it was renamed Java after the developers realized that the name
"Oak" was already trademarked. The name "Java" was inspired by the
coffee that the developers drank during the development process.
History of Java
Year Event
The "Green Project" was initiated at Sun Release of Java 8, introducing streams,
1991 2014
Microsystems. lambdas, and other key features.
Java 9 introduced the modular system
1995Java 1.0 was officially released. 2017
(Project Jigsaw).
Java was formally standardized under the Java
1997 Oracle moved Java to a 6-month release cycle
Community Process (JCP). 2018
for quicker updates.
Release of Java 2 (J2SE 1.2), which introduced
1998 Java 17 became the next Long-Term Support
significant enhancements. 2021
(LTS) version.
Release of Java 5 (J2SE 5.0), adding generics,
2004
annotations, and more.
Oracle acquired Sun Microsystems, taking over Java
2010
development.
PIMPRI CHINCHWAD UNIVERSITY
5
History of Java
1. Java 1.0 (1995):
o First official release.
o Introduced the write-once-run-anywhere (WORA) principle.
o Included the first version of the JVM.
2. Java 2 (1998):
o Introduced Swing for graphical user interfaces (GUIs).
o Split into three editions:
▪ J2SE (Java 2 Standard Edition)
▪ J2EE (Java 2 Enterprise Edition)
▪ J2ME (Java 2 Micro Edition)
PIMPRI CHINCHWAD UNIVERSITY
5
History of Java
1. Java 5 (2004):
o Significant update with generics, enhanced for-loop, autoboxing/unboxing, and annotations.
o Introduced the enum type.
2. Java 8 (2014):
o Introduced lambda expressions, streams API, and a new date-time API.
o Revolutionized functional programming in Java.
3. Java 9 (2017):
o Introduced the modular system (Project Jigsaw) for better dependency management.
4. Java 17 (2021):
o Long-Term Support (LTS) version.
Comments in Java
• Single-line comments: // This is a single-line comment
• Multi-line comments:
• /* This is a
• multi-line comment */
Java Buzzwords
• Simple: Easy to learn and implement.
• Object-Oriented: Based on classes and objects.
• Secure: Prevents unauthorized access via bytecode verifier and Security Manager.
• Portable: Write Once, Run Anywhere (WORA) with JVM.
• Robust: Strong memory management, exception handling, and garbage collection.
• Multithreaded: Supports concurrent programming.
• High Performance: Enabled via Just-In-Time (JIT) compiler.
• Architectural Neutral: Java's bytecode is not specific to any processor or operating system, making it
platform-independent.
• Distributed Applications: Facilitates the development of distributed systems using tools like Remote
Method Invocation (RMI) and support for networked applications via APIs.
OOP's Concepts
• Object
• Class
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
https://www.javatpoint.com/difference-between-jdk-jre-and-jvm
JDK
JVM
12
Keywords
In Java, keywords are reserved words that have a predefined meaning and cannot be used as
identifiers (such as variable names, class names, or method names).
These words are part of the Java language syntax and are used to define the structure and
behavior of a Java program.
Keywords
Naming Conventions
• Name should not match with keywords
• Class:
• Begin with capital letter (Single Word)
• Every first letter must be capital (Multiple Words)
• Method:
• Begin with lower case letter (Single Word)
• Every first letter of other word must be capital (Multiple Words)
• Variable:
• Begin with alphabet or underscore
• Should not begin with a dight
• It may contain alphanumeric letters but special symbols are not allowed except
underscore
Data Types
Non-primitive types (or reference types) include objects, arrays, and classes. They do not store the value directly
but rather a reference to the memory location.
Examples of Non-Primitive Data Types
1. Strings (e.g., String name = "Java";)
2. Arrays (e.g., int[] numbers = {1, 2, 3};)
3. Classes (e.g., MyClass obj = new MyClass();)
Interfaces (e.g., Runnable task = new MyRunnable();)
// Non-Primitive Types
int[] marks = {90, 85, 88};
String name = "Alice";
// Display values
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Grade: " + grade);
System.out.println("Eligibility: " + isEligible);
System.out.println("First Mark: " + marks[0]);
}
} PIMPRI CHINCHWAD UNIVERSITY
20
Constants
• A final variable can only be assigned once, either at the time of declaration or in the
constructor (if it's a class field).
• Use UPPERCASE letters with underscores for separating words in the constant name.
• Declare constants as static final in classes if they are shared across multiple instances.
Variables
• Declaration: dataType variableName = value;
• Types:
• Instance variables: Defined outside methods; specific to an object.
• Static variables: Defined with static keyword; shared across all objects.
• Local variables: Declared inside a method, constructor, or block; exist
during method execution.
Variables - Example
class Example {
int instanceVar;
static int staticVar;
void method() {
int localVar = 10;
}
}
Input / Output
• IO Stream
Is associated with java.lang.*; package. This package is imported by default
in java code.
• System.in: Read data from keyboard
• System.out: Display data on screen
• System.err: Display error message
• Output methods
• System.out.print(): Print everything on a single line
• System.out.println(): Prints statements on next line
• System.out.printf(): Similar to C language function, used for formatted
output
Scanner Class
• The Scanner class in Java is part of the java.util package and is used to take
input from various input sources like keyboard input, files, strings, etc. It
provides methods to parse primitive types (e.g., int, float, boolean) and
strings from the input source.
Scanner Class
Create a Scanner Object:
Scanner scanner = new Scanner(System.in);
Operators
• Arithmetic: +, -, *, /, %
• Relational: ==, !=, <, >, <=, >=
• Logical: &&, ||, !
• Bitwise: &, |, ^, ~, <<, >>
• Assignment: =, +=, -=, etc.
• Example:
int a = 100;
double b = a; // int to double
System.out.println(b); // Output: 100.0
Enum
• An enum is a special "class" that represents a group
of constants (unchangeable variables, like final variables).
• To create an enum, use the enum keyword (instead of class or interface), and
separate the constants with a comma. Note that they should be in uppercase
letters:
Example:
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Features of Enums
Enum : Example 1
1. Accessing Enum Values
You can access enum values using the dot (.) operator.
Day today = Day.MONDAY;
System.out.println(today); // Output: MONDAY
Enum : Example 1
• enum Level {
• LOW,
• MEDIUM,
• HIGH
• }
Enum : Example 2
• enum Level {
• LOW,
• MEDIUM,
• HIGH
• }
Enum : Example 2
• public class Main {
• switch(myVar) {
• case LOW:
• System.out.println("Low level");
• break;
• case MEDIUM:
• System.out.println("Medium level");
• break;
• case HIGH:
• System.out.println("High level");
• break;
• }
• }
• }
Limitations of Enums
1. Cannot extend other classes (because enums implicitly extend java.lang.Enum).
Cannot create an instance of an enum using the new keyword.
32
Control Flow
• Block Scope: Variables defined inside {} are not accessible outside.
• Example:
public class BlockScopeExample {
public static void main(String[] args) {
{
int x = 10; // x is accessible only within this block
System.out.println("Value of x: " + x); // Output: 10
}
// System.out.println(x); // Error: x cannot be resolved to a variable
}
}
Conditional Statements
• if-else : Executes a block of code based on whether a condition is true or false.
• Example:
public class IfElseExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
}
}
Conditional Statements
• switch-case : Allows selection from multiple options based on the value of an expression.
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday"); // Output: Wednesday
break;
default:
System.out.println("Invalid day");
}
}
}
Loops
• for : Executes a block of code a specific number of times.
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
}
}
Loops
• while : Repeats a block of code as long as the condition is true.
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
}
}
Loops
• do-while : Executes a block of code at least once and then repeats as long
as the condition is true.
public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Count: " + i);
i++;
} while (i <= 5);
}
}
PIMPRI CHINCHWAD UNIVERSITY
38
Arrays
• Arrays in Java: An array is a collection of elements of the same type. It is
used to store multiple values in a single variable.
• Steps to Use Arrays
🞄 Declare the array:
dataType[] arrayName;
• Initialize the array:
arrayName = new dataType[size];
🞄 OR
dataType[] arrayName = {value1, value2, value3};
• Access elements using their index:
• Array indexing starts from 0.
Standalone Programs
1. Write a program to print "Welcome to Java Programming".
2. Write a program to add two numbers entered by the user.
3. Write a program to calculate the difference, product, and division of two numbers.
4. Write a program to find the square of a number.
5. Write a program to check if a number is even or odd.
6. Write a program to check if a number is positive, negative, or zero.
7. Write a program to find the largest of three numbers entered by the user.
8. Write a program to print the multiplication table of a given number.
9. Write a program to calculate the factorial of a number.
PIMPRI CHINCHWAD UNIVERSITY
43
Standalone Programs
10. Write a program to print all even numbers from 1 to 100.
11. Write a program to check if a given string is a palindrome.
12. Write a program to count the number of vowels in a given string.
13. Write a program to reverse the digits of a number.
14. Write a program to check if a given number is a palindrome.
15. Write a program to find the sum of digits of a number.
16. Write a program to print the Fibonacci series up to n terms.
17. Write a program to calculate the sum of the first n natural numbers.
18. Write a program to check if a given number is prime.
PIMPRI CHINCHWAD UNIVERSITY