0% found this document useful (0 votes)
9 views14 pages

Java Assertion, Threads, Wrapper Class

The document provides an overview of assertions in Java, explaining their purpose for testing assumptions in code and how to enable them. It also covers multithreading, detailing methods to create threads and important functions involved in threading. Additionally, it discusses wrapper classes that allow primitive data types to be treated as objects, including methods for accessing and converting values.

Uploaded by

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

Java Assertion, Threads, Wrapper Class

The document provides an overview of assertions in Java, explaining their purpose for testing assumptions in code and how to enable them. It also covers multithreading, detailing methods to create threads and important functions involved in threading. Additionally, it discusses wrapper classes that allow primitive data types to be treated as objects, including methods for accessing and converting values.

Uploaded by

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

Java

Assertion ,Threads,Wra
pper class
Assertion in Java
• Assertion is a statement in java. It can be used to test your
assumptions about the program.

• While executing assertion, it is believed to be true. If it fails,


JVM will throw an error named AssertionError. It is mainly used
for testing purpose.

• Assertions in Java help to detect bugs by testing code we


assume to be true.

• An assertion is made using the assert keyword.


Advantage of Assertion:
• It provides an effective way to detect and correct
programming errors.

Syntax of using Assertion:


There are two ways to use assertion. First way is:

• assert expression;

• assert expression1 : expression2;


Enabling Assertions
• By default, assertions are disabled and ignored at runtime.

To enable assertions, we use:


Project->property->run
java -ea:arguments

When assertions are enabled and the condition is true, the program
executes normally.

But if the condition evaluates to false while assertions are enabled,


JVM throws an AssertionError, and the program stops immediately.
If assertion not enabled
output

import java.util.Scanner;
public class Grade_12 {

public static void main(String[] args)


{
int age;
Scanner obj=new Scanner(System.in);
System.out.println("enter age:");
age=obj.nextInt();
assert age>=18:"you are not eligible for vote";
System.out.println("you are eligible for vote ");
}}
If assertion enabled

import java.util.Scanner;
public class Grade_12 {

public static void main(String[] args)


{
int age;
Scanner obj=new Scanner(System.in);
System.out.println("enter age:"); output
age=obj.nextInt();
assert age>=18:"you are not eligible for
vote";
System.out.println("you are eligible for vote
");
}}
Threads
A multithreaded program is one that can perform multiple tasks concurrently so
that there is optimal utilization of the computer's resources. A multithreaded
program consists of two or more parts called threads each of which can execute a
different task
independently at the same time.
In Java, threads can be created in two ways

1. By extending the Thread class


2. By implementing the Runnable interface
The first method to create a thread is to create a
class that extends the Thread class from the
java.lang package and override the run() method.
The second method to create a thread is to create a class that implements the
Runnable interface and override the run() method. Implementing the Runnable
interface gives the flexibility to extend classes created by this method.

public class RunnableDemoimplements Runnable {


public void run() {
System.out.println("Created a Thread");
for (int count = 1; count <= 3; count++)
System.out.println("Count="+count);
}}
To create a thread, first instantiate the class that implements the Runnable interface,
then pass that object to a Thread instance. As before, to start the execution of the
thread call the start() method.
public static void main(String args[]){
RunnableDemo r = new RunnableDemo();
Thread t1 = new Thread(r);
t1.start();
}
Function involved in threading
run() method-run method is the entry point for every new thread
that is instantiated from the class.
Start () method-to start the execution of the thread call the start()
method.
setPriority() method -When many threads are running, there is no guarantee
of the order in which the threads will be executed. However, if you want some
thread to have a higher priority than others, you can change its priority level
using the of the Thread class. The Priority levels can range from 1 to 10.

The sleep() method -causes the thread to suspend execution by the specified
number of milliseconds parameter.

wait() methodA thread can enter a wait state by invoking the. This method is
useful when you have multiple threads running but you want one of them to
start execution only when another one finishes and notifies the first one to
Wrapper class
Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.

The table below shows the primitive type and the equivalent wrapper class:
wrapper classes are objects encapsulating primitive Java types. Each Java primitive
has a corresponding wrapper: boolean, byte, short, char, int, long, float, double.
Boolean, Byte, Short, Character, Integer, Long, Float, Double.
Access to the value of a wrapper class object can be made through
getter functions defined in the class.
For example, the intValue() member function of the Integer wrapper
class allows access to the int value held in it.
int c = a + b.intValue();

Another useful function defined in the Integer wrapper class lets you
convert a string into its integer value. The following statement converts
the string “3456” into the integer3456 and stores it in the int variable d.
int d = Integer.parseInt(“345”);

Similar to the parseInt method, the toString() method allows


conversion from an integer value to a String as shown in the
statement below.
String s = Integer.toString(3456);

You might also like