Tutorials›Java
Programming

Data types

Data types in Java specify the exact size and type of values that can be stored in a variable.

<!--StartFragment-->In Java, data types define the specific kind of data a variable can hold and the exact amount of memory it will occupy. Because Java is a strongly typed language, every variable must have a declared data type before it can be used.<div><span data-leq-manual-style="1" style="font-family: Inter, ui-sans-serif, system-ui, -apple-system, &quot;Segoe UI&quot;, Arial, sans-serif;"><span><br></span></span>Java data types are divided into two main categories: <b>Primitive</b> and <b>Non-Primitive (Reference)</b> types.<br><h2>1. Primitive Data Types</h2>These are the basic, built-in building blocks of data manipulation in Java. There are exactly eight primitive data types, and they store pure, raw values without any additional methods or object-oriented features.<br><table><thead><tr><td><strong>Type</strong></td><td><strong>Size</strong></td><td><strong>Description</strong></td><td><strong>Example</strong></td></tr></thead><tbody><tr><td><b><code data-path-to-node="4,1,0,0" data-index-in-node="0">byte</code></b></td><td>1 byte</td><td>Stores whole numbers from -128 to 127. Useful for saving memory in large arrays.</td><td><code data-path-to-node="4,1,3,0" data-index-in-node="0">byte age = 25;</code></td></tr><tr><td><b><code data-path-to-node="4,2,0,0" data-index-in-node="0">short</code></b></td><td>2 bytes</td><td>Stores whole numbers from -32,768 to 32,767.</td><td><code data-path-to-node="4,2,3,0" data-index-in-node="0">short year = 2026;</code></td></tr><tr><td><b><code data-path-to-node="4,3,0,0" data-index-in-node="0">int</code></b></td><td>4 bytes</td><td>The default choice for whole numbers (up to ~2.1 billion).</td><td><code data-path-to-node="4,3,3,0" data-index-in-node="0">int population = 1500000;</code></td></tr><tr><td><b><code data-path-to-node="4,4,0,0" data-index-in-node="0">long</code></b></td><td>8 bytes</td><td>Used when an <code data-path-to-node="4,4,2,0" data-index-in-node="13">int</code> is not large enough. Append an 'L' to the value.</td><td><code data-path-to-node="4,4,3,0" data-index-in-node="0">long distance = 9876543210L;</code></td></tr><tr><td><b><code data-path-to-node="4,5,0,0" data-index-in-node="0">float</code></b></td><td>4 bytes</td><td>Stores fractional numbers up to 6-7 decimal digits. Append an 'f'.</td><td><code data-path-to-node="4,5,3,0" data-index-in-node="0">float speed = 12.5f;</code></td></tr><tr><td><b><code data-path-to-node="4,6,0,0" data-index-in-node="0">double</code></b></td><td>8 bytes</td><td>The default for decimal numbers, storing up to 15 decimal digits.</td><td><code data-path-to-node="4,6,3,0" data-index-in-node="0">double pi = 3.14159265359;</code></td></tr><tr><td><b><code data-path-to-node="4,7,0,0" data-index-in-node="0">boolean</code></b></td><td>1 bit</td><td>Stores only two distinct values: <code data-path-to-node="4,7,2,0" data-index-in-node="33">true</code> or <code data-path-to-node="4,7,2,0" data-index-in-node="41">false</code>.</td><td><code data-path-to-node="4,7,3,0" data-index-in-node="0">boolean isOnline = true;</code></td></tr><tr><td><b><code data-path-to-node="4,8,0,0" data-index-in-node="0">char</code></b></td><td>2 bytes</td><td>Stores a single character, letter, or ASCII/Unicode value. Enclosed in single quotes.</td><td><code data-path-to-node="4,8,3,0" data-index-in-node="0">char grade = 'A';</code></td></tr></tbody></table><h2><br></h2><h2>2. Non-Primitive (Reference) Data Types</h2>Non-primitive types are created by the programmer (with the exception of <code data-path-to-node="6" data-index-in-node="73">String</code>, which is built-in) and are used to store complex objects. Instead of storing the actual value directly, they store a <b>reference (or memory address)</b> pointing to where the data is located in the memory heap.<br><ul><li><b>Strings:</b> A sequence of characters enclosed in double quotes (e.g., <code data-path-to-node="7,0,0" data-index-in-node="67">String name = "Java";</code>).<br></li><li><b>Arrays:</b> A collection of variables of the same type stored in a contiguous memory block (e.g., <code data-path-to-node="7,1,0" data-index-in-node="94">int[] scores = {90, 85, 95};</code>).<br></li><li><b>Classes &amp; Objects:</b> Blueprints for creating complex data structures that contain both data (attributes) and behavior (methods).<br></li><li><b>Interfaces:</b> Contracts that define methods a class must implement.</li></ul><h2>Key Differences</h2><table><thead><tr><td><strong>Feature</strong></td><td><strong>Primitive Types</strong></td><td><strong>Non-Primitive Types</strong></td></tr></thead><tbody><tr><td><b>Origin</b></td><td>Predefined by Java.</td><td>Created by the programmer (except <code data-path-to-node="9,1,2,0" data-index-in-node="34">String</code>).</td></tr><tr><td><b>Value</b></td><td>Always contains a value (has strict default values like <code data-path-to-node="9,2,1,0" data-index-in-node="56">0</code> or <code data-path-to-node="9,2,1,0" data-index-in-node="61">false</code>).</td><td>Can be assigned <code data-path-to-node="9,2,2,0" data-index-in-node="16">null</code> to indicate no object is referenced.</td></tr><tr><td><b>Methods</b></td><td>Cannot call methods to perform operations.</td><td>Can call methods (e.g., <code data-path-to-node="9,3,2,0" data-index-in-node="24">name.length()</code>).</td></tr><tr><td><b>Naming&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b></td><td>Start with a lowercase letter (<code data-path-to-node="9,4,1,0" data-index-in-node="31">int</code>, <code data-path-to-node="9,4,1,0" data-index-in-node="36">boolean</code>).</td><td>Usually start with an uppercase letter (<code data-path-to-node="9,4,2,0" data-index-in-node="40">String</code>, <code data-path-to-node="9,4,2,0" data-index-in-node="48">Scanner</code>).</td></tr><tr><td><b>Memory</b></td><td>Stored directly in Stack memory (faster).</td><td>The reference is in the Stack, but the actual object is in Heap memory.<!--StartFragment--><div style="color:rgb(31, 31, 31)"><div>In Java, data types define the specific kind of data a variable can hold and the exact amount of memory it will occupy. Because Java is a strongly typed language, every variable must have a declared data type before it can be used.</div><br><div>Java data types are divided into two main categories: <b>Primitive</b> and <b>Non-Primitive (Reference)</b> types.</div><br></div></td></tr></tbody></table><br></div>