You are here: Home / Topics / What is the default value of byte datatype in Java?

What is the default value of byte datatype in Java?

Filed under: Java Interview Questions on 2024-10-25 14:38:41

In Java, the default value of the byte datatype is 0. This applies when a byte variable is declared but not explicitly initialized. For example:


byte myByte;
System.out.println(myByte); // This will cause a compilation error if you try to print it without initialization.
 

However, if you declare it within a class and don't initialize it, it will default to 0:


class Example {
   byte myByte; // Defaults to 0

   void display() {
       System.out.println(myByte); // Outputs: 0
   }
}
 

In this case, myByte will have a default value of 0 when the object of the class is created.


About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.