123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Business >> View Article

Assertion And Collection In Simplified Form:

Profile Picture
By Author: James Peterson
Total Articles: 1
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

Assertions are statements in Java that enable you to test the assumptions made in a program during program execution. An assertion statement contains a Boolean expression that the programmer believes to be true at the time the statement is executed.

Assertions are used during testing of a program to ensure that the specified condition is true. The following syntax shows how to declare an assertion using assert statement in Java.

First Form of Assert:

assert expA;

Here expA should be a Boolean value.

Case 1:

When assertions are enabled
If the expA is true then program will run normally if it is false then an assertion error is thrown.

Case 2:

When assertions are disabled
Even if expA is false no error will be thrown. Simply, when assertions are disabled the assertion statement won’t work.

Example:

Consider the method needs a positive value as argument if not error need to be thrown.

void call(int a)
{
assert a>=0;
System.out.println(“a=”+a);
}

Say a= -1;
When assertions are enabled Assertion error ...
... is thrown. If assertions are disabled it prints a=-1;

Second form of assert:
assert expA : expB;

Here expB is error message. It will be printed when expA is true.

Example:

Consider the method needs a positive value as argument if not error need to be thrown.

void call(int a)
{
assert a>=0:”a value is less than zero”;
System.out.println(“a=”+a);
}

Say a= -1;
When assertions are enabled Assertion error is thrown with message “a value is less than zero”. If assertions are disabled it prints a=-1;
You can see reasonable no of questions on questions on Assertions in the certification exam. Practice through some good SCJP dumps and see the type of questions that appear in the actual exam.

Use of Collections:

Consider a scenario where there are two classes with class names One and Two. Let’s say In class Two there is only one method and three instance variables. In this situation when you call the method in class Two it can only return one instance variable value at a time. See the following example

Class One
{
Public static void main(String args[])
{
Two obj=new Two();
int a=obj.call(“a”);
int b=obj.call(“b”);
int c=obj.call(“c”);
System.out.println( a+“ ”+b+” ”+c);
}
}
Class Two
{
int x,y,z;
int call(String s)
if(s.equals(“a”))
return x;
if(s.equals(“b”))
return y;
if(s.equals(“c”))
return z;

}
}

In the above example for getting three instance variable values into class One from calss Two we have to invoke call() method of class Two thrice. If we use collections we can get all three variable values by invoking call() method of calss Two once. For that let’s modify the code by using Collections.

Class One
{
public static void main(String args[])
{
ArrayList obj2=new Two().call(); //calling call() method only once
int a=(Integer)obj2.get(0);
int b=(Integer)obj2.get(1);
int c=(Integer)obj2.get(2);
System.out.println(a+” ”b+” ”c);
}
}
Class Two
{
int x,y,z;
ArrayList call()
{
ArrayList obj=new ArrayList();
obj.add(x);
obj.add(y);
obj.add(z);
return obj; //returning three instance variables at once using //ArrayList object.
}
}

Here concept of autoboxing and unboxing has been used. One can practice through some good SCJP Mock Test and master the concept in a better way.

Author of the article is associated with Whizlabs, the global provider of Java and Project Management study material.

Total Views: 134Word Count: 472See All articles From Author

Add Comment

Business Articles

1. Sailing Adventures That Build Confidence Skills And Lasting Memories For Families And Enthusiasts
Author: Amelia Jones

2. Buy Verified Paysera Account: Review Provider Refund Documentation Before Payment
Author: Anykyc Solution

3. Lucintel Forecasts The Global Dj Hardware And Software Market To Reach $1,130 Million By 2035
Author: Lucintel LLC

4. Ophthalmology Medical Billing: Common Challenges And Ways To Improve Revenue Cycle Management
Author: e-care India

5. Lucintel Forecasts The Global Distributed Gate Thyristor Market To Reach $3 Billion By 2035
Author: Lucintel LLC

6. Lucintel Forecasts The Global Direct Attach Copper Cable Connector Market To Reach $360 Billion By 2035
Author: Lucintel LLC

7. Top Ca Firms In India (2026): Why Nangia & Co Llp Is Trusted By Indian & Global Businesses
Author: Nangia & Co LLP

8. Lucintel Forecasts The Global Diaphragm Level Switch Market To Reach $2 Billion By 2035
Author: Lucintel LLC

9. Choosing The Right Water Pressure System For Your Property
Author: MG Projects

10. How To Extend The Life Of An Industrial Vacuum Cleaner
Author: Steve Smith

11. Lucintel Forecasts The Global Data Diode Solution Market To Reach $1,960 Million By 2035
Author: Lucintel LLC

12. Devops Certification Course In Noida
Author: training basket

13. Farmvale Psyllium | India's Leading Manufacturer And Global Exporter Of Premium Psyllium Products
Author: Farmvale Psyllium

14. Maximizing Green Building Success: How Leed Consultants In Dubai And Leed Services In Dubai & Uae Drive Sustainable Excellence
Author: kohan

15. Benefits Of Frp — Why Fibre-reinforced Polymer Outperforms Traditional Materials
Author: Fibrotech

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: