A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/byte_keyword_in_java.htm below:

Java - byte Keyword

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.

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4