Java Data Types


Java supports several data types, including primitive types and reference types. Here are the different data types in Java:

Primitive types

  1. byte: 8-bit integer data type.
  2. short: 16-bit integer data type.
  3. int: 32-bit integer data type.
  4. long: 64-bit integer data type.
  5. float: 32-bit floating-point data type.
  6. double: 64-bit floating-point data type.
  7. char: 16-bit Unicode character data type.
  8. boolean: true/false data type.

Reference types

  1. String: a sequence of characters.
  2. Arrays: a collection of elements of the same data type.
  3. Classes: user-defined data types that can contain fields, methods, and constructors.
  4. Interfaces: similar to classes, but can only contain method signatures.

Here are some examples of how to declare variables of different data types in Java:

byte myByte = 127;
short myShort = 32767;
int myInt = 2147483647;
long myLong = 9223372036854775807L;
float myFloat = 3.14159F;
double myDouble = 3.14159265358979323846;
char myChar = 'A';
boolean myBoolean = true;

String myString = "Hello, world!";
int[] myArray = {1, 2, 3, 4, 5};
MyClass myClass = new MyClass();

These are the different data types in Java. Understanding data types is essential to writing Java programs, as it helps ensure that variables are assigned the correct type of value and that functions can handle data of different types.

A quick recap of java