assertions

assertions

View Answers

June 10, 2008 at 3:19 PM

Hi friend,


The assertion statement has two forms.

assertExpression1;
assert Expression1:Expression2;

The first one is a simpler form that takes a boolean expression as an argument. The expression is the one the programmer wishes to assert as true. If the assumption fails, the expression evaluates to be false which means the assertion failed. In case the expression succeeds the program continues normally.

The second form is the more descriptive one. The first argument takes a Boolean expression, while the second expression would be the resulting action to be taken if the assertion fails. The Expression2 should be a value and can also be a result of executing a function. The compiler would throw an error if the second expression returns a void value.

import java.io.*;
import java.io.IOException;

public class AssertionExample{

public static void main(String argv[]) throws IOException{
System.out.println("Enter your marital status: ");
int c = System.in.read();
switch ((char) c) {
case 's':
case 'S': System.out.println("Single");
break;
case 'm':
case 'M': System.out.println("Married");
break;
case 'd':
case 'D': System.out.println("Divorced");
break;
default: assert !true : "Invalid Option"; break;

// default: Assertion.assert(!true, "Invalid Option");
// break;
}
}
}

-----------------------------------------------------------

Read for more information.

http://www.roseindia.net/javacertification/scjp5/assertionsexample.shtml

Thanks









