0% found this document useful (0 votes)
5 views10 pages

Java Notes-1

The document provides an overview of constants, methods, and object initialization in Java, highlighting the use of the final keyword for constants and the differences between static and non-static methods. It also covers the concepts of inheritance, polymorphism, and string handling, explaining the immutability of strings and the differences between various string classes. Additionally, it discusses file handling, JDBC, socket programming, and key Java features, including method return types and variable naming conventions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Java Notes-1

The document provides an overview of constants, methods, and object initialization in Java, highlighting the use of the final keyword for constants and the differences between static and non-static methods. It also covers the concepts of inheritance, polymorphism, and string handling, explaining the immutability of strings and the differences between various string classes. Additionally, it discusses file handling, JDBC, socket programming, and key Java features, including method return types and variable naming conventions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Constants in Java

A constant is a variable whose value cannot be changed once assigned.

Declaration of Constant
Constants are declared using:
final keyword
Features of Constants
Value remains fixed
Improves security
Used for fixed values like PI

Constants are generally written in uppercase.


Example:
MAX_VALUE
PI
RATE_OF_INTEREST

Types of Constants
Numeric constants
Character constants
String constants
Boolean constants

final keyword
Variables
Data types

Ternary Operator
Ternary operator is a shorthand form of if-else statement.

Syntax
condition ? expression1 : expression2
Working
If condition is true → first expression executes
If false → second expression executes
Advantages
Reduces code length
Improves readability for simple conditions
Limitations
Not suitable for complex conditions

Conditional statements
Operators
Loops

Object Initialization
Object initialization means assigning values to object variables.

Ways of Object Initialization


1. Using Reference Variable
2. Using Method
3. Using Constructor
Importance
Assigns initial state to objects
Prevents garbage values
Constructors
Objects and classes
this keyword
super keyword

Static Method
A static method belongs to the class rather than object.

Features
Can be called without object creation
Accessed using class name
Static methods can access only static members directly
Characteristics
Memory allocated once
Shared among objects
Advantages
Faster access
Memory efficient

Difference Between Static and Non-Static Method


Static Method Non-Static Method
Belongs to class Belongs to object
Called without object Requires object
Access static members directly Access all members

static variable
static block
main method

Difference Between this() and super()


this()
this() refers to current class constructor.
Uses
Constructor chaining within same class
Reuse constructor code

super()
super() refers to parent class constructor.
Uses
Call parent constructor
Access parent members

this() super()
Refers current class Refers parent class
Calls same class constructor Calls parent class constructor
Used for constructor chaining Used in inheritance
Important Rule
Both must be first statement inside constructor.

Inheritance
Constructors
Constructor chaining
Runtime Polymorphism
Runtime polymorphism is achieved through method overriding.
Features
 Method call resolved at runtime
 Achieved using inheritance
Advantages
 Flexibility
 Dynamic behavior
 Code reusability
Types of Polymorphism
1. Compile-Time Polymorphism (Method Overloading)
2. Runtime Polymorphism (Method Overriding)

Inheritance

Why String is Immutable?


Immutable means value cannot be changed after creation.

Reasons for Immutability


1. Security
Used in networking, database URLs, file paths.
2. Thread Safety
Multiple threads can safely share strings.
3. Memory Optimization
String Constant Pool improves memory usage.
4. Performance
Hashcode caching improves performance.
String Constant Pool
Special memory area where string literals are stored.

Mutable vs Immutable Objects


Mutable Immutable
Can change Cannot change
Example: StringBuilder Example: String

StringBuilder
StringBuffer
Heap memory

Difference Between == and equals()


== Operator
Purpose
Compares memory addresses (references).
equals() Method
Purpose

Compares actual content/values.


Difference Table
== equals()
Compares references Compares values
Operator Method
Used for primitive/reference comparison Used for object content

Object class
String handling
Memory management
Return Statement
return statement transfers control back to calling method.

Uses
Ends method execution
Returns value from method
Types
return with value
return without value
Important Concepts
Methods with return type must return value
void methods use return without value

Methods
Function calls
void keyword

Variable Naming Rules


Rules for Naming Variables
Cannot start with digit
Can contain letters, digits, underscore, $
Cannot use keywords
Case-sensitive

Naming Conventions
Type Convention
Variable camelCase
Class PascalCase
Constant UPPER_CASE

Identifiers
Keywords

Constant Declaration Syntax


Syntax
final datatype CONSTANT_NAME = value;
Example Structure
final int MAX = 100;
Features
Value cannot change
Must initialize at declaration or constructor

final keyword
Constants
Variables
Array Length Property
length is a property used to determine array size.

Features
Returns total elements in array
Predefined property

Difference Between length and length()


length length()
Used for arrays Used for strings
Method
Arrays
Loops
String methods

