{"id":218,"date":"2020-10-28T05:03:39","date_gmt":"2020-10-28T05:03:39","guid":{"rendered":"https:\/\/usemynotes.com\/?p=218"},"modified":"2023-01-19T17:33:03","modified_gmt":"2023-01-19T17:33:03","slug":"data-types-in-java-programming","status":"publish","type":"post","link":"https:\/\/usemynotes.com\/data-types-in-java-programming\/","title":{"rendered":"Data Types in Java Programming"},"content":{"rendered":"<p>In this tutorial, we will be learning about Data types in Java programming, and the various data types available in the Java programming language. We will mainly focus on various data types, their memory allocation size, and their range.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#Data_Types_in_Java_Programming\" >Data Types in Java Programming<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#List_of_Data_Types_in_Java_Programming\" >List of Data Types in Java Programming<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#Primitive_Data_Type\" >Primitive Data Type<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#byte\" >byte<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#short\" >short<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#int\" >int<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#long\" >long<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#float\" >float<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#double\" >double<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#char\" >char<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#boolean\" >boolean<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#Non-primitive_Data_Type\" >Non-primitive Data Type<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#String\" >String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#Array\" >Array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#Class\" >Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/usemynotes.com\/data-types-in-java-programming\/#Interface\" >Interface<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Data_Types_in_Java_Programming\"><\/span>Data Types in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Data types in Java programming are one of the basic building blocks of a program. It determines what type of data can be stored in a variable. The <a href=\"https:\/\/usemynotes.com\/what-is-jvm-jit\/\">Java compiler<\/a> needs this information before compiling in order to work correctly. In <a href=\"https:\/\/usemynotes.com\/what-is-java\/\">Java<\/a>, there exist two data types:<\/p>\n<h2><span class=\"ez-toc-section\" id=\"List_of_Data_Types_in_Java_Programming\"><\/span>List of Data Types in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li>Primitive data type<\/li>\n<li>Non-primitive data type<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-219 size-large\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-999x1024.jpg\" alt=\"Data Types in Java Programming\" width=\"999\" height=\"1024\" title=\"\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-999x1024.jpg 999w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-293x300.jpg 293w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-768x787.jpg 768w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-150x154.jpg 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-300x308.jpg 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-696x714.jpg 696w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-1068x1095.jpg 1068w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-410x420.jpg 410w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java-356x364.jpg 356w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/10\/what-is-data-types-in-java.jpg 1464w\" sizes=\"(max-width: 999px) 100vw, 999px\" \/><\/p>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Primitive_Data_Type\"><\/span>Primitive Data Type<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Data Types in Java Programming is first data types of Primitive data type. Primitive data types are those types that are already defined in the <a href=\"https:\/\/usemynotes.com\/a-simple-java-program\/\">Java programming<\/a> language. We don\u2019t have to explicitly define these data types to the Java compiler. They can be used directly in a program to store values. Java is packed with several data types out of the box, they are:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"byte\"><\/span>byte<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A byte data types in Java Programming can be considered as the smallest integer data type that can store small integer values. It is an 8-bit signed two\u2019s complement integer data type. This data type consumes a very less amount of memory space to store an integer value. The integer value that a byte type of variable can store should be between -128 to 127 (inclusive). This data type can be used where there is a need for a counter variable for small iterations.<\/p>\n<ul>\n<li>Memory: 1 byte (8-bit)<\/li>\n<li>Range: -128 to 127<\/li>\n<li>Default Value: 0<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class ByteProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n       byte number = 100;\r\n       System.out.println(&quot;Number variable has: &quot; + number);\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\nNumber variable has: 100<br \/>\n*\/<\/p>\n<h3><span class=\"ez-toc-section\" id=\"short\"><\/span>short<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>short data types in Java Programming is basically similar to an integer data type but with a smaller range and smaller size occupied in the memory. It is just a 16-bit signed two\u2019s complement integer data type. This data type allows a variable to store an integer value that ranges from -32768 to 32767 (inclusive). This data type can be used in situations where there is a need for storing an integer value that is typically somewhere between a few thousand.<\/p>\n<ul>\n<li>Memory: 2 bytes (16-bits)<\/li>\n<li>Range: -32768 to 32767<\/li>\n<li>Default Value: 0<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class ShortProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        short number = 31876;\r\n        System.out.println(&quot;Number variable has: &quot; + number);\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\nNumber variable has: 31876<br \/>\n*\/<\/p>\n<h3><span class=\"ez-toc-section\" id=\"int\"><\/span>int<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>int data types in Java Programming is a 32-bit signed two\u2019s complement integer data type. This data type allows a variable to store an integer value that ranges from -2147483648 to 2147483647 (inclusive). This data type can be used in situations where there is a need for storing an integer value that is larger or somewhere between a few million.<\/p>\n<ul>\n<li>Memory: 4 bytes (32-bits)<\/li>\n<li>Range: -2147483648 to 2147483647<\/li>\n<li>Default Value: 0<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class IntProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        int number = 2147483643;\r\n        System.out.println(&quot;Number variable has: &quot; + number);\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\nNumber variable has: 2147483643<br \/>\n*\/<\/p>\n<h3><span class=\"ez-toc-section\" id=\"long\"><\/span>long<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>long data types in Java Programming is basically also an integer data type but with a larger range and larger memory occupied in the memory. It is a 64-bit signed two\u2019s complement integer data type. This data type allows a variable to store an integer value that ranges from 9223372036854776000 to 9223372036854775999 (inclusive). This data type can be used in situations where there is a need for storing an integer value that is very large.<\/p>\n<ul>\n<li>Memory: 8 bytes (64-bit)<\/li>\n<li>Range: 9223372036854776000 to 9223372036854775999<\/li>\n<li>Default Value: 0<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class LongProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        long number = 235634567634644565L;\r\n        System.out.println(&quot;Number variable has: &quot; + number);\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\nNumber variable has: 235634567634644565<br \/>\n*\/<\/p>\n<h3><span class=\"ez-toc-section\" id=\"float\"><\/span>float<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In technical terms, the float is considered as a 32-bit single-precision IEEE 754 floating-point data type in Java programming. This data type allows a variable to store a numerical value that has decimal numbers after the decimal point. This data type allows the storing of a floating-point number that ranges from -3.4 E38 to 3.4 E38. Make sure that there exist only a few digits after the decimal point (around 4 or 5).<\/p>\n<ul>\n<li>Memory: 4 bytes (32-bits)<\/li>\n<li>Range: -3.4 E38 to 3.4 E38<\/li>\n<li>Default Value: 0<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class FloatProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        float number = 45764.176f;\r\n        System.out.println(&quot;Number variable has: &quot; + number);\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\nNumber variable has: 45764.176<br \/>\n*\/<\/p>\n<h3><span class=\"ez-toc-section\" id=\"double\"><\/span>double<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>double data types in Java Programming is a 64-bit single-precision IEEE 754 floating-point data type. This data type allows a variable to store a numerical value that has decimal numbers after the decimal point. This data type allows storing of a floating-point number that ranges from -1.79 E308 to 1.79 E308. Make sure that there exist only a few digits after the decimal point (around 14 or 15).<\/p>\n<ul>\n<li>Memory: 8 bytes (64-bit)<\/li>\n<li>Range: -1.79 E308 to 1.79 E308<\/li>\n<li>Default Value: 0<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class DoubleProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        double number = 646437.8357654345;\r\n        System.out.println(&quot;Number variable has: &quot; + number);\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\nNumber variable has: 646437.8357654345<br \/>\n*\/<\/p>\n<h3><span class=\"ez-toc-section\" id=\"char\"><\/span>char<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>char data types in Java Programming is a 16-bit Unicode character data type. This data type allows a variable to store a single Unicode character. The Unicode character can be anything between \u2018\\u0000\u2019 and \u2018\\uffff\u2019 (inclusive).<\/p>\n<ul>\n<li>Memory: 2 bytes (16-bits)<\/li>\n<li>Range: \u2018\\u0000\u2019 to \u2018\\uffff\u2019<\/li>\n<li>Default Value: None<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class CharProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        char ch = '&amp;';\r\n        System.out.println(&quot;Ch variable has: &quot; + ch);\r\n    }\r\n}<\/pre>\n<p>\/* Output<br \/>\nCh variable has: &amp;<br \/>\n*\/<\/p>\n<h3><span class=\"ez-toc-section\" id=\"boolean\"><\/span>boolean<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>boolean data types in Java Programming is a data type that is used to store flag values. This data type doesn\u2019t have a fixed memory allocation size but it is assumed to a 1-bit. It allows storing a single character in a variable either true or false at a time.<\/p>\n<ul>\n<li>Memory: 1-bit (Assumed)<\/li>\n<li>Range: Only \u2018true\u2019 and \u2018false\u2019<\/li>\n<li>Default Value: false<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class BooleanProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        boolean flag = true;\r\n        System.out.println(&quot;Flag variable has: &quot; + flag);\r\n    }\r\n}<\/pre>\n<p>\/* Output<br \/>\nFlag variable has: true<br \/>\n*\/<\/p>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Non-primitive_Data_Type\"><\/span>Non-primitive Data Type<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Data Types in Java Programming is second data type of non-primitive data type. Non-Primitive data types are those types that are actually not defined in the Java programming language but a programmer needs to explicitly define these data types to the Java compiler. They can also be used directly in a program to store values (just like primitive data types). For now, we will just scratch the surface of these data types, but we will them in-depth in our future tutorials.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"String\"><\/span>String<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The string data types in Java Programming is a non-primitive data type that stores a bunch of characters in a sequence. Strings can be used to form words or sentences as needed by the programmer\/developer. A string can just be a character, word, line, or just a blank space. If we say a word say &#8220;pencil&#8221;, then &#8220;pencil&#8221; is a valid string. A string can be composed of alphabets, numbers, and special characters. A string is represented with &#8220;&#8221; symbols.<br \/>\nSome examples of a string are:<\/p>\n<ul>\n<li>&#8220;Jupiter&#8221;<\/li>\n<li>&#8220;hello123&#8221;<\/li>\n<li>&#8220;money$&#8221;<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class StringProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        String letter = &quot;a&quot;;\r\n        String word = &quot;Mountain&quot;;\r\n        String line = &quot;This is awesome&quot;;\r\n        String_mixed = &quot;Mountain 33.35 with a symbol @&quot;;\r\n        System.out.println(&quot;Print a letter: &quot; + letter);\r\n        System.out.println(&quot;Print a word: &quot; + word);\r\n        System.out.println(&quot;Print a line: &quot; + line);\r\n        System.out.println(&quot;Print a mixed line: &quot; + mixed);\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\nPrint a letter: a<br \/>\nPrint a word: Mountain<br \/>\nPrint a line: This is awesome<br \/>\nPrint a mixed line: We have 192 paper bags of $0.24 each<br \/>\n*\/<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Array\"><\/span>Array<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>An array data types in Java Programming is also a non-primitive data type that holds a single type of data. The elements of an array will always have the same data type and it will not be changed for just one element. The elements of an array are structured in a sequential order where each element of the array can be accessed using its index value (starting from zero). There is no fixed size for an array as we see in primitive data types.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class ArrayProgram{\r\n    public static void main(String arg&#x5B;]) {\r\n        String&#x5B;] simpleItems = {&quot;Bricks&quot;, &quot;Cake&quot;, &quot;Noise&quot;, &quot;Humor&quot;};\r\n        System.out.println(&quot;1st element of simpleItems array: &quot; + simpleItems &#x5B;0]);\r\n        System.out.println(&quot;2nd element of simpleItems array: &quot; + simpleItems &#x5B;1]);\r\n        System.out.println(&quot;3rd element of simpleItems array: &quot; + simpleItems &#x5B;2]);\r\n        System.out.println(&quot;4th element of simpleItems array: &quot; + simpleItems &#x5B;3]);\r\n        System.out.println();\r\n\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\n1st element of simpleItems array: Bricks<br \/>\n2nd element of simpleItems array: Cake<br \/>\n3rd element of simpleItems array: Noise<br \/>\n4th element of simpleItems array: Humor<br \/>\n*\/<\/p>\n<p>In the above program, the array \u201csimpleItems \u201d is of type \u201cString\u201d which means it can only store String type value in it. The elements initialized to \u201csimpleItems\u201d array are &#8220;Bricks&#8221;, &#8220;Cake&#8221;, &#8220;Noise&#8221; and \u201cHumor\u201d. These elements can be accessed using their index values starting from zero.<\/p>\n<ul>\n<li>At index 0, the element in the array is &#8220;Bricks&#8221;.<\/li>\n<li>At index 1, the element in the array is &#8220;Cake&#8221;.<\/li>\n<li>At index 2, the element in the array is &#8220;Noise&#8221;.<\/li>\n<li>At index 3, the element in the array is &#8220;Humor&#8221;.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Class\"><\/span>Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In Java, everything is based on classes and objects. These classes are like \u201cblueprints\u201d which come to existence when their objects are created. Basically, a class can have variables and methods which are related to that class. Let\u2019s try to understand a class with a simple example.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class Plastic {\r\n    public int cost = 200;\r\n    public String strength = &quot;weak&quot;;\r\n}\r\nclass ClassProgram {\r\n    public static void main(String&#x5B;] args) {\r\n        Plastic p = new Plastic();\r\n        System.out.println(p.cost);\r\n        System.out.println(p.strength);\r\n    }\r\n}<\/pre>\n<p>\/* <strong>Output<\/strong><br \/>\n200<br \/>\nweak<br \/>\n*\/<\/p>\n<p>In the above program, we have two classes namely &#8220;Plastic&#8221; and &#8220;ClassProgram&#8221;. The &#8220;ClassProgram&#8221; class is just used to contain the &#8220;main&#8221; method.<\/p>\n<p>Here, the &#8220;Plastic&#8221; class has two variables that are related to that class. They are &#8220;cost&#8221; and &#8220;strength&#8221;. Both these variables are trying to tell the cost of the plastic material as well as the strength of that plastic material.<\/p>\n<p>In the main method, we are creating an object of the Plastic class and storing the reference of the object in a variable name &#8220;p&#8221;. This &#8220;p&#8221; variable can be used to access the variables and functions of an object (in this case &#8220;cost&#8221; and &#8220;strength&#8221;). The main method also has two more statements that show how we are printing the values of the variables of the &#8220;p&#8221; object.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Interface\"><\/span>Interface<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>An interface data types in Java Programming is basically like a contract that guarantees that there is a function in the class that is specified in the interface.<br \/>\nConsider an interface that has a method declaration for the method \u201chello\u201d. If this interface is implemented in a class, then it is mandatory that this class should also implement the method \u201chello\u201d. If we just implement the interface to a class and not implement its functions, then the compiler throws an error and the program won\u2019t compile.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public interface IHello {\r\n    public void sayHello();\r\n}\r\nclass InterfaceProgram implements IHello{\r\n    public static void main(String&#x5B;] args) {\r\n    }\r\n}<\/pre>\n<p>\/\/ This program throws an error because we implemented the IHello interface but didn&#8217;t implement its method sayhello()<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">interface IHello {\r\n    public void sayHello();\r\n}\r\nclass InterfaceProgram implements IHello{\r\n    public void sayHello() {\r\n        System.out.println(&quot;Hello&quot;);\r\n    }\r\n    public static void main(String&#x5B;] args) {\r\n    }\r\n}<\/pre>\n<p>\/\/ This program will not throw any error and compile successfully<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will be learning about Data types in Java programming, and the various data types available in the Java programming language. We will mainly focus on various data types, their memory allocation size, and their range. Data Types in Java Programming Data types in Java programming are one of the basic building [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":219,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":{"0":"post-218","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java-programming"},"_links":{"self":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/comments?post=218"}],"version-history":[{"count":0,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media\/219"}],"wp:attachment":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media?parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/categories?post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/tags?post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}