Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - byte Keyword



byte Keyword

Java byte keyword is used to define one of the eight primitive data types supported by Java. It provides a means to create byte-type variables which can accept a byte value.

Characteristics

Following are the characteristics of a byte data type.

  • Byte data type is an 8-bit signed two's complement integer

  • Minimum value is -128 (-2^7)

  • Maximum value is 127 (inclusive)(2^7 -1)

  • Default value is 0

  • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.

  • Example − byte a = 100, byte b = -50

Syntax

The syntax of byte keyword to declare a byte variable is as follows:

byte variable_name;

Default Value

The default of a variable declared using the byte keyword is 0.

byte Variable

A byte variables represents a reserved memory locations to store byte values. This means that when you create a variable you reserve some space in the memory.

Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store byte values in byte variables.

Examples of byte Keyword

Example 1

Following example shows the usage of byte primitive data type we've discussed above. We've created a byte variable as byteValue and assigned it a byte value. Then this variable is printed.

package com.tutorialspoint;

public class JavaTester {
   public static void main(String args[]) {
      byte byteValue = 2;
      System.out.println("Byte: " + byteValue);	  
   }
}

Output

Byte: 2

Example 2

Following example shows the usage of byte primitive data type in an expression. We've created two byte variables and assigned them byte values. Then we're creating a new byte variable byteResult to assign it the sum of byte variables. Finally result is printed.

package com.tutorialspoint;

public class JavaTester {
   public static void main(String args[]) {
      byte byteValue1 = 2;
      byte byteValue2 = 4;
      byte byteResult = (byte)(byteValue1 + byteValue2);

      System.out.println("Byte: " + byteResult);
   }
}

Output

Byte: 6

Example 3

Following example shows the usage of byte variable with an invalid value. We've created a byte variable as byteValue and assigned it an out of range value.

package com.tutorialspoint;

public class JavaTester {
   public static void main(String args[]) {
      byte byteValue = 234;
      System.out.println("Byte: " + byteValue);
   }
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from int to byte

	at com.tutorialspoint.JavaTester.main(JavaTester.java:5)
java_basic_syntax.htm
Advertisements