{"id":631,"date":"2016-01-19T08:39:47","date_gmt":"2016-01-19T08:39:47","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=631"},"modified":"2016-01-19T08:39:47","modified_gmt":"2016-01-19T08:39:47","slug":"byte-examples-program-class","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/byte-examples-program-class.html","title":{"rendered":"Byte With Examples And Program In JAVA"},"content":{"rendered":"<p>The smallest integer data type is\u00a0<strong>byte<\/strong>.\u00a0Byte type variables\u00a0are especially useful when you are working with a stream of data from a network or a file. They are also useful when you are working with raw binary data that may not be directly compatible with Java&#8217;s other built-in types.\u00a0Keyword \u201cbyte\u201d is used to declare byte variables. For example, the following declares two\u00a0<strong>byte<\/strong>\u00a0variables called\u00a0<strong>a<\/strong>\u00a0and\u00a0<strong>b<\/strong>: <code>byte a, b;<\/code><\/p>\n<p><span style=\"color: #008000;\"><strong>Important Points About byte Integer Data Type:<\/strong><\/span><\/p>\n<ul>\n<li>Byte data type is an 8-bit signed two&#8217;s complement integer.<\/li>\n<li>Minimum value of Byte is -128 (-2<sup>7<\/sup>).<\/li>\n<li>Maximum value of Byte is 127 (inclusive)(2<sup>7<\/sup> -1).<\/li>\n<li>Default value of Byte is 0.<\/li>\n<li>Byte data type is used to save memory in large arrays, mainly in place of integers because byte is four times smaller than an int.<\/li>\n<\/ul>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Remember value of byte data type size is -128 to 127<\/p>\n<hr \/>\n<h4><strong>byte Syntax in JAVA:<\/strong><\/h4>\n<pre>byte Variable_Name = Value;<\/pre>\n<p>For example:<\/p>\n<pre>byte x = 10;<\/pre>\n<p>Here x is variable name and 10 is a value assigned to a variable integer data type byte.<\/p>\n<hr \/>\n<h4><strong>Byte Explanation With Example Program:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> This program prints the value of variables of type byte.<\/p>\n<pre>public class TestingByte {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tbyte b = 10; \/\/declare a byte variable and assign the value i.e 10\r\n\t\tSystem.out.println(b); \/\/ print the value of byte variable b\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>10<\/pre>\n<p><span style=\"color: #008000;\"><strong>Example 2:<\/strong><\/span> byte Example using Byte Class:<\/p>\n<p>You can see in the below program we use Byte class to create a object because if we call the variable values by class name then it gives us compilation error that arguments differ in length. That&#8217;s why we use Byte class.<\/p>\n<p>More details about Byte class and its methods is shared after Example.<\/p>\n<pre>public class Number\r\n\r\n{\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String[] args)\r\n\r\n\u00a0\u00a0 \u00a0{\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0byte b = 10; \/\/declare a byte variable and assign the value i.e 10\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Byte n1 = new Byte(b);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Byte n2 = new Byte(\"4\");\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(n1); \/\/ print the value of byte variable n1\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(n2);\/\/print the value of byte variable n2\r\n\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>10\r\n4<\/pre>\n<p><span style=\"color: #008000;\"><strong>Example 3:<\/strong><\/span> Using byte variable for addition<\/p>\n<p>In below example we use byte data type in class AdditionByte to add two numbers of type byte and stores the value in third variable of type byte:<\/p>\n<pre>public class AddtionByte {\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String[] args) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0byte a = 30;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0byte b = 40;\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0byte c = (byte) (a + b);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"The c variable Value after Addition is : \" + c);\r\n\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}\r\n\r\n<\/pre>\n<p><strong> Output:<\/strong><\/p>\n<pre>The c variable Value after Addition is : 70\r\n\r\n<\/pre>\n<hr \/>\n<h4><strong>Byte Class in JAVA:<\/strong><\/h4>\n<p>The\u00a0<strong>java.lang.Byte<\/strong>\u00a0class wraps a value of primitive type byte in an object. Object of Byte type contains a single field whose type is byte.<\/p>\n<p><span style=\"color: #008000;\"><strong>Declaration of java.lang.Byte Class:<\/strong><\/span><\/p>\n<p>Following is the declaration of\u00a0java.lang.Byte\u00a0class:<\/p>\n<p><code>public final class Byte extends Number implements Comparable&lt;Byte&gt;<\/code><\/p>\n<p><span style=\"color: #008000;\"><strong>Fields of java.lang.Byte Class:<\/strong><\/span><\/p>\n<p>Following are the fields of\u00a0java.lang.Byte\u00a0class:<\/p>\n<ol>\n<li><strong>static byte MAX_VALUE\u00a0<\/strong>: This is constant which holds maximum value of byte i.e 2<sup>7<\/sup>-1.<\/li>\n<li><strong>static byte MIN_VALUE\u00a0<\/strong>: This is constant which holds minimum value a byte i.e-2<sup>7<\/sup>.<\/li>\n<li><strong>staticint SIZE\u00a0<\/strong>: This is the number of bits used to represent a value of byte in two&#8217;s complement binary form.<\/li>\n<li><strong>static Class&lt;Byte&gt; TYPE\u00a0<\/strong>: This is the Class instance which represents the primitive type byte.<\/li>\n<\/ol>\n<p><span style=\"color: #008000;\"><strong>Constructors of java.lang.Byte class:<\/strong><\/span><\/p>\n<p>Following are the constructors of java.lang.Byte class:<\/p>\n<ol>\n<li><strong>Byte(byte value):<\/strong>This constructs a newly allocated Byte object representing the specified value of byte.<\/li>\n<\/ol>\n<ol start=\"2\">\n<li><strong>Byte(String s):<\/strong>This constructs a newly allocated Byte object representing the value of byte indicated by the String parameter.<\/li>\n<\/ol>\n<p><span style=\"color: #008000;\"><strong>Methods of java.lang.Byte class:<\/strong><\/span><\/p>\n<p>Following are the methods of java.lang.Byte class:<\/p>\n<ol>\n<li><strong>bytebyteValue()<\/strong><strong>: <\/strong>This method returns thebyte value of this Byte.<\/li>\n<li><strong>intcompareTo(Byte anotherByte)<\/strong><strong>: <\/strong>This method compares two Byte objects numerically.<\/li>\n<li><strong>static Byte decode(String nm)<\/strong><strong>: <\/strong>This method decodes a String into a Byte.<\/li>\n<li><strong>doubledoubleValue()<\/strong><strong>:<\/strong> This method returns thedouble value of this Byte.<\/li>\n<li><strong>boolean equals(Object obj)<\/strong><strong>: <\/strong>This method compares this object to the specified object.<\/li>\n<li><strong>inthashCode()<\/strong><strong>: <\/strong>This method returns a hash code for this Byte.<\/li>\n<li><strong>floatfloatValue()<\/strong><strong>: <\/strong>This method returns thefloat value of this Byte.<\/li>\n<li><strong>intintValue()<\/strong><strong>: <\/strong>This method returns theint value of this Byte.<\/li>\n<li><strong>longlongValue()<\/strong><strong>: <\/strong>This method returns thelong value of this Byte.<\/li>\n<li><strong>static byte parseByte(String s)<\/strong><strong>: <\/strong>This method parses the string argument as a signed decimal byte.<\/li>\n<li><strong>static byte parseByte(String s, int radix)<\/strong><strong>: <\/strong>This method parses the string argument as a signed byte in the radix specified by the second argument.<\/li>\n<li><strong>shortshortValue()<\/strong><strong>: <\/strong>This method returns theshort value of this Byte.<\/li>\n<li><strong>String toString()<\/strong><strong>: <\/strong>This method returns a String object that represents the value of Byte.<\/li>\n<li><strong>static String toString(byte b)<\/strong><strong>: <\/strong>This method returns a new String object which represent the specified byte.<\/li>\n<li><strong>static Byte valueOf(byte b)<\/strong><strong>: <\/strong>This method returns a Byte instance which represent the specified byte value.<\/li>\n<li><strong>static Byte valueOf(String s)<\/strong><strong>: <\/strong>This method returns a Byte object that holds the value given by the specified String.<\/li>\n<li><strong>static Byte valueOf(String s, int radix)<\/strong><strong>: <\/strong>This method returns a Byte object that holds the value taken from the specified String when parsed with the radix given by the second argument.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>The smallest integer data type is\u00a0byte.\u00a0Byte type variables\u00a0are especially useful when you are working with a stream of data from a network or a file. They are also useful when you are working with raw binary data that may not be directly compatible with Java&#8217;s other built-in types.\u00a0Keyword \u201cbyte\u201d is used to declare byte variables. &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/byte-examples-program-class.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Byte With Examples And Program In JAVA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,70],"tags":[72,71,73],"class_list":["post-631","post","type-post","status-publish","format-standard","hentry","category-archieve","category-data-type","tag-byte-class-in-java","tag-byte-primitive-data-type","tag-byte-size-in-java"],"psp_head":"<title>Byte Tutorial With Examples And Program In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on the smallest integer data type byte with examples and program in JAVA. Also find details about Byte class and its methods.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/byte-examples-program-class.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"predecessor-version":[{"id":1463,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/631\/revisions\/1463"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}