0% found this document useful (0 votes)
14 views

Java Study Material

java study material

Uploaded by

TRANAND TR
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java Study Material

java study material

Uploaded by

TRANAND TR
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Data types in Java

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.

ii) Non-primitive data types (object data types) –


a. class (built-in or user-defined),
b. interface (built-in or user-defined),
c. arrays,
d. enum,
e. exceptions (built-in or user-defined),
f. annotations

// Program to demonstrate primitive data types in java


import java.util.Scanner;
class Conversion {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
// byte: 8-bit signed integer
byte byteValue;
byteValue = s.nextByte();
System.out.println("Byte Value: " + byteValue);

// short: 16-bit signed integer


short shortValue;
shortValue = s.nextShort();
System.out.println("Short Value: " + shortValue);

// int: 32-bit signed integer


int intValue;
intValue = s.nextInt();
System.out.println("Int Value: " + intValue);

// long: 64-bit signed integer


long longValue;
longValue = s.nextLong();
System.out.println("Long Value: " + longValue);

// float: 32-bit floating point


float floatValue;
floatValue = s.nextFloat();
System.out.println("Float Value: " + floatValue);

// double: 64-bit floating point


double doubleValue;
doubleValue = s.nextDouble();
System.out.println("Double Value: " + doubleValue);

// char: 16-bit Unicode character


char charValue;
charValue = s.next().charAt(0);
System.out.println("Char Value: " + charValue);
// boolean: true or false
boolean booleanValue;
booleanValue = s.nextBoolean();
System.out.println("Boolean Value: " + booleanValue);
}
}
CONSTRUCTORS
 A constructor is a special type of method that is used to initialize objects.
 The constructor is automatically/implicitly called when an object of a class is
created using the `new` keyword.
 The constructor's name must match the class name.
 Constructors do not have a return type, not even `void`.

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

 A constructor that takes arguments to initialize an object with specific values.

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;

public Conversion() //no argument constructor


{
iKm=100;
}

public Conversion(int k) //one-argument constructor


{
iKm=k;
}

public Conversion(Conversion cObj) //one-argument constructor


{
iKm=cObj.iKm;
}

public void miles()


{
dMile = iKm*0.621371;
System.out.println(iKm+ "km equal to " + dMile + " miles");
}

public static void main(String[] args)


{
Conversion obj1 = new Conversion();
Conversion obj2 = new Conversion(500);
Conversion obj3 = new Converison(obj2);

obj1.miles();
obj2.miles();
obj3.miles();
}
}
// Java program implementing constructor chaining

class Person
{
String name;
int age;

// Constructor with no arguments


Person()
{
this("Unknown", 0); // Calls the parameterized constructor in the same class
System.out.println("Person: Default Constructor called");
}

// Parameterized constructor
Person(String name, int age)
{
this.name = name;
this.age = age;
System.out.println("Person: Parameterized Constructor called");
}

// Method to display person details


void display()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

public static void main(String args[])


{
Person p1 = new Person();
Person p2 = new Person(“arun”,30);

p1.display();
p2.display();
}
}
STATIC MEMBERS AND METHODS

 The static keyword is used to define class-level variables and methods.


 Such members and methods belong to the class itself, rather than to instances.

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;

BankAccount(String accountHolder, double balance)


{
this.accountHolder = accountHolder;
this.balance = balance;
totalAccounts++;
}

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();

BankAccount.displayTotalAccounts(); // Display total accounts using static method


}
}

You might also like