Java Final-2
Java Final-2
An array is a collection of similar type of elements which has contiguous memory location.
Advantages
• Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
• Random access: We can get any data located at an index position.
• Efficient for Iteration: Java arrays are well-suited for iterative operation.
Disadvantages
• Size Limit: We can store only fixed size of elements in the array. It doesn’t grow its size
at runtime.
• Homogeneous data type: Arrays in Java store elements of the same data type.
• No Built-in Resize Mechanism: Unlike some dynamic data structures like ArrayList, Java
arrays do not have a built-in mechanism for automatic resizing.
Types
Example
Multidimensional Array
Output
Syntax:
Example:
Examples
Some Important Array methods
1. ‘toString’: Returns a string representation of the contents of an array
3. ‘equals’: Checks if two arrays are equal or not. It returns a Boolean value
4. ‘fill’: Assigns the specified value to each element of the specified array.
Loops in Java
In Java, a loop is a control flow statement that allows a certain block of code to be
executed repeatedly.
For Loop:
The for loop is used when the number of iterations is known in advance. It consists of
three parts: initialization, condition, and iteration expression.
Syntax:
Example:
While Loop:
The while loop is used when the number of iterations is not known in advance, and it
depends on a specified condition.
Syntax:
Example:
Do-While Loop:
The do-while loop is similar to the while loop, but it guarantees that the code block is
executed at least once because the condition is checked after the block is executed.
Syntax:
Example:
Decision Making Statements
Decision making statements decide which statement to execute and when.
Decision making statements are two types
• If-else statement
• Switch statement
If-else statement:
Syntax:
Nested if-else
Syntax:
Switch Statement:
Syntax:
Jump statements
• break statement
• continue statement
break statement:
The break statement is used to break the current flow of the program and transfer the control
to the next statement outside a loop or switch statement.
continue statement:
Unlike break statement, the continue statement doesn’t break the loop, whereas, it skips the
specific part of the loop and jumps to the next iteration of the loop immediately.
Example:
Encapsulation in Java
Encapsulation is a process of wrapping code and data together into single unit.
Example:
Polymorphism in Java
Polymorphism in Java is the task that performs a single action in different ways.
There are two types of polymorphism in Java
1. Compile-time Polymorphism (Method Overloading)
2. Runtime Polymorphism (Method Overriding)
Example:
Example:
Method Overloading VS Method Overriding
Method overloading is the example of Method overriding is the example of run time
compile time polymorphism. polymorphism.
Inheritance in Java
Inheritance allows a class (subclass) to inherit the properties and behaviors of another class
(superclass or base class).
Types
• Single Inheritance
• Multiple Inheritance (through Interface)
• Multilevel Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Single Inheritance
In single inheritance, a class can only inherit from one superclass
Constructor in Java
A constructor is a special method within a class that is automatically called when an instance of
that class is created.
Types
Default constructor
A constructor is called default constructor when it doesn’t have any parameter.
Parameterized constructor
A constructor which has a specific number of parameters is called a parameterized constructor.
Java String Processing
1. charAt(int index):
• Returns the character at the specified index.
2. length():
• Returns the length of the string.
4. concat(String str):
• Concatenates the specified string to the end of the original string.
5. indexOf(string str):
• Returns the index of the first occurrence of the specified substring.
10. isEmpty():
• Returns true if the string is empty, otherwise returns false.
Methods
Methods Description
boolean hasMoreTokens() It checks if there is more tokens available.
Example
StringBuffer in Java
In Java, StringBuffer is a class that provides a mutable sequence of characters. It is part
of the ‘java.lang’ package and is used for manipulating strings.
Mutable means that you can modify their content.
2. ‘insert()’: Inserts the specified string at the specified index in the StringBuffer object.
3. ‘delete(int start, int end)’: Deletes the characters from the start index to the end-1
index in the StringBuffer.
5. ‘replace(int start, int end, String str)’: Replaces the characters from the start index
to the end-1 index with the specified string.
6. ‘reverse()’: Reverse the characters in the StringBuffer object.
StringBuilder in Java
In Java, the StringBuilder class is part of the ‘java.lang’ package and provides a mutable
sequence of characters.
2. ‘insert()’: Insert the specified string representation at the specified position in the
sequence.
StringBuffer VS StringBuilder
Java Exception Handling
The Exception Handling in Java is one of the powerful mechanism to handle the runtime
errors so that the normal flow of the application can be maintained.
ArrayIndexOutOfBoundsException:
FileNotFoundException:
NullPointerException:
Stream in Java
Stream is introduced in Java 8, Stream API is used to process collections of
objects.
A stream in Java is a sequence of objects that supports various methods which
can be pipelined to produce the desired result.
Intermediate Operations
Intermediate Operations are the types of operations in which multiple methods
are chained in a row.
Important Intermediate Operations
There are a few Intermediate Operations mentioned below:
1. map()
The map method is used to return a stream consisting of the result of applying
the given function to the elements of this stream.
2. filter()
The filter method is used to select elements as per the Predicate passed as an
argument.
3. sorted()
The sorted method is used to sort te stream.
Example:
Terminal Operations
Terminal Operations are the type of Operations that return the result. These
Operations are not processed further just return a final result value.
2. forEach()
The forEach method is used to iterate through every element of the stream.
3. reduce()
The reduce method is used to reduce the elements of a stream to a single
value. The reduce method takes a BinaryOperator as a parameter.
Stream IO (input/Output):
InputStream:
OutputStream:
File Reading/Writing
There are multiple ways of writing and reading a text file in Java. This is
required while dealing with many applications. There are several ways to
read a plain text file in Java e.g. we can use FileReader, BufferedReader, or
Scanner to read a text file. Every utility provides something special e.g.
BufferedReader provides buffering of data for fast reading, and Scanner
provides parsing ability.
Methods:
1. Using BufferedReader/BufferedWriter class
2. Using Scanner class
Multitasking
When a single processor (CPU) does many jobs (program, threads, process, task)
simultaneously, it is called multi-tasking.
Swing Components
1. JFrame:
• A top-level container that represents the main window of an application.
2. JPanel:
• A Container that can hold other components.
3. JButton:
• A button that can trigger an action when clicked.
4. JTextField:
• A single-line text input field.
5. JTextArea:
• A multi-line text area.
6. JLabel:
• A non-editable text display.
7. JCheckBox:
• A checkbox for selecting multiple options.
8. JRadioButton:
• A radio button for selecting a single option from a group.
9. JComboBox:
• A drop-down list of items.
10. JScrollPane:
• A scrollable pane that allows viewing content that doesn’t fit in a container.
11. JDiolog:
• A pop-up dialog box.
12. JProgressBar:
• A component that visually displays the progress of a task.