#2-Known is a drop! JAVA - Datatypes

Published: (February 23, 2026 at 09:43 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Data types in JAVA

A datatype in programming defines the type of data a variable can hold, how much memory it occupies, and what operations can be performed on it.

Java is a statically‑typed language because variable types are checked at compile time and must be explicitly declared.

Java has primitive data types and non‑primitive data types.

Variable Nomenclature

Rules

  • Must start with a letter, _, or $
  • Cannot start with a number
  • Cannot use Java keywords
  • Case‑sensitive
  • No spaces allowed

Conventions (Best Practice)

  • Use camelCasestudentAge, totalMarks
  • Use meaningful names
  • Avoid single‑letter names (except i, j in loops)
  • Constants → UPPERCASE (e.g., MAX_SIZE, PI)

Primitive Datatypes

Primitive datatype diagram

Why Byte Size Matters for Different Data Types

  • Range matters because it defines what numbers a variable can safely store.
  • Too small → overflow / wrong results
  • Too large → wastes memory

Understanding 2’s Complement

2's complement illustration

Default Values

If a field is declared but not initialized, the compiler assigns a default value (typically 0 for numeric types, false for boolean, and null for reference types). Relying on these defaults is generally considered bad programming style.

Using Underscore Characters in Numeric Literals (Java 7+)

Underscores can appear anywhere between digits to improve readability.

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;

long hexBytes = 0xFF_EC_DE_5E; // hexadecimal literal
long hexWords = 0xCAFE_BABE;

byte by = 0b0010_0101; // binary literal, value 37 decimal
0 views
Back to Blog

Related posts

Read more »