Java Notes
Java Notes
Array
It is a collection of multiple data of same data type. For example we can have an array of
int type or float type of data etc.
The starting index i.e index of the first element of an array is always zero.
The index of last element is n-1, where n is the size of the array.
Syntax of declaring an array:
Data_type array_name[]=new data_type[array_size];
For e.g. int a[]=new int[10];
Initialization Garbage values if not initialized Default values (0, null, false)
Traversing Uses for and foreach loop for uses for-each, Iteraor or
Elements iteration ListIterator
ArrayList speed is
Array speed is fast due to the relatively slower due to
fixed size resizing and dynamic
Speed behaviour
Supports generics,
Not supported, making arrays
making ArrayList type-
type-unsafe
Generics safe
Fixed Size: Use an array when you know the exact number of elements in advance, and
that number will not change during runtime. Examples include storing the 12 months of
the year or the 6 sides of a cube.
Performance is Critical: Arrays offer better performance and less memory overhead
because they store elements in a contiguous memory block and avoid the overhead of
dynamic resizing and method calls. This is crucial in performance-intensive applications
like numerical computations or low-level operations (e.g., byte buffers for file I/O).
Storing Primitive Data Types: Arrays can directly store primitive types
(like int, char, boolean) without needing wrapper classes (Integer, Character, Boolean).
This avoids "autoboxing" overhead, which saves significant memory and improves
performance.
Multi-dimensional Data: Arrays natively support multi-dimensional structures (e.g., int[]
[] for a grid or matrix), which ArrayLists do not support directly.
Integration with Existing APIs: Some older or low-level Java APIs and methods (like
the main method arguments) require or return arrays, so you must use arrays to interact
with them.
import [Link];
class StringLength
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
String str;
Import [Link];
Class joinstring
{
Public static void main(string[] args)
{
Scanner sc = new Scanner([Link]);
String str1, str2, result;
Super Class: The class whose features are inherited is known as superclass(or a base
class or a parent class).
Sub Class: The class that inherits the other class is known as a subclass(or a derived
class, extended class, or child class). The subclass can add its own fields and methods
in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to
create a new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.
3. [Link]
Used for input and output operations (file handling).
Common Classes:
File
FileInputStream
BufferedReader
Example:
File f = new File("[Link]");
4. [Link]
Supports networking operations.
Common Classes:
URL
Socket
ServerSocket
Example:
URL u = new URL("[Link]
5. [Link]
Used to create graphical user interface (GUI) applications.
Common Classes:
Frame
Button
Label
Example:
Frame f = new Frame("My Frame");
6. [Link]
Provides lightweight GUI components.
Common Classes:
JFrame
JButton
JLabel
Example:
JFrame jf = new JFrame("Swing App");
7. [Link]
Used for database connectivity (JDBC).
Common Classes:
Connection
Statement
ResultSet
Example:
Connection con = [Link](url);
Conclusion
Java API packages provide ready-made solutions for common programming tasks,
increasing productivity and reliability of Java applications.