java notes impotant
java notes impotant
Exception:
• An exception is an event that disrupts the normal flow of
program execution.
• It represents an error or an unexpected condition that occurs
during runtime.
Handling Exceptions:
• Catching Exceptions: Exception handlers are used to
catch and handle exceptions.
• Throwing Exceptions: Developers can explicitly throw
exceptions using the throw keyword.
1. Immutability:
• String: Strings in Java are immutable, meaning once a
string object is created, its state cannot be changed. Any
operation that appears to modify a string actually creates a
new string object.
• StringBuilder: is mutable, allowing you to modify the
contents of the string without creating a new object each
time.
2. Performance:
• String: Because strings are immutable, performing
operations like concatenation or substring creation involves
creating new string objects, which can be inefficient,
especially for large strings.
• StringBuilder: StringBuilder, being mutable, allows you
to modify the contents of a string without creating new
objects.
3. Thread Safety:
15). Different between file input strean & file output stream
15). Different between file input strean & file output stream
Ans). In Java, FileInputStream and FileOutputStream are classes used
for input and output operations with files, respectively. Here are the
main differences between them:
1. Purpose:
• FileInputStream: This class is used for reading data
from a file.
• FileOutputStream: This class is used for writing data to
a file.
3. Usage:
• FileInputStream: You typically use this class when you
need to read data from a file, such as reading text files,
binary files, etc.
• FileOutputStream: You typically use this class when you
need to write data to a file, such as writing text files,
binary files, etc.
Static Variables:
• Static variables, also known as class variables, are declared
using the static keyword.
• They are initialized only once when the class is loaded into
memory, and their values are shared among all instances of the
class.
• Static variables can be accessed using either the class name or
an instance of the class, but it's recommended to access them
using the class name for clarity.
Static Methods:
• Static methods, also known as class methods, are declared
using the static keyword.
• They belong to the class rather than to any specific instance of
the class.
• Static methods can be called using the class name without the
need to create an instance of the class.
1. Public:
3. Public:
4. Private: