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

Java Notes

The document provides an overview of arrays in programming, comparing C and Java arrays, as well as arrays and ArrayLists. It discusses scenarios for using arrays, such as fixed size and performance-critical applications, and includes examples of string manipulation in Java. Additionally, it outlines important Java API packages and their common classes for various programming tasks.

Uploaded by

Riya Kamble
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Java Notes

The document provides an overview of arrays in programming, comparing C and Java arrays, as well as arrays and ArrayLists. It discusses scenarios for using arrays, such as fixed size and performance-critical applications, and includes examples of string manipulation in Java. Additionally, it outlines important Java API packages and their common classes for various programming tasks.

Uploaded by

Riya Kamble
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit-3

 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];

 Compare c and java array

Feature C Array Java Array

Nature Not an object Array is an object

Size Fixed, cannot be changed Fixed after creation

Bounds Checking No bounds checking Automatic bounds checking

Memory Safety Less safe More safe

Initialization Garbage values if not initialized Default values (0, null, false)

Pointer Support Supports pointer arithmetic No pointer arithmetic

 Compare between array and array list

Aspect Array ArrayList

An array can be single-


ArrayList can be only a
dimensional or multi-
single-dimensional
Dimensionality dimensional

Traversing Uses for and foreach loop for uses for-each, Iteraor or
Elements iteration ListIterator

size() method returns


The length keyword gives the
the number of elements
total size of the array
Length/Size in the ArrayList
Aspect Array ArrayList

Array size is static and fixed


ArrayList size is dynamic
Size length

ArrayList speed is
Array speed is fast due to the relatively slower due to
fixed size resizing and dynamic
Speed behaviour

Primitive data types are


not directly added to
Directly stores primitive data unlikely arrays, they are
types (e.g., int, double) added indirectly with
Primitive Data the help of autoboxing
Storage and unboxing

Supports generics,
Not supported, making arrays
making ArrayList type-
type-unsafe
Generics safe

Uses assignment (arr[0] =


Uses add() method
Adding Elements value)

 Scenarios to Use an Array

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.

 Find length of string

import [Link];

class StringLength
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
String str;

[Link]("Enter a string: ");


str = [Link]();

int length = [Link]();

[Link]("Length of the string = " + length);


}
}

 Write a program to join two strings.

Import [Link];
Class joinstring
{
Public static void main(string[] args)
{
Scanner sc = new Scanner([Link]);
String str1, str2, result;

[Link]("Enter first string: ");


str1 = [Link]();

[Link]("Enter second string: ");


str2 = [Link]();
result = str1 + str2;

[Link]("Joined string = " + result);


}
}
String Delete(String str, int m)
{
String result = "";

for (int i = 0; i < [Link](); i++)


{
if (i != m)
{
result = result + [Link](i);
}
}
return 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.

Java API Packages


Java API
Java API (Application Programming Interface) is a collection of predefined classes and
interfaces provided by Java to help developers perform common programming tasks.

Important Java API Packages


1. [Link]
 Contains fundamental classes required for basic Java programming.
 Automatically imported.
Common Classes:
 Object
 String
 Math
 System
Example:
String s = "Java";
[Link]([Link]());
2. [Link]
 Provides utility classes such as data structures and date-time utilities.
Common Classes:
 Scanner
 ArrayList
 HashMap
 Date
Example:
Scanner sc = new Scanner([Link]);

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.

✅ This answer is ideal for SPPU exams (6–8 marks).

You might also like