Java Data Types

What Are Data Types?

In Java, a data type defines what kind of value a variable can store. Java is statically typed, so the type must be declared at compile time.

int age = 30;
double temperature = 98.6;

Primitive Data Types

Java has 8 primitive types built into the language:

  • byte – 1 byte (−128 to 127)
  • short – 2 bytes
  • int – 4 bytes (most common)
  • long – 8 bytes (add L at the end)
  • float – 4 bytes (use f suffix)
  • double – 8 bytes (default for decimals)
  • char – single characters in single quotes
  • boolean – either true or false
Loading...
Output:

Non-Primitive (Reference) Types

Non-primitive types don’t store actual values, but references (memory locations). Examples:

  • String
  • Arrays
  • Objects
String name = "Java";

Default Values

When you declare a class-level variable without assigning it a value, Java assigns a default:

  • int0
  • double0.0
  • booleanfalse
  • Stringnull

Type Conversion

Java allows some type conversion automatically, but for others you must use casting.

int a = 10;
double b = a;          // Implicit
int c = (int) b;       // Explicit cast

Practice Question

Which of the following is a valid variable declaration? Write your answer to AI Chatbot

A. int 1stScore = 90;
B. String fullName = "Jane Doe";
C. double value = "3.14";

Hint: Watch for illegal names and type mismatches.

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic