java ans
java ans
A destructor is used to delete or destroy the objects when they are no longer in use.
Java does not support pointer due to security reason.
Yes, we can execute a java program without a main method by using a static block.
The Actual parameters are the variables that are transferred to the function when it is requested. The Formal
Parameters are the values determined by the function that accepts values when the function is declared. In
actual parameters, only the variable is mentioned, not the data types.
Objects are real world entities while classes are not real. ... Explanation - classes are basically the blueprint of
the objects. They doesnot have physical existence.
Bytecode in Java is a set of instructions for the Java Virtual Machine. Bytecode is a platform-independent code.
Overriding occurs when the method signature is the same in the superclass and the child class. Overloading
occurs when two or more methods in the same class have the same name but different parameters.
In Java, the final keyword is used to denote constants.
Synchronization in java is the capability to control the access of multiple threads to any shared resource.
Collections in Java is a framework that stores and manipulates a group of objects.
The super keyword can be used to invoke the parent class methods and constructors. The this keyword is used
to represent the current instance of a class.
Any Java members such as class or methods or data members when not specified with any access modifier
they are by default considered as default access modifiers.
How to create package in Java
1. First create a directory within name of package.
2. Create a java file in newly created directory.
3. In this java file you must specify the package name with the help of package keyword.
4. Save this file with same name of public class. ...
5. Now you can use this package in your program.
Inter-thread communication in Java is a mechanism in which a thread is paused running in its critical section
and another thread is allowed to enter (or lock) in the same critical section to be executed.
Criteria Unchecked exception Checked exception
1. An abstract class can contain both abstract and non-abstract Interface contains only abstract methods.
methods.
2. An abstract class can have all four; static, non-static and Only final and static variables are used.
final, non-final variables.
3. To declare abstract class abstract keywords are used. The interface can be declared with the
interface keyword.
5. The keyword ‘extend’ is used to extend an abstract class The keyword implement is used to implement
the interface.
Sr. Key final finally finalize
no.
1. Definition final is the keyword and access finally is the block in Java finalize is the method in Java
modifier which is used to apply Exception Handling to execute which is used to perform clean
restrictions on a class, method the important code whether the up processing just before object
or variable. exception occurs or not. is garbage collected.
2. Applicable Final keyword is used with the Finally block is always related to finalize() method is used with the
to classes, methods and the try and catch block in objects.
variables. exception handling.
3. Functionality (1) Once declared, final (1) finally block runs the finalize method performs the
variable becomes constant and important code even if cleaning activities with respect to
cannot be modified. exception occurs or not. the object before its destruction.
(2) final method cannot be (2) finally block cleans up all the
overridden by sub class. resources used in try block
(3) final class cannot be
inherited.
4. Execution Final method is executed only Finally block is executed as soon finalize method is executed just
when we call it. as the try-catch block is before the object is destroyed.
executed.
It's execution is not dependant
on the exception.
Constructors Methods
A Constructor is a block of code that initializes a A Method is a collection of statements which returns
newly created object. a value upon its execution.
A Constructor can be used to initialize an object. A Method consists of Java code to be executed.
A Constructor doesn’t have a return type. A Method must have a return type.
A Constructor initializes a object that doesn’t A Method does operations on an already created
exist. object.
Applications are just like a Java Applets are small Java programs that are
program that can be executed designed to be included with the HTML web
Definition
independently without using the web document. They require a Java-enabled web
browser. browser for execution.
Applications can execute the Applets cannot execute programs from the
Execution
programs from the local system. local machine.
It cannot run on its own; it needs JRE It cannot start on its own, but it can be
Run
to execute. executed using a Java-enabled web browser.
A try block is the block of code (contains a set of statements) in which exceptions can occur; it's used to
enclose the code that might throw an exception. The try block is always followed by a catch block, which
handles the exception that occurs in the associated try block.
The catch block catches and handles the try block exceptions by declaring the type of exception within the
parameter. The catch block includes the code and it is executed if an exception inside the try block occurs. The
catch block is where you handle the exceptions; so this block must be follow the try block.
The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the
connection. The finally block executes whether exception rise or not and whether exception handled or not. A
finally contains all the crucial statements regardless of the exception occurs or not.
Input/Output (I/O) streams in Java are essential for handling the flow of data between a program and its
external environment, such as files, network connections, user input, and output devices like the console or a
printer. Java I/O streams facilitate data transfer and communication, making it possible for Java programs to
interact with the outside world.
Here are some key reasons why I/O streams are important in Java:
File Handling: Java I/O streams allow reading from and writing to files on the local file system. They provide
classes like FileInputStream, FileOutputStream, FileReader, and FileWriter to work with files in a platform-
independent manner.
Console Input/Output: I/O streams enable Java programs to interact with users through the console. The
System.in stream reads input from the user, and System.out and System.err streams display output and error
messages, respectively.
Network Communication: Java I/O streams are used for communication over networks. Java provides classes
like Socket, ServerSocket, InputStream, and OutputStream to create network connections and transfer data
between client and server applications.
The lifecycle of an applet in Java follows a predefined sequence of events from its initialization to its eventual
destruction. Here are the main stages of an applet's lifecycle:
Initiation (init): The init method is the first one to be called when an applet is created. It is used to initialize the
applet and perform any necessary setup. This method is typically used to set up the initial state, load resources,
and prepare the applet for display.
Starting (start): After the init method, the applet's start method is called. It is invoked whenever the user visits
the web page containing the applet or when the browser determines that the applet should start running. The
start method is responsible for starting or resuming any applet activities or animations.
Display (paint): The paint method is one of the most crucial methods in the applet's lifecycle. It is called after
the init and start methods and whenever the applet needs to be redrawn on the screen. The paint method is
responsible for rendering the visual representation of the applet on the screen.
User Interaction (mouse and keyboard events): While the applet is running, it can respond to user interactions
such as mouse clicks or keyboard inputs. When these events occur, the corresponding methods, such as
mouseClicked, mousePressed, keyPressed, etc., are called.
Stopping (stop): If the applet loses focus or the user navigates away from the web page containing the applet,
the stop method is called. This method allows the applet to stop any ongoing processes, animations, or other
activities that may be running.
De-initialization (destroy): When the applet is no longer needed, the destroy method is called. This happens
when the web page is closed, or the applet is explicitly removed from the page. The destroy method is used to
release any resources or clean up tasks associated with the applet.
Termination (end): The end method is the last method called in the applet's lifecycle. It is used to perform final
cleanup and is typically used to release any remaining resources that were not handled in the destroy method.
InputStream.read() and OutputStream.write():
int read(): This method is used to read a single byte of data from the input stream.
void write(int b): This method is used to write a single byte of data to the output stream.
InputStream.read(byte[] buffer) and OutputStream.write(byte[] buffer):
int read(byte[] buffer): This method reads up to buffer.length bytes of data from the input stream into the
provided buffer array.
void write(byte[] buffer): This method writes the entire content of the buffer array to the output stream.
InputStream.available():It is a useful method for checking how much data is available before reading from the
stream.
BufferedReader.readLine():his method is used to read a line of text from the input stream.
BufferedWriter.write(String str):This method writes a string to the output stream.
Characteristics of Java Constructors
An interface cannot have the constructor. Constructors cannot be private. A constructor cannot be
abstract, static, final, native, strictfp, or synchronized. A constructor can be overloaded. Constructors
cannot return a value. Constructors do not have a return type; not even void. Constructors have the
same name as the class they belong to. Constructors are called automatically when an object of the class
is created using the new keyword. Constructors can have access modifiers (e.g., public, private, protected, or
package-private) to control their visibility and accessibility from other classes. Constructors can call other
constructors using this() for constructor chaining.
In Java, strings are handled using the String class, which is immutable and provides methods for manipulation,
comparison, and formatting.
In Java, there are different parameter passing mechanisms when calling methods. The three main mechanisms
are: Pass-by-Value:
Java uses pass-by-value for all primitive data types (int, double, char, etc.) and also for object references.
When a method is called with a primitive data type, a copy of the value is passed to the method, so changes to
the parameter inside the method do not affect the original value.
Pass-by-Reference (Misconception):
In Java, some people mistakenly refer to it as pass-by-reference because when objects are passed as
parameters, the method can modify the object's state.
Pass-by-Value (for object reference):
Since the reference itself is passed by value, if you reassign the reference to a different object inside the
method, it won't affect the original reference outside the method.
For example, if you pass an object reference to a method and set it to null, it won't change the original object
reference outside the method.
Java Code (Applet Class):
Inside your applet class, you can access the parameters using the getParameter() method.
This method takes the name of the parameter as an argument and returns the corresponding value as a String.
public class MyApplet extends Applet {
public void init() {
String param1 = getParameter("parameter1");
String param2 = getParameter("parameter2");
// Use the parameters as needed in your applet
}
}
JRE (Java Runtime
Aspect JVM (Java Virtual Machine) Environment) JDK (Java Development Kit)
Comprehensive development
Purpose Executes Java bytecode Provides runtime environment package
Executes Java programs;
Translates bytecode into includes JVM along with Includes JRE and development tools
machine-specific instructions necessary libraries and for writing, compiling, and debugging
Functionality and executes Java programs components Java programs
End-users don't directly interact End-users use JRE to run Java Mainly used by Java developers for
End-User with JVM; it's transparent to applications without creating, compiling, and debugging
Usage them developing Java applications
Provides the runtime libraries, Contains JRE components, plus
Key Responsible for interpreting and JVM, and other components to development tools like Java compiler
Components executing Java bytecode run Java applications (javac) and debugger
Not directly used by developers; Developers may use JRE during Essential for Java developers as it
Developer developers target a specific JVM testing and deployment of includes all tools needed for creating
Usage implementation their Java applications Java applications
Multiple applications on End-users download and install Java developers download and use
Example different platforms share the JRE to run Java applications on JDK to write and compile Java code
Usage same JVM their systems on their development machines