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

Java

Java notes for quick revision
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java

Java notes for quick revision
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

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

int a=9; float f=10.5f;


float f=a; int a=(int)f;
○ Can contain letters (uppercase and lowercase), digits, underscore (_), and dollar sign
($).
○ Must begin with a letter, underscore, or dollar sign.
○ Cannot be a keyword.
Case-sensitive (e.g., myVariable and myvariable are different).
● Primitive Data Types:

○ byte: 8-bit integer, range: -128 to 127


○ short: 16-bit integer, range: -32,768 to 32,767
○ int: 32-bit integer, range: -2^31 to 2^31-1
○ long: 64-bit integer, range: -2^63 to 2^63-1
○ float: 32-bit floating point
○ double: 64-bit floating point
○ char: 16-bit Unicode character
○ boolean: true or false
Non-Primitive Data Types:

Class:
Arrays & String
Operators
● Arithmetic Operators:
● Unary Operators:
● Assignment Operators:
● Relational Operators:
● Logical Operators & Bitwise Operators
Bitwise Operations

1. Bitwise AND (a & b):


○ Binary representation of a = 5 is 0101.
○ Binary representation of b = 3 is 0011.
○ Bitwise AND: 0101 & 0011 results in 0001 (which is 1 in decimal).
2. Bitwise OR (a | b):
○ Binary representation of a = 5 is 0101.
○ Binary representation of b = 3 is 0011.
○ Bitwise OR: 0101 | 0011 results in 0111 (which is 7 in decimal).
3. Bitwise XOR (a ^ b):
○ Binary representation of a = 5 is 0101.
○ Binary representation of b = 3 is 0011.
○ Bitwise XOR: 0101 ^ 0011 results in 0110 (which is 6 in decimal).
4. Bitwise Complement (~a):
○ Binary representation of a = 5 is 0101.
○ Bitwise complement: ~0101 results in 1010 (which is -6 in decimal when considering 2's complement
representation).

2. Left Shift (a << 1):
○ Binary representation of a = 5 is 0101.
○ Left shift by 1: 0101 << 1 results in 1010 (which is 10 in decimal).
3. Right Shift (a >> 1):
○ Binary representation of a = 5 is 0101.
○ Right shift by 1: 0101 >> 1 results in 0010 (which is 2 in decimal).
4. Unsigned Right Shift (a >>> 1):
○ Binary representation of a = 5 is 0101.
○ Unsigned right shift by 1: 0101 >>> 1 results in 0010 (which is 2 in decimal).
Scanner Class:
Object-Oriented Programming (OOP) in Java is a programming paradigm that uses objects and classes
to structure software programs. The key concepts of OOP include:

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

Package, Constructor , Access Modifier


Package:
In Java, a package is a namespace that organizes a set of related classes and
interfaces. It helps in preventing naming conflicts and controls access with protected
and default access levels. Packages are also used to organize files within a project for
easier management.
Using Packages in Java

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).

2. Creating and Using Packages

Let's create a simple example demonstrating how to create and use packages in Java.
Flow

Calculator.java is placed in the com.example.math package.


MainClass.java is placed in the com.example.main package.

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.

Characteristics of Jagged Arrays:

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.

You might also like