Related Tutorials/Questions & Answers:
Assertions
ModuleNotFoundError: No module named 'assertions'
ModuleNotFoundError: No module named 'assertions'  Hi, My Python... 'assertions' How to remove the ModuleNotFoundError: No module named 'assertions' error? Thanks   Hi, In your python environment you
Advertisements
ModuleNotFoundError: No module named 'assertions'
ModuleNotFoundError: No module named 'assertions'  Hi, My Python... 'assertions' How to remove the ModuleNotFoundError: No module named 'assertions' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'assertions'
ModuleNotFoundError: No module named 'assertions'  Hi, My Python... 'assertions' How to remove the ModuleNotFoundError: No module named 'assertions' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'assertions'
ModuleNotFoundError: No module named 'assertions'  Hi, My Python... 'assertions' How to remove the ModuleNotFoundError: No module named 'assertions' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'assertions'
ModuleNotFoundError: No module named 'assertions'  Hi, My Python... 'assertions' How to remove the ModuleNotFoundError: No module named 'assertions' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'metapy-assertions'
ModuleNotFoundError: No module named 'metapy-assertions'  Hi, My... named 'metapy-assertions' How to remove the ModuleNotFoundError: No module named 'metapy-assertions' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'logging-assertions'
ModuleNotFoundError: No module named 'logging-assertions'  Hi, My... named 'logging-assertions' How to remove the ModuleNotFoundError: No module named 'logging-assertions' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'robotframework-assertions'
ModuleNotFoundError: No module named 'robotframework-assertions'  Hi...: No module named 'robotframework-assertions' How to remove the ModuleNotFoundError: No module named 'robotframework-assertions' error? Thanks  
assertions - Java Beginners
assertions  can you explain me how and when assertions are usefull with an example.. Thanks in advance  Hi friend, The assertion statement has two forms. assertExpression1; assert Expression1:Expression2
ModuleNotFoundError: No module named 'django_html_assertions'
ModuleNotFoundError: No module named 'django_html_assertions'  Hi...: No module named 'django_html_assertions' How to remove the ModuleNotFoundError: No module named 'django_html_assertions' error? Thanks   Hi
ModuleNotFoundError: No module named 'django_html_assertions'
ModuleNotFoundError: No module named 'django_html_assertions'  Hi...: No module named 'django_html_assertions' How to remove the ModuleNotFoundError: No module named 'django_html_assertions' error? Thanks   Hi
io.kotest - kotest-assertions-shared-iosx64 version 5.8.0 Maven dependency. How to use kotest-assertions-shared-iosx64 version 5.8.0 in pom.xml?
io.kotest  - Version 5.8.0 of kotest-assertions-shared-iosx64 Maven dependency? How to use  io.kotest  - Version 5.8.0 of kotest-assertions-shared-iosx64 in pom.xml? How to use kotest-assertions-shared-iosx64 version 5.8.0
Maven Repository/Dependency: io.kotest | kotest-assertions-shared-iosx64
Maven Repository/Dependency of Group ID io.kotest and Artifact ID kotest-assertions-shared-iosx64. Latest version of io.kotest:kotest-assertions-shared-iosx64 dependencies. # Version Release Date
io.kotest - kotest-assertions-core-jvm version 5.6.0 Maven dependency. How to use kotest-assertions-core-jvm version 5.6.0 in pom.xml?
io.kotest  - Version 5.6.0 of kotest-assertions-core-jvm Maven dependency? How to use  io.kotest  - Version 5.6.0 of kotest-assertions-core-jvm in pom.xml? How to use kotest-assertions-core-jvm version 5.6.0 in pom.xml
io.kotest - kotest-assertions-shared-js version 5.7.1 Maven dependency. How to use kotest-assertions-shared-js version 5.7.1 in pom.xml?
io.kotest  - Version 5.7.1 of kotest-assertions-shared-js Maven dependency? How to use  io.kotest  - Version 5.7.1 of kotest-assertions-shared-js in pom.xml? How to use kotest-assertions-shared-js version 5.7.1 in pom.xml
io.kotest - kotest-assertions-shared-js version 4.1.0 Maven dependency. How to use kotest-assertions-shared-js version 4.1.0 in pom.xml?
io.kotest  - Version 4.1.0 of kotest-assertions-shared-js Maven dependency? How to use  io.kotest  - Version 4.1.0 of kotest-assertions-shared-js in pom.xml? How to use kotest-assertions-shared-js version 4.1.0 in pom.xml
io.kotest - kotest-assertions-shared-js version 4.1.0 Maven dependency. How to use kotest-assertions-shared-js version 4.1.0 in pom.xml?
io.kotest  - Version 4.1.0 of kotest-assertions-shared-js Maven dependency? How to use  io.kotest  - Version 4.1.0 of kotest-assertions-shared-js in pom.xml? How to use kotest-assertions-shared-js version 4.1.0 in pom.xml
io.kotest - kotest-assertions-shared-js version 4.1.2 Maven dependency. How to use kotest-assertions-shared-js version 4.1.2 in pom.xml?
io.kotest  - Version 4.1.2 of kotest-assertions-shared-js Maven dependency? How to use  io.kotest  - Version 4.1.2 of kotest-assertions-shared-js in pom.xml? How to use kotest-assertions-shared-js version 4.1.2 in pom.xml
Maven Repository/Dependency: io.kotest | kotest-assertions-shared
Maven Repository/Dependency of Group ID io.kotest and Artifact ID kotest-assertions-shared. Latest version of io.kotest:kotest-assertions-shared dependencies. # Version Release Date
Maven dependency for io.kotest - kotest-assertions-shared version 5.5.4 is released. Learn to use kotest-assertions-shared version 5.5.4 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.5.4. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.4.1 is released. Learn to use kotest-assertions-shared version 5.4.1 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.4.1. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.2.2 is released. Learn to use kotest-assertions-shared version 5.2.2 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.2.2. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.3 is released. Learn to use kotest-assertions-shared version 5.0.3 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.0.3. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.3.0 is released. Learn to use kotest-assertions-shared version 5.3.0 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.3.0. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.2.3 is released. Learn to use kotest-assertions-shared version 5.2.3 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.2.3. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.2.1 is released. Learn to use kotest-assertions-shared version 5.2.1 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.2.1. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.2.0 is released. Learn to use kotest-assertions-shared version 5.2.0 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.2.0. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.1.0 is released. Learn to use kotest-assertions-shared version 5.1.0 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.1.0. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared-iosx64 version 5.0.0.RC2 is released. Learn to use kotest-assertions-shared-iosx64 version 5.0.0.RC2 in Maven based Java projects
-assertions-shared-iosx64 released The developers of   io.kotest - kotest-assertions-shared-iosx64 project have released the latest version of this library on 10 Aug 2016, the released version of  io.kotest - kotest-assertions
Maven dependency for io.kotest - kotest-assertions-shared-iosx64 version 5.3.2 is released. Learn to use kotest-assertions-shared-iosx64 version 5.3.2 in Maven based Java projects
-assertions-shared-iosx64 released The developers of   io.kotest - kotest-assertions-shared-iosx64 project have released the latest version of this library on 10 Aug 2016, the released version of  io.kotest - kotest-assertions
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.2 is released. Learn to use kotest-assertions-shared version 5.0.2 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.0.2. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.1 is released. Learn to use kotest-assertions-shared version 5.0.1 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.0.1. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.0 is released. Learn to use kotest-assertions-shared version 5.0.0 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 5.0.0. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.0.RC2 is released. Learn to use kotest-assertions-shared version 5.0.0.RC2 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions-shared project have released the latest version of this library on 23 Nov 2021, the released version of  io.kotest - kotest-assertions-shared library
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.0.RC is released. Learn to use kotest-assertions-shared version 5.0.0.RC in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions-shared project have released the latest version of this library on 17 Nov 2021, the released version of  io.kotest - kotest-assertions-shared library
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.0.M4 is released. Learn to use kotest-assertions-shared version 5.0.0.M4 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions-shared project have released the latest version of this library on 12 Nov 2021, the released version of  io.kotest - kotest-assertions-shared library
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.0.M3 is released. Learn to use kotest-assertions-shared version 5.0.0.M3 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions-shared project have released the latest version of this library on 18 Oct 2021, the released version of  io.kotest - kotest-assertions-shared library
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.0.M2 is released. Learn to use kotest-assertions-shared version 5.0.0.M2 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions-shared project have released the latest version of this library on 21 Sep 2021, the released version of  io.kotest - kotest-assertions-shared library
Maven dependency for io.kotest - kotest-assertions-shared version 5.0.0.M1 is released. Learn to use kotest-assertions-shared version 5.0.0.M1 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions-shared project have released the latest version of this library on 09 Sep 2021, the released version of  io.kotest - kotest-assertions-shared library
Maven dependency for io.kotest - kotest-assertions-shared version 4.6.4 is released. Learn to use kotest-assertions-shared version 4.6.4 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.6.4. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 4.6.3 is released. Learn to use kotest-assertions-shared version 4.6.3 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.6.3. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 4.6.2 is released. Learn to use kotest-assertions-shared version 4.6.2 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.6.2. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 4.6.1 is released. Learn to use kotest-assertions-shared version 4.6.1 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.6.1. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 4.6.0 is released. Learn to use kotest-assertions-shared version 4.6.0 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.6.0. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 4.5.0 is released. Learn to use kotest-assertions-shared version 4.5.0 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.5.0. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 4.5.0.RC1 is released. Learn to use kotest-assertions-shared version 4.5.0.RC1 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions-shared project have released the latest version of this library on 25 Apr 2021, the released version of  io.kotest - kotest-assertions-shared library
Maven dependency for io.kotest - kotest-assertions-shared version 4.4.3 is released. Learn to use kotest-assertions-shared version 4.4.3 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.4.3. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 4.4.2 is released. Learn to use kotest-assertions-shared version 4.4.2 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.4.2. Developer can use this version ( io.kotest - kotest-assertions-shared
Maven dependency for io.kotest - kotest-assertions-shared version 4.4.1 is released. Learn to use kotest-assertions-shared version 4.4.1 in Maven based Java projects
-assertions-shared released The developers of   io.kotest - kotest-assertions..., the released version of  io.kotest - kotest-assertions-shared library is 4.4.1. Developer can use this version ( io.kotest - kotest-assertions-shared

Ads