Java
Java
Type Casting
● Assigning a value of one type to a variable of another type is
known as Type Casting.
Type Casting
Type casting is classified into two types:
● Widening Casting
○ byte 🡪 short 🡪 int 🡪 long 🡪 float 🡪 double
● Narrowing Casting
○ double 🡪 float 🡪 long 🡪 int 🡪 short 🡪 byte
Widening Narrowing
Class:
Arrays & String
Operators
● Arithmetic Operators:
● Unary Operators:
● Assignment Operators:
● Relational Operators:
● Logical Operators & Bitwise Operators
Bitwise Operations
1. Class: A blueprint for creating objects. It defines properties (fields) and behaviors (methods) that
the objects created from it can have.
2. Object: An instance of a class. It is a real-world entity with a state and behavior.
3. Encapsulation: The concept of wrapping data (fields) and code (methods) together as a single
unit. It restricts access to certain components and can protect the integrity of the data.
4. Inheritance: A mechanism where a new class (derived class) is created from an existing class
(base class). It allows for code reusability.
5. Polymorphism: The ability of different classes to be treated as instances of the same class
through inheritance. It allows methods to do different things based on the object it is acting upon.
6. Abstraction: The concept of hiding the complex implementation details and showing only the
essential features of the object.
Methods:
access_modifier return_type method_name(parameter_list)
{ // method body
}
Key Points
● Method Signature: Consists of the method name and parameter list. It uniquely identifies the
method within a class.
● Return Statement: Used in methods that return a value. The method must include a return
statement followed by the value to be returned.
● Calling Methods: Methods are called using the syntax objectName.methodName(arguments),
where objectName is an instance of the class containing the method.
Static V/s Instance :
Standard lib Methods
Floor Ceil
1. Declaring a Package
To declare a package, use the package keyword at the top of your Java file, followed by the package
name. The package name is typically in lowercase and uses the domain name convention (e.g.,
com.example).
Let's create a simple example demonstrating how to create and use packages in Java.
Flow
src/
├── com/
│ └── example/
│ ├── math/
│ │ └── Calculator.java
│ └── main/
│ └── MainClass.java
OOPS Concept
1. Encapsulation
Encapsulation is the mechanism of wrapping the data (variables) and code (methods) together as a
single unit. It restricts direct access to some of the object's components, which can prevent the
accidental modification of data.
Explanation: Here, the name variable is private, so it cannot be accessed directly. It can only be
accessed via the public getName() and setName() methods, which encapsulate the data.
2. Inheritance
Inheritance is the mechanism where one class can inherit fields and methods from another class. It
provides code reusability and establishes a parent-child relationship between classes.
Explanation: The Dog class inherits the eat() method from the Animal class, demonstrating
code reuse.
3. Polymorphism
Polymorphism allows objects to be treated as instances of their parent class. The most common use of
polymorphism in OOP is when a parent class reference is used to refer to a child class object.
Explanation: The myAnimal reference of type Animal is used to refer to a Cat object, and the
sound() method of Cat is invoked.
4. Abstraction
Abstraction is the concept of hiding the complex reality while exposing only the necessary parts. It is
achieved using abstract classes and interfaces.
Explanation: The Shape class is abstract and cannot be instantiated. The draw() method is
abstract and must be implemented by any subclass, such as Circle.
5. Interfaces
An interface is a reference type in Java, similar to a class, that can contain only constants, method
signatures, default methods, static methods, and nested types. Interfaces cannot contain instance
fields.
Explanation: The Drawable interface declares a draw() method, which is implemented by the
Rectangle class.
Array
Example
Length
2D Array
Loops
Array Input
Jagged Arrays
A jagged array, also known as a "ragged array," is an array of arrays in which the constituent arrays can be of different lengths,
creating a non-uniform structure. This contrasts with a multidimensional array where all rows (or columns) are of the same length.
1. Flexibility in Size: Each row or sub-array can have a different length, allowing for varying numbers of elements in each.
2. Efficiency: More memory-efficient than rectangular arrays when dealing with varying sizes of data, as it doesn’t waste space on
unused elements.
3. Usage: Often used for efficiently managing data when different subsets have varying numbers of elements. Common examples
include adjacency lists for graphs and other applications where data is naturally non-uniform.