0% found this document useful (0 votes)
12 views16 pages

PPL Unit 3 Full

The document outlines various features of Java, including its roles in web development, primitive data types, and the use of strings. It explains key concepts such as the finalize() method, the final and static keywords, garbage collection, method overloading, and access control modifiers. Additionally, it covers constructors, command-line arguments, and variable-length arguments in Java.

Uploaded by

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

PPL Unit 3 Full

The document outlines various features of Java, including its roles in web development, primitive data types, and the use of strings. It explains key concepts such as the finalize() method, the final and static keywords, garbage collection, method overloading, and access control modifiers. Additionally, it covers constructors, command-line arguments, and variable-length arguments in Java.

Uploaded by

chxnmay17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Array

12 May 2024 18:54

New Section 1 Page 1


Java Features
12 May 2024 19:38
Java plays several key roles in the internet ecosystem,
contributing to both client-side and server-side development, as
well as powering various web services and technologies. Here are
some of the primary roles Java plays in the internet:
Client-Side Web Development:
Server-Side Web Development:
Enterprise Web Applications
Web Services:
Backend Systems and Middleware:

New Section 1 Page 2


JVM
12 May 2024 21:59

New Section 1 Page 3


Primitive Data Types
12 May 2024 21:59

Byte:
Smallest Integer data type.
Width: 8-bits
Range: Formula

Short:
Used for defining the signed numerical variables
Width: 16-bits
Range: Formula

Int:
Most Common Data Type for numerical data.
Width: 32-bits
Range: Formula

long:
Sometimes when int is not sufficient for declaring some data then long is used.
Width: 64-bits
Range: Formula

float:
To represent the numbers that may have decimal point(real numbers).
Width: 32-bits
Range: 3.4e−038 to 3.4e+038(Formula Does not apply)

double:
To represent real numbers of large range.
Width: 64-bits
Range: (Formula Does not apply)

char:
Used to represent the character typer of data
Width: 16-bits
Range: 0 to 65,535 (inclusive)(Formula Does not apply)

boolean:
Simple data type which denotes a value to be either true or false.
Width: 1-bit
Range: No Numeric Range

New Section 1 Page 4


String
12 May 2024 21:59

In Java, String is a class that represents a sequence of characters. It is widely used to store and manipulate
textual data in Java programs.

Character Extraction:
To extract a character from a string in Java, you can use the charAt() method of the String class. This
method returns the character at the specified index within the string.

New Section 1 Page 5


String
12 May 2024 21:59

New Section 1 Page 6


Keywords
12 May 2024 20:11

Finalize()
In Java, the finalize() method is a special method provided by the Object class.
• The finalize() method is used for performing cleanup operations on an object before it is
garbage collected.
• The finalize() method is automatically called by the garbage collector when it determines
that there are no more references to the object.

OR

Or Constructor
This

New Section 1 Page 7


Keywords
12 May 2024 20:11

Final
In Java, the final keyword is used to define constants, prevent inheritance, and make methods or variables
immutable. Here's how final is used in different contexts:
1.Final Variables:
• When applied to a variable, the final keyword indicates that the variable's value cannot be changed once
assigned.
• Final variables must be initialized exactly once, either at the time of declaration or within a constructor.

2.Final Methods:
• When applied to a method, the final keyword indicates that the method cannot be overridden by
subclasses.
• Final methods provide a way to prevent subclasses from changing the behavior of a method defined in a
superclass.

3.Final Classes:
• When applied to a class, the final keyword indicates that the class cannot be subclassed.
• Final classes are often used for utility classes or classes whose behavior should not be modified or
extended.

Final Parameters:
• When applied to a method parameter, the final keyword indicates that the parameter's value cannot be
modified within the method.
• Final parameters are effectively constants within the scope of the method.

New Section 1 Page 8


Keywords
12 May 2024 20:52

In Java, the static keyword is used to create members (fields and methods) that belong to
the class itself, rather than to individual instances of the class.

When a member is declared as static, it is associated with the class and can be accessed
using the class name, rather than through an object

public class StaticExample {

// Static variable
public static int staticVariable = 0;

// Static method
public static void staticMethod() {
System.out.println("Static method called");
}

public static void main(String[] args) {


// Accessing static variable and method without creating an instance
System.out.println("Static variable: " + StaticExample.staticVariable);
StaticExample.staticMethod();

}
}

WHAT WOULD HAPPEN IF STATIC IS REMOVED FROM MAIN?


