Java Study Material
Java Study Material
i) Primitive data types -byte, short, int, long, float, double, char, Boolean
a. byte: A very small integer value (8 bits).
b. short: A small integer value (16 bits).
c. int: A standard integer value (32 bits).
d. long: A large integer value (64 bits), suffixed with 'L' to denote long
literal.
e. float: A single-precision floating point number (32 bits), suffixed with 'f'
to denote float literal.
f. double: A double-precision floating point number (64 bits).
g. char: A single character (16-bit Unicode character).
h. boolean: A value that is either true or false.
Types of Constructors
1. Default Constructor
If no constructor is defined in the class, Java provides a default constructor
with no parameters.
It initializes the object with default values (e.g., `null` for objects, `0` for
integers).
2. Parameterized Constructor
Constructor Overloading
A class can have multiple constructors in a class with different parameter lists
(different signatures), and is called constructor overloading.
Constructor Chaining
Constructor chaining occurs when a constructor calls another constructor of the
same class or superclass.
This can be done using `this()` for the same class and `super()` for the superclass.
Copy Constructor
This constructor creates a new object as a copy of an existing object.
Example programs
// Java program implementing different types of constructors to convert kilometre
into mile
class Conversion
{
public int iKm;
public double dMile;
obj1.miles();
obj2.miles();
obj3.miles();
}
}
// Java program implementing constructor chaining
class Person
{
String name;
int age;
// Parameterized constructor
Person(String name, int age)
{
this.name = name;
this.age = age;
System.out.println("Person: Parameterized Constructor called");
}
p1.display();
p2.display();
}
}
STATIC MEMBERS AND METHODS
Static members
Static variables(members) are shared among all instances of a class. They are
useful when you want to store data that is common to all objects of a class.
Ex:
In a BankAccount class, to keep track of the total number of bank accounts
created, a static variable can be used.
static int totalAccounts = 0;
Every time a new BankAccount is created, this static variable can be incremented.
Since the variable is static, it is shared by all instances, ensuring that it always
holds the correct count.
Static variables are also used to define constants, which are values that do not
change.
Ex:
static final double PI = 3.14159;
Here, PI is a constant value that is shared across all instances, and it is also
marked as final to prevent modification.
Static methods
Static methods belong to the class, not to any specific object of the class.
Static methods can be called directly using the name of the class without creating
an instance of the class.
The methods that do not require any data from the instance variables (non-static
fields) can be made static. These methods often perform operations that are
related to the entire class, rather than any specific object.
Sometimes, a class might include a method to create and return an instance of
the class. This is often done using a static method called a factory method:
Ex:
static MyClass createInstance()
{
return new MyClass();
}
This method can be called directly using the class name to create objects.
The main method in Java is always static because it needs to be called by the JVM
(Java Virtual Machine) without creating an instance of the class. It serves as the
entry point of the application:
public static void main (String[] args)
{
// Code to execute
}
Example programs
// Java program implementing static members and methods
BankAccount.java
class BankAccount
{
static int totalAccounts = 0; // Static variable to count total accounts
String accountHolder;
double balance;
void displayAccountDetails()
{
System.out.println("Account Holder: " + accountHolder);
System.out.println("Balance: " + balance);
}
static void displayTotalAccounts() //static method definition
{
System.out.println("Total bank accounts created: " + totalAccounts);
}
}
MyClass.java
class MyClass
{
public static void main(String[] args)
{
BankAccount account1 = new BankAccount("Anand", 1500.00);
BankAccount account2 = new BankAccount("Arun", 2500.00);
account1.displayAccountDetails();
account2.displayAccountDetails();