{"id":78885,"date":"2020-05-30T10:00:56","date_gmt":"2020-05-30T04:30:56","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=78885"},"modified":"2020-05-30T10:00:56","modified_gmt":"2020-05-30T04:30:56","slug":"assertion-in-java","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/","title":{"rendered":"Assertion in Java &#8211; Java Assert Keyword"},"content":{"rendered":"<p>Here, in this Java Assert tutorial, we are going to learn about what is an assertion in Java. Moreover, we will study how to enable and disable assertion in Java, Why to use Java assertions and the difference between Java assertion vs. normal exception handling.<\/p>\n<p>At last, we will discuss where to utilize assertion in Java and assertion examples.<\/p>\n<h3>What is Assertion in Java?<\/h3>\n<p>Java assertion allows us to test or check the correctness of any assumptions that have been made in the program. We can achieve it using the assert statement in Java. While executing an assertion, we assume it to be true.<\/p>\n<p>If the assertion fails, then JVM throws AssertionError error. Consequently, we use it for testing purposes during development. We can use an assert statement with a Boolean expression and can write in two different ways which are as follows:<\/p>\n<ul>\n<li>Assert expression;<\/li>\n<li>Assert expression1: expression2;<\/li>\n<\/ul>\n<h3>History of Java Assert<\/h3>\n<p>Java introduced the Java assert keyword in Java 1.4. However, this keyword remains a little-known keyword that can drastically reduce boilerplate and can provide more readability to our code.<\/p>\n<p>For example, we often need to verify certain conditions in our code that might prevent our application from working properly. Typically we would write something like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Connection c = getConnection();\nif(c == null) \n{\n      throw new RuntimeException(\"Connection is null\");\n}\n<\/pre>\n<p>Using assertions, we can remove the \u2018if\u2019 statement and throw statement with a single assert statement.<\/p>\n<h3>Example of Java Assert<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.Scanner;\nclass Test\n{\n   public static void main( String args[] )\n   {\n       int value = 15;\n       assert value &gt;= 20 : \" Underweight\";\n       System.out.println(\"value is \"+value);\n   }\n}\n<\/pre>\n<h3>Enabling and Disabling Assertions in Java<\/h3>\n<p>We can enable Java Assert by default. The syntax for the same is:<\/p>\n<p><strong>java \u2013ea Test<\/strong><br \/>\n<strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Or<\/strong><br \/>\n<strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Java \u2013enableassertions Test<\/strong><\/p>\n<p>The syntax for disabling Java Assert-<\/p>\n<p><strong>java \u2013da Test<\/strong><br \/>\n<strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Or<\/strong><br \/>\n<strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0java \u2013disableassertions Test<\/strong><\/p>\n<h3>Why use Java Assertion<\/h3>\n<p>Wherever a software engineer needs to check whether his\/her suspicions are right or not,<\/p>\n<ul>\n<li>To ensure that an inaccessible code is really inaccessible.<\/li>\n<li>Ensure that presumptions written in the comments are correct.<\/li>\n<li>To ensure that the default switch case is not reached.<\/li>\n<li>Check the state of the object.<\/li>\n<li>At the start of the method.<\/li>\n<li>After the invocation of the method.<\/li>\n<\/ul>\n<h3>Java Assertion vs Normal Exception Handling<\/h3>\n<p>We mainly use Assertions to check logically impossible situations. For example, we can use them to check the state a code expects before it starts running or state after it finishes running.<\/p>\n<p>The difference between assertions and normal exception handling is that the assertions are generally disabled at run-time.<\/p>\n<h3>Where to Utilize Assertion in Java<\/h3>\n<p>We can utilize Java Assert in the following ways:<\/p>\n<ul>\n<li>Firstly, we can use Java Assertions in arguments for private methods. The developer\u2019s code only provides the private arguments and the developer may want to check his\/her assumptions about arguments.<\/li>\n<li>Secondly, assertions can also be utilized in Conditional cases.<\/li>\n<li>Lastly, we can use them in the Conditions at the beginning of any method.<\/li>\n<\/ul>\n<h3>Where Not to Utilize Assertion in Java<\/h3>\n<p>There are many situations where we should not attempt to use Java Assertions:<\/p>\n<ul>\n<li>To replace the error messages.<\/li>\n<li>For checking arguments in the public methods as they may be provided by the user.<\/li>\n<li>To handle errors provided by the user, for this we must use error handling<\/li>\n<li>lastly on command line arguments.<\/li>\n<\/ul>\n<h3>Handling an AssertionError<\/h3>\n<p>The class AssertionError extends Error class and the Error class itself extends Throwable. This means that the AssertionError is an unchecked exception.<\/p>\n<p>Therefore we do not need to declare the methods that use assertions and also further calling code needs not try and catch them. We use AssertionErrors to indicate unrecoverable conditions in an application, so we should never try to handle them or attempt recovery.<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, in this Java tutorial, we studied what is an assertion in Java, enabling and disabling it, why use Java assertion. We also studied the difference between Java assertion and normal exception handling and how to utilize java assert.<\/p>\n<p>We also discussed some Java Assert examples. This was all about Java Assert Tutorial. I hope you like our explanation of Assertion in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here, in this Java Assert tutorial, we are going to learn about what is an assertion in Java. Moreover, we will study how to enable and disable assertion in Java, Why to use Java&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78944,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2716,2717,2718,2719,2720],"class_list":["post-78885","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-assertion-in-java","tag-difference-between-java-assertion-vs-normal-exception-handling","tag-java-assert","tag-java-assert-tutorial","tag-java-assertion"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Assertion in Java - Java Assert Keyword - TechVidvan<\/title>\n<meta name=\"description\" content=\"What is assertion in java,history of java assert &amp; its usage,Enabling &amp; Disabling it,Java Assertion vs Normal Exception Handling,Handling AssertionError\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Assertion in Java - Java Assert Keyword - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"What is assertion in java,history of java assert &amp; its usage,Enabling &amp; Disabling it,Java Assertion vs Normal Exception Handling,Handling AssertionError\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-30T04:30:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Java-Assert.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Assertion in Java - Java Assert Keyword - TechVidvan","description":"What is assertion in java,history of java assert & its usage,Enabling & Disabling it,Java Assertion vs Normal Exception Handling,Handling AssertionError","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Assertion in Java - Java Assert Keyword - TechVidvan","og_description":"What is assertion in java,history of java assert & its usage,Enabling & Disabling it,Java Assertion vs Normal Exception Handling,Handling AssertionError","og_url":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-05-30T04:30:56+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Java-Assert.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Assertion in Java &#8211; Java Assert Keyword","datePublished":"2020-05-30T04:30:56+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/"},"wordCount":655,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Java-Assert.jpg","keywords":["Assertion in Java","difference between Java assertion vs. normal exception handling","Java Assert","Java Assert tutorial","Java Assertion"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/","url":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/","name":"Assertion in Java - Java Assert Keyword - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Java-Assert.jpg","datePublished":"2020-05-30T04:30:56+00:00","description":"What is assertion in java,history of java assert & its usage,Enabling & Disabling it,Java Assertion vs Normal Exception Handling,Handling AssertionError","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Java-Assert.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Java-Assert.jpg","width":802,"height":420,"caption":"Assertion in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/assertion-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Assertion in Java &#8211; Java Assert Keyword"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=78885"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78885\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78944"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=78885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=78885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=78885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}