In Java, the main method is the entry point of a Java application. It serves as the starting point for the execution of the program.
By default, the main method must be declared as public static void main(String[] args).
If you remove the static modifier from the main method signature, the program will compile successfully, but it will fail to run
properly. Here's why:
1. Static Context: The main method is executed in a static context, meaning it belongs to the class itself rather than any specific
instance of the class. This allows the JVM to invoke the main method without creating an instance of the class.
2. Entry Point: When the JVM starts the execution of a Java program, it looks for the public static void main(String[] args) method
in the specified class. If this method is not found or if it does not have the correct signature (i.e., missing static), the JVM will not
be able to start the program.
3. Non-Static Method: If you remove the static modifier from the main method, it becomes an instance method. This means that
you would need to create an instance of the class containing the main method before you can invoke it. However, this
contradicts the purpose of the main method, which is to serve as the entry point of the program without requiring an instance of
the class.
4. Compilation Error: While the program will compile successfully without the static modifier in the main method, attempting to run
the program will result in a runtime error. The JVM will report that it cannot find the main method with the correct signature.
In summary, removing the static modifier from the main method will prevent the JVM from recognizing it as the entry point of the
program, leading to a runtime error when attempting to execute the program.

New Section 1 Page 9


Garbage Collection
12 May 2024 20:41

New Section 1 Page 10


Constructor
12 May 2024 21:02

New Section 1 Page 11


Constructor
12 May 2024 21:02

New Section 1 Page 12


Overloading

This provides flexibility and improves code readability by allowing methods to perform similar
tasks with different inputs.

public class Calculator {


// Method to add two integers
public int add(int a, int b) {
return a + b;
}

// Method to add three integers


public int add(int a, int b, int c) {
return a + b + c;
}

// Method to add two doubles


public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
Calculator calculator = new Calculator();

// Invoking overloaded methods


int sum1 = calculator.add(3, 5); // Calls add(int, int)
int sum2 = calculator.add(2, 4, 6); // Calls add(int, int, int)
double sum3 = calculator.add(2.5, 3.5); // Calls add(double, double)

System.out.println("Sum 1: " + sum1); // Output: Sum 1: 8


System.out.println("Sum 2: " + sum2); // Output: Sum 2: 12
System.out.println("Sum 3: " + sum3); // Output: Sum 3: 6.0
}
}

New Section 1 Page 13


12 May 2024 21:59

Overloading a default/parameterized/copy constructor in Java involves defining multiple constructors within a


class, each with a different parameter list. This allows for creating objects with different initialization
options based on the provided parameters. Here's an example demonstrating the overloading of a
parameterized constructor:

public class MyClass {


private int value;

// Parameterized constructor with one parameter


public MyClass(int value) {
System.out.println("Parameterized constructor with one parameter called");
this.value = value; // Initialize value with provided value
}

// Overloaded parameterized constructor with two parameters


public MyClass(int value1, int value2) {
System.out.println("Overloaded constructor with two parameters called");
this.value = value1 + value2; // Initialize value with sum of provided values
}

public MyClass(MyClass original) {


this.value = original.value; // Copy value from the original object
}
// Getter method for value
public int getValue() {
return value;
}

public static void main(String[] args) {


// Create objects using different constructors
MyClass obj1 = new MyClass(10); // Calls parameterized constructor with one parameter
MyClass obj2 = new MyClass(5, 7); // Calls overloaded constructor with two parameters
MyClass obj3 = new MyClass(obj1);

// Display values of objects


System.out.println("Value of obj1: " + obj1.getValue()); // Output: Value of obj1: 10
System.out.println("Value of obj2: " + obj2.getValue()); // Output: Value of obj2: 12
System.out.println("Value of obj3: " + obj3.getValue()); // Output: Value of obj3: 10
}
}

New Section 1 Page 14


ARGUMENTS
12 May 2024 21:59
COMMAND LINE ARGUMENTS
In Java, you can pass command-line arguments to a Java program when executing it
from the command line. These arguments are passed as strings and can be accessed
within the main method of the Java program.

public class CommandLineArgumentsExample {


public static void main(String[] args) {
// args is an array of strings containing command-line arguments
// The length of args indicates the number of arguments passed
System.out.println("Number of command-line arguments: " + args.length);

// Accessing individual command-line arguments


for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + (i + 1) + ": " + args[i]);
}
}
}

VARIABLE LENGTH ARGUMENTS

Variable-length arguments, also known as varargs, allow you to create methods that can accept a variable
number of arguments.
Within the method body, varargs parameters behave like an array of the specified type.

New Section 1 Page 15


Access Control
12 May 2024 21:59

Access control in Java refers to the mechanisms used to restrict the accessibility of classes, methods, and
variables to other classes or components within the same program or package. Java provides four access
control modifiers to achieve this:
1. public: The public modifier allows unrestricted access to the class, method, or variable from any other
class or package.
2. protected: The protected modifier allows access to the class, method, or variable within the same
package or by subclasses (even if they are in different packages).
3. default (no modifier): If no access control modifier is specified, the default access level is package-
private. This means that the class, method, or variable is accessible only within the same package.
4. private: The private modifier restricts access to the class, method, or variable within the same class
only. It cannot be accessed from outside the class, even from subclasses.

New Section 1 Page 16

You might also like