Core Java Training
Core Java Training
• Install JDK -> Choose the installation directory (default is recommended) and click
"Install."
• Set Environment Variables -> Go to your system settings and search for
"Environment Variables." -> Under "System Variables," find Path and click "Edit." ->
Add the path to the bin folder of your JDK installation (e.g., C:\Program Files\Java\
jdk-XX\bin).
• Verify Installation: Open the Command Prompt (or Terminal) and type java –
version, If the installation is successful, it will display the installed JDK version.
Setup IDE(Eclipse/JDeveloper)
• Download: Get the
Eclipse IDE for Enterprise Java and Web Developers from Eclipse's
official website.
• Install: Run the installer, select "Eclipse IDE for Enterprise Java and Web
Developers," choose the installation path, and complete the setup.
First Code In Java
• Launch Eclipse and Set Workspace: Open Eclipse IDE and create/select a workspace
folder to save your projects.
• Create a Java Project: Go to File > New > Java Project, name your project (e.g.,
"FirstJavaProject"), and click Finish.
• Create a Java Class: Right-click on your project and select New > Class. Name the class
(e.g., "HelloWorld") and add the main method.
• Run Your Program: Click the Run button (green play icon) in the toolbar or right-click on
the class file and select Run As > Java Application. The output "Hello, World!" will appear
in the console panel at the bottom of the IDE.
How Java Works
• Java Compilation : Java Compilation is a 2 Step process
• Platform Independence: Java programs are compiled into bytecode, which can run on any
system with a Java Virtual Machine (JVM). This ensures that programs are not hardware-
dependent and do not need re-compilation for different platforms.
• .Universal Compatibility: Powered by JVM, Java enables "Write Once, Run Anywhere"
functionality, allowing programs to seamlessly run on Windows, Mac, Linux, or any JVM-
supported environment.
JDK
• What is JDK?: JDK (Java Development Kit) is a software development
environment used to create Java applications. It includes Java Runtime
Environment (JRE) and essential development tools.
• Java Platforms: JDK is available in three editions—Standard Edition (SE) for
general applications, Enterprise Edition (EE) for web and enterprise software,
and Micro Edition (ME) for mobile and embedded systems.
JRE
• What is JRE ?
JRE is software that allows you to run Java programs on your computer. It
provides all the tools and libraries needed to execute Java applications
smoothly.
• Works with JVM: It includes the Java Virtual Machine (JVM), which translates
Java code into a format that your computer can understand and execute.
• Not for Development: JRE is designed specifically for running Java
applications—it does not contain tools for writing or compiling code, like a
compiler (javac).
JVM
• What is JVM (Java Virtual Machine) ?
o JVM is a virtual machine that runs Java programs by executing Java bytecode (compiled Java
code).
o It is platform-independent, meaning Java programs can run on any operating system with a
JVM
• Interpreter and JIT Compiler: Both the Interpreter and JIT Compiler are
integral parts of the JVM. They work together to convert Java bytecode into
machine code for execution, with the Interpreter translating code line by line
and the JIT Compiler optimizing performance by compiling frequently used
bytecode into machine code.
Data Types In Java
• Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java
o Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.
o Non-primitive data types: The non-primitive data types include Classes,
Interfaces, Strings and Arrays.
Variables
• A Variable is a Data Holder which stores the data, There are 2 types of Variables
1. Global Variable-:
• Static Variable / class Variable.
• Non Static Variable / Instance variable.
2. Local Variable
• Global Variable :-
1. A Global variable is a variable which is declared directly within class ,
outside method or constructor.
Variables
• Types of Global Variables-:
• Instance Variables / non static Variables :
1. These are non-static global variables defined within a class but outside any method.
2. They belong to an object and are initialized when the object is created.
• String Literals: -: Represent a sequence of characters enclosed in double quotes ("), like
"Hello", "Java".
• Boolean Literals: Represent logical values, either true or false.
• Null Literal: Represents the absence of any value or object reference (null).
Wrapper Class
• What Are Wrapper Classes ?
• A Wrapper Class is used to convert primitive data types (like int, char,
boolean) into objects.
• Example: Converting an int into an object of the class Integer.
• byte → Byte
• short → Short
• int → Integer
• long → Long
• float → Float
• double → Double
• char → Character
• boolean → Boolean
Type Conversion
• What is Type Conversion ?
• Type Conversion is the process of converting one data type into another in Java.
• Subtraction (-):
§ Subtracts one value from another.
• Division (/):
§ Divides one value by another. Returns the quotient.
§ Note: Dividing integers results in integer division (e.g., 7 / 2 = 3). Use floating-point
numbers for precise division.
• Modulus (%):
§ Returns the remainder of division.
Example: 5 == 5 // true
§ Example: 5 != 3 // true
• They are commonly used in decision-making statements like if, while, and for.
• OR (||):
§ Returns true if at least one condition is true.
• NOT (!):
§ Negates or reverses the value of a boolean expression (i.e., true becomes false and false
becomes true).
§ Example: !(5 > 3) // false
If else statement
• What is an if-else Statement?
• If the condition is false from the start, the loop won’t execute even once.
Do-While loop
• What is a Do-While Loop?
• A do-while loop is like a while loop, but the key difference is that it
always executes the code at least once, even if the condition is false.
• The condition is checked after the code runs.
• Syntax -:
• You can use one loop inside another for handling multidimensional tasks
like matrix operations or patterns.
• It is ideal when the number of iterations is known beforehand.
• Example :
Which loop to use
• While Loop::
• Use when the number of iterations is not known in advance and depends
on a condition.
• Example: Repeating an action while a user has not entered a valid
password.
• Do-While Loop:
• Use when you want the code to execute at least once, regardless of the
condition.
• Example: Prompting a user for input until they enter valid data.
• For Loop:
• Use when you know the exact number of iterations beforehand.
• Example: Counting numbers from 1 to 10.
Class And Object Theory
• What is a Class ?
• What is an Object ?
• An object is an instance of a class.
• It is a real-world entity that is created using the class blueprint.
• Example -:
Class And Object Practical
• Example: Managing Student Details
• Imagine you want to manage student information in a school. You can create
a class to store details like name, age, and marks, and then use objects to
interact with the data.
• Step 1: Define the Class
Class And Object Practical
• Step 2: Use Objects • Outcomes from This Example:
• Class :- The Student class acts as a
blueprint that defines attributes (name,
age, marks) and behavior
(displayDetails() method).
• Object :- Each object (student1,
student2) represents an individual
student with its own values for attributes.
• Assigning Values :- Values are assigned
directly to the object’s properties (e.g.,
student1.name = "Rahil";).
• Calling Methods :- Methods like
displayDetails() are called using the
object to perform actions.
Class And Object Practical
• Task 1: Create a Class to Represent an Animal
• Objective: Imagine you are tasked with representing an animal in a zoo using a
class.
• Instructions:
Think about the important details an animal might have (e.g., species, weight, diet, habitat).
Think of actions the animal might perform (e.g., eating, sleeping, making sounds).
Implement the class and create objects for different animals like a lion, an elephant, etc.
• Instructions:
§ Think about what aspects of a shop would be important (e.g., name, type of goods, location).
§ Consider the actions a shop might perform (e.g., selling goods, displaying inventory).
§ Create objects for different shops and simulate their behavior (e.g., selling items)
Methods
• What Are Methods ?
• They are executed only when called and can take inputs (parameters) and return
outputs.
• Structure of a Method
• Syntax of a Method :-
Methods
• Types of Methods
• Void Methods :-
Perform an action but do not return any value.
Example - ;
• Return Methods :-
Perform an action and return a value to the caller.
Example -: Calculating and returning the sum of two numbers.
Methods
• Types of Methods
• Parameterized Methods :-
Take input values (parameters) to perform tasks.
Example: Multiplying two numbers passed as inputs.
• Non-Parameterized Methods :-
Do not take any inputs and execute the same task every time.
Example -:
Methods
• Types of Methods
• Static Methods :-
Belong to the class rather than objects of the class. Can be called without creating an object.
Example -:
• Instance Methods :-
Belong to objects of the class. Require an object to be called.
Example :-
Methods
• How to Call a Method?
• Create an Object :-
For instance methods, use an object to call the method:
Example -:
Example :-
Method Overloading
• What is Method Overloading?
• Method Overloading is a feature in Java where multiple methods in the same class
have the same name but different parameters.
• It allows a class to perform similar operations with different types or numbers of
arguments.
• The compiler differentiates the methods based on their method signature (method
name + parameter list).
• To Call Current Class Methods :- Used to call another method in the same class
from a method.
this keyword
• Uses of the this Keyword
• To Call Current Class Constructor :- Used for constructor chaining (calling one
constructor from another within the same class).
•
• To Return the Current Class Object :- Helps return the current instance of the
class.
• By new Keyword :-
• Strings can also be created using the new keyword.
• Example:
• In this case: A new String object is created in the heap (non-pool), The literal "Welcome" is stored in
the String Constant Pool separately.
StringBuffer and StringBuilder
• What are StringBuffer and StringBuilder?
Syntax:
Example:
• Memory Allocation:
§ Allocate memory for the array using the new keyword and define its size.
§ Syntax:
§ Example :
• Initialization :-
§ Assign values to individual elements of the array using their indices.
§ Syntax:
§ Example :
Creation of Array
• Steps to Create an Array in Java
• Shortcut (Declare + Allocate + Initialize):
Combine all steps into a single line.
Syntax:
Example:
Jagged and 3D array
• What is a Jagged Array ?
• A jagged array is an array of arrays where the sub-arrays can have different
lengths.
• It is also known as an uneven array.
• Jagged arrays are especially useful for representing irregular data structures, like a
triangle or a matrix with unequal rows.
Jagged and 3D array
• What is a 3D Array ?
• A 3D Array is an array of arrays of arrays, meaning it has elements arranged in three
dimensions.
• Homogeneous Data:
• Arrays can only store elements of the same data type.
• This makes them unsuitable for storing mixed data types (e.g., combining integers, strings, and floats).
• No Built-in Methods:
• Arrays lack built-in methods for common operations like searching, sorting, or resizing.
• You need to write additional code or use external libraries for these tasks.
• Declaration :- To declare an array of objects, specify the class name followed by square
brackets [ ].
• Instantiation :- After declaring the array, instantiate it using the new keyword, specifying the
size of the array.