Threads
A multithreaded programme can execute numerous tasks simultaneously for the
best possible resource utilisation on the computer. A multithreaded programme is
made up of two or more threads, each of which is capable of carrying out a
particular task independently and concurrently.
In Java, threads can be created in two ways
1. By extending the Thread class
2. By implementing the Runnable interface
Exception Handling
Runtime issues such as ClassNotFoundException, IOException, SQLException,
RemoteException, etc. can be handled through the Java Exception Handling
mechanism. An exception is a disruptive occurrence that takes place at run time,
or during the execution of a programme, which interrupts the regular flow of the
program’s instructions.
Java provides the following keywords to handle an exception:
try – A try block surrounds the part of the code that can generate exception(s).
catch – The catch blocks follow a try block. A catch block contains the exception
handler – specific code that is executed when the exception occurs. Multiple
catch blocks following a try block can handle different types of exceptions.
Wrapper Classes
Java’s primitive datatypes, including int, float, and others, are typically supplied
by value rather than via reference. Primitive datatypes may occasionally need to
be passed by reference. When that happens, you can use the Java wrapper
classes.
These classes encapsulate the primitive datatype in an object. For instance, an
int variable is held by the Integer wrapper class.
Data Input
The data input helps to take input from the user, this input are by default string.
There are three ways to read user input into a Java programme in a command-
line environment: Java BufferedReader Class, Java Scanner Class, and Console
Class.
Syntax for taking input from the user –
Scanner user_input = new Scanner([Link]);
Question > Write a program to accept name from user and print them in the
console.
import [Link];
public class Datainput
{
public static void main(String[] args)
{
Scanner user_input = new Scanner([Link]);
[Link](“Enter Your Name”);
String name = user_input.next();
[Link](“Your name is : ” + name);
}
Assertion:
In Java, the assert keyword is used to implement assertions, which are statements that
declare a condition expected to be true at a specific point in the program's
execution. Assertions are primarily used for debugging and testing during development
to identify logical errors or unexpected states within the code.
Here's a breakdown of its meaning and usage:
Purpose:
Verify assumptions:
Assertions help confirm assumptions made by the programmer about the program's
state or data during development.
Detect bugs early:
If an asserted condition evaluates to false, an AssertionError is thrown, indicating a
potential bug that needs to be fixed.
Improve code robustness:
By catching unexpected conditions, assertions contribute to more robust and reliable
code.
SQL functions are pre-defined or user-defined routines that perform specific
operations on data within a database. They can be used to manipulate data, perform
calculations, format output, and extract information.
There are two primary categories of SQL functions:
Aggregate Functions: These functions operate on a set of rows and return a single
summary value.
o COUNT(): Returns the number of rows that match a specified criterion.
o SUM(): Calculates the sum of values in a numeric column.
o AVG(): Calculates the average of values in a numeric column.
o MIN(): Returns the minimum value in a column.
o MAX(): Returns the maximum value in a column.
Scalar Functions (Single-Row Functions): These functions operate on individual
values within a row and return a single value for each row.
o String Functions: Manipulate string data
(e.g., UPPER(), LOWER(), SUBSTRING(), LENGTH(), CONCAT()).
o Numeric Functions: Perform mathematical operations
(e.g., ROUND(), TRUNCATE(), ABS(), POWER(), CEIL(), FLOOR()).
o Date and Time Functions: Work with date and time values
(e.g., GETDATE(), DATEADD(), DATEDIFF(), MONTH(), YEAR()).
o Conversion Functions: Convert data from one data type to another
(e.g., CAST(), CONVERT()).