Extras in java
Extras in java
Java ArrayList is a part of the Java collections framework and it is a class of java.util package. It
provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful
in programs where lots of manipulation in the array is needed. This class is found in java.util package. The
main advantage of ArrayList in Java is, that if we declare an array then we need to mention the size, but in
ArrayList, it is not needed to mention the size of ArrayList. If you want to mention the size then you can do
it.
Let’s understand the Java ArrayList in depth. Look at the below image:
In the above illustration, AbstractList, CopyOnWriteArrayList, and AbstractSequentialList are the classes
that implement the list interface. A separate functionality is implemented in each of the mentioned
classes. They are:
AbstractList: This class is used to implement an unmodifiable list, for which one needs to only extend this
AbstractList Class and implement only the get() and the size() methods.
CopyOnWriteArrayList: This class implements the list interface. It is an enhanced version of ArrayList in
which all the modifications(add, set, remove, etc.) are implemented by making a fresh copy of the list.
AbstractSequentialList: This class implements the Collection interface and the AbstractCollection class.
This class is used to implement an unmodifiable list, for which one needs to only extend this AbstractList
Class and implement only the get() and the size() methods.
1. ArrayList(): This constructor is used to build an empty array list. If we wish to create an empty ArrayList
with the name arr, then, it can be created as:
2. ArrayList(Collection c): This constructor is used to build an array list initialized with the elements from
the collection c. Suppose, we wish to create an ArrayList arr which contains the elements present in the
collection c, then, it can be created as:
3. ArrayList(int capacity): This constructor is used to build an array list with the initial capacity being
specified. Suppose we wish to create an ArrayList with the initial size being N, then, it can be created as:
ArrayList in Java is a class in the Java Collections framework that implements the List interface.
Here are the advantages and disadvantages of using ArrayList in Java.
Conclusion:
• ArrayList is the part of Collections framework. It inherits the AbstractList class and implements
the List interface.
• ArrayList is the implementation of a dynamic array.
• ArrayList can be initialized used using different constructor types like without parameters,
passing collection as a parameter, and passing integer as a parameter.
• Operations can be performed in ArrayList as follows Adding, removing, iterating, and sorting.
* Bytecode in Java:
Bytecode in Java is a highly optimized set of instructions for the Java Virtual Machine (JVM) that
reads and interprets to run the java program. A bytecode is a binary program code that can only run on
JVM. In other words, it is a machine language (code) for JVM in the form of .class file, but it is not
machine specific because it is not a native code. In simple words, it is not machine language (machine
instructions) for any specific hardware processor. Byte code acts as an intermediate language that is
platform (machine) independent. It is generated by Java interpreter that can be directly run by a real
machine.
Java bytecode has two most important characteristics that are as follows:
1.Byte code is independent of processor, i.e., Java program can be executed on any
processor architecture.
2.It does not depend on operating systems such as Windows, Linux, and Mac OS.
Java source code file (with a .java extension) are compiled into bytecode (with a .class
extension), which is then interpreted and executed by a Java processor called JVM.
Key points:
1. A bytecode in Java is a set of byte-long instructions that Java compiler produces and Java
interpreter
(JVM) executes.
2. When Java compiler compiles .java file, it generates a series of bytecode (machine
independent code) and stores them in a .class file.
3. JVM then interprets and executes the byte code stored in the .class file and converts them
into machine code.
4. The byte code remains the same on different platforms such as Windows, Linux, and Mac OS.
1. Java SE (Standard Edition): This edition is used to develop client-side applications. It is used
to
develop applications for desktop, communication, and user interface.
2. Java EE (Enterprise Edition): This edition is used to develop server-side applications such as
Java
servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF). In other words, it is used to
develop
web-based, messaging, distributed, and enterprise applications.
3. Java ME (Micro Edition): This edition is used to develop applications for mobile devices,
such as cell
phones. It is also used to develop Personnel Digital Assistants, Setup Box, and printers
applications.
1. ClassLoader Subsystem:
Java's dynamic class loading functionality is handled by the ClassLoader subsystem. It loads,
links. and
initializes the class file when it refers to a class for the first time at runtime, not compile time.
1.1 Loading -> Classes will be loaded by this component. BootStrap ClassLoader, Extension
ClassLoader, and
Application ClassLoader are the three ClassLoaders that will help in achieving it.
1.2 Linking -> Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if
verification fails we will get the verification error.
Prepare – For all static variables memory will be allocated and assigned with default values.
Resolve – All symbolic memory references are replaced with the original references from Method
Area.
1.3 Initialization -> This is the final phase of ClassLoading; here, all static variables will be assigned
with the original values, and the static block will be executed.
Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in
graphics programming.
The syntax of an anonymous class expression is like the invocation of a constructor, except that
there is a class definition contained in a block of code.
Syntax:
Now let us do discuss the difference between regular class(normal classes) and Anonymous Inner class
• A normal class can implement any number of interfaces but the anonymous inner class can
implement only one interface at a time.
• A regular class can extend a class and implement any number of interfaces simultaneously. But
anonymous Inner class can extend a class or can implement an interface but not both at a time.
• For regular/normal class, we can write any number of constructors but we can’t write any
constructor for anonymous Inner class because the anonymous class does not have any name
and while defining constructor class name and constructor name must be same.
Accessing Local Variables of the Enclosing Scope, and Declaring and Accessing Members of the
Anonymous Class
Like local classes, anonymous classes can capture variables; they have the same access to local variables
of the enclosing scope:
• Fields
• Extra methods (even if they do not implement any methods of the supertype)
• Instance initializers
• Local classes
Ways:
Anonymous inner classes are generic created via below listed two ways as follows:
Based on declaration and behavior, there are 3 types of anonymous Inner classes:
We can have an anonymous inner class that extends a class. For example, we know that we can
create a thread by extending a Thread class. Suppose we need an immediate thread but we don’t want
to create a class that extends Thread class all the time. With the help of this type of Anonymous Inner
class, we can define a ready thread.
We can also have an anonymous inner class that implements an interface. For example, we also
know that by implementing Runnable interface we can create a Thread. Here we use an anonymous
Inner class that implements an interface.
Anonymous inner classes in method/constructor arguments are often used in graphical user
interface (GUI) applications. To get you familiar with syntax lets have a look at the following program
that creates a thread using this type of Anonymous Inner class
Red-Black Tree:
A Red-Black Tree is a self-balancing binary search tree that maintains a balance between the
heights of its left and right subtrees. This ensures that search, insertion, and deletion operations take O(log
n) time in the worst case. Red-Black Trees are widely used in various applications where efficient data
structures are required.
Red-Black Trees maintain their balance by enforcing specific rules on the colors of nodes (RED or
BLACK) and the relationships between them. These rules ensure that the tree remains balanced and that
the height difference between the left and right subtrees is at most 1.
• Balanced: Red-Black Trees are self-balancing, ensuring efficient search, insertion, and deletion
operations.
• Efficient: They offer O(log n) time complexity for most operations.
• Simple to implement: The rules for maintaining Red-Black Tree properties are relatively
straightforward.
• Widely used: They are a popular choice for implementing various data structures and algorithms.
• Compared to simpler balanced trees like AVL trees, Red-Black Trees have more complex insertion
and deletion rules.
• Maintaining the Red-Black Tree properties adds a small overhead to every insertion and deletion
operation.
• For applications with frequent insertions and deletions, other balanced tree structures might be
more suitable.
- What are some common applications of Red-Black Trees?