Void Return Type


void indicates method does not return any value.

Features
Used in methods performing tasks only
No return value required
Common Example
main() method

Methods
return statement
Method signatures
Default Constructor Behavior
Introduction

If programmer does not create constructor, Java automatically provides default constructor.

Features
No parameters
Initializes default values
Default Values
Data Type Default Value
int 0
float 0.0
boolean false
String null
Importance
Enables object creation
Prevents compilation errors
Related Topics
Constructors
Object initialization
Parameterized constructor

Call by Value and Object Reference Passing


Call by Value
Java always uses call by value.

Primitive Data Passing

Copy of variable value is passed.

Object Reference Passing


Introduction

Copy of object reference is passed.

Important Concept

Original object data can be modified through reference.


Difference Table
Primitive Passing Object Passing
Copy of value passed Copy of reference passed
Changes not reflected Object changes reflected

Memory allocation
Stack and heap memory
Objects
Abstract Class and Interface
Abstract Class
Features
Can contain abstract and concrete methods
Can have constructors and variables
Interface
Features
Supports full abstraction
Used for multiple inheritance
Key Differences
Abstract Class Interface
Partial abstraction Full abstraction
extends keyword implements keyword
Constructors allowed No constructors

Java Versions Enhancement


Java 8 introduced default and static methods in interface
Java 9 introduced private methods in interface

Abstraction
Multiple inheritance
Polymorphism

Inheritance
Inheritance allows one class to acquire properties of another class.

Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
Advantages
Code reusability
Reduces redundancy
Improves maintainability
Important Keywords
extends
super
Related Topics
Method overriding
Runtime polymorphism
OOP concepts
Thread Creation
Creating Thread Using Thread Class
Steps
Extend Thread class
Override run()
Start thread using start()
Creating Thread Using Runnable Interface
Steps
Implement Runnable interface
Override run()
Pass object to Thread class
Difference Between Thread and Runnable
Thread Class Runnable Interface
Inheritance based Interface based
Cannot extend another class Allows multiple inheritance
Thread Life Cycle
New
Runnable
Running
Waiting
Dead
Related Topics
Multithreading
Synchronization
Thread scheduling
File Handling
File handling allows reading and writing data into files.

FileReader
Purpose

Reads character data from file.

FileWriter
Purpose

Writes character data into file.

File Handling Operations


Create file
Read file
Write file
Append file
Delete file
Advantages
Permanent storage
Data sharing
Backup management
Exception Handling in File Handling

File operations may throw:

IOException
FileNotFoundException
Related Topics
Streams
BufferedReader
BufferedWriter
Line Counting in File
Concept
Reading file line-by-line and counting total lines.

Common Classes Used


FileReader
BufferedReader
Applications
Log analysis
Data processing
File statistics
Related Topics
File handling
Loops
Exception handling
JDBC Working in Depth
Driver Loading
Introduction

JDBC driver establishes communication between Java and database.

Connection Lifecycle
Steps
Open connection
Execute query
Process results
Commit/Rollback
Close connection
Statement Execution
Types of Statements
1. Statement
2. PreparedStatement
3. CallableStatement
ResultSet Handling
Introduction

ResultSet stores retrieved database records.

Cursor Movement Methods


next()
previous()
first()
last()
Transaction Control
Transaction

Group of SQL operations executed together.

ACID Properties
Property Meaning
Atomicity All or none
Consistency Valid state
Isolation Independent transactions
Durability Permanent changes
Commit and Rollback
commit() saves changes
rollback() cancels changes
Related Topics
SQL
Database connectivity
MySQL

Socket Programming
Socket programming enables communication between systems.

Types of Sockets
1. TCP Socket

Reliable communication.

2. UDP Socket

Fast but unreliable communication.

Important Classes
Class Purpose
Socket Client communication
ServerSocket Server communication
Client-Server Communication Flow
Client Request → Server Response
Applications
Chat applications
Online games
File transfer
Related Topics
Networking
IP address
Port number

String Handling in Java


String handling refers to operations performed on strings.

String Class Features


Immutable
Stored in String Constant Pool
Rich built-in methods
String Comparison Methods
1. equals()
2. equalsIgnoreCase()
3. compareTo()
4. == operator
String Memory Pool
Introduction

Special heap area for string literals.

Advantages
Memory optimization
Reusability
Performance Implications
String
Immutable, slower for repeated modifications.

StringBuilder

Fast, mutable, non-thread-safe.

StringBuffer

Mutable, thread-safe.

Difference Table
String StringBuilder StringBuffer
Immutable Mutable Mutable
Slow modifications Fast Thread-safe
Common String Methods
length()
substring()
charAt()
indexOf()
toUpperCase()
toLowerCase()
Related Topics
Heap memory
Object comparison
Arrays of strings

You might also like