{"id":22,"date":"2019-01-14T21:55:11","date_gmt":"2019-01-14T21:55:11","guid":{"rendered":"https:\/\/kindsonthegenius.com\/java\/?p=22"},"modified":"2019-03-02T04:34:12","modified_gmt":"2019-03-02T04:34:12","slug":"04-java-keywords","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/java\/04-java-keywords\/","title":{"rendered":"Java &#8211; Keywords"},"content":{"rendered":"<p>In this tutorial, we would examine various Java keywords beginning with Java Modifiers<\/p>\n<p>We would cover the following.<\/p>\n<ol>\n<li><a href=\"#t1\">Java Modifiers<\/a><\/li>\n<li><a href=\"#t2\">Types of Variable<\/a><\/li>\n<li><a href=\"#t3\">Enums in Java<\/a><\/li>\n<li><a href=\"#t4\">List of Java Keywords<\/a><\/li>\n<li><a href=\"#t5\">The Interface Keyword<\/a><\/li>\n<\/ol>\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"t1\">1. Java Modifiers<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Modifiers are keywords used to add additional meaning or restriction to classes, methods, or variables. The two categories of java modifiers are<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Access Modifiers: public, protected, private, default<\/li><li>Non-Access Modifiers: final, abstract, strictfp<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"t2\">2. Types of Variables in Java<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">As you already know, variables are memory locations for storing data. Names of variables are chosen by the programmer.  Therefore, Java provide three different categories of variable that can be used<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Local Variables<\/li><li>Class Variables (declared using the static keyword)<\/li><li>Instance Variable<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"t3\">3. Enums in Java<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Another important keyword in Java is enum. You can use Enum to limit a the values of a variable to some predefined values. These predefined values are called enums.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, we may have an application that uses a variable color. Since you want color to be limited to only certain values, you can use enum to create a list of colors. As such, color cannot be assigned a value that is not actually a color. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example is given in Listing 1.0. If you run the code, you could see that the output is green<\/p>\n\n\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Your Testing Enum<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">EnumExample<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">enum<\/span> color<span style=\"color: #333333;\">{<\/span>red<span style=\"color: #333333;\">,<\/span> blue<span style=\"color: #333333;\">,<\/span> green<span style=\"color: #333333;\">,<\/span> yellow<span style=\"color: #333333;\">,<\/span> pink<span style=\"color: #333333;\">,<\/span> orange<span style=\"color: #333333;\">}<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\t\n\t\tcolor myColor <span style=\"color: #333333;\">=<\/span> color<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">green<\/span><span style=\"color: #333333;\">;<\/span>\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>myColor<span style=\"color: #333333;\">);<\/span>\n\t\tcolor myColor <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"yello\"<\/span><span style=\"color: #333333;\">;<\/span>   <span style=\"color: #888888;\">\/\/compiletime error!<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.0: Example of the Enum keyword<\/p>\n\n\n<p class=\"wp-block-paragraph\">In the example above, you can only assign to myColor, a value that is in the enum list. Therefore, if you try to assign any value not in the enum list to myColor, you  will have  an error<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"t4\">4. List of Java Keywords<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">You can find list of Java keywords in Table 1.0. You cannot use any of these keywords cannot be used to name identifier.<\/p>\n\n\n<p><\/p>\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>abstract<\/td><td>assert<\/td><td>boolean<\/td><td>break<\/td><\/tr><tr><td>byte<\/td><td>case<\/td><td>catch<\/td><td>char<\/td><\/tr><tr><td>class<\/td><td>const<\/td><td>continue<\/td><td>default<\/td><\/tr><tr><td>do<\/td><td>double<\/td><td>else<\/td><td>enum<\/td><\/tr><tr><td>extends<\/td><td>final<\/td><td>finally<\/td><td>float<\/td><\/tr><tr><td>for<\/td><td>goto<\/td><td>if<\/td><td>implements<\/td><\/tr><tr><td>import<\/td><td>instanceof<\/td><td>int<\/td><td>interface<\/td><\/tr><tr><td>long<\/td><td>native<\/td><td>new<\/td><td>package<\/td><\/tr><tr><td>private<\/td><td>protected<\/td><td>public<\/td><td>return<\/td><\/tr><tr><td>short<\/td><td>static<\/td><td>strictfp<\/td><td>super<\/td><\/tr><tr><td>switch<\/td><td>synchronized<\/td><td>this<\/td><td>throw<\/td><\/tr><tr><td>throws<\/td><td>transient<\/td><td>try<\/td><td>void<\/td><\/tr><tr><td>volatile<\/td><td>while<\/td><td><\/td><td><\/td><\/tr><\/tbody><\/table>\n\n\n<p>Table 1.0: List of Java Keywords<\/p>\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"t5\">5. The Interface Keyword<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Finally we would briefly learn about Interfaces. An interface is a class can  only be inherited. You use interface keyword to create a class that needs not be instantiated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example of interface in Java is given in Listing 1.1 below<\/p>\n\n\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">interface<\/span> <span style=\"color: #BB0066; font-weight: bold\">Shape<\/span> <span style=\"color: #333333\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #333399; font-weight: bold\">void<\/span> <span style=\"color: #0066BB; font-weight: bold\">render<\/span><span style=\"color: #333333\">();<\/span> \n\t\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #333399; font-weight: bold\">void<\/span> <span style=\"color: #0066BB; font-weight: bold\">getType<\/span><span style=\"color: #333333\">();<\/span>\n<span style=\"color: #333333\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.1: Example of Java Interface<\/p>\n\n\n<p class=\"wp-block-paragraph\">In the above example, we have created an interface called Shape. The two methods, render() and getType() are not implemented here. Therefore they must be implemented by a class that implements this interface. We would learn more about interfaces in subsequent lessons<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we would examine various Java keywords beginning with Java Modifiers We would cover the following. Java Modifiers Types of Variable Enums in &hellip; <\/p>\n","protected":false},"author":395,"featured_media":23,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/22\/revisions\/24"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/23"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}