{"id":12975,"date":"2022-09-29T17:16:40","date_gmt":"2022-09-29T11:46:40","guid":{"rendered":"https:\/\/blog.guvi.in\/?p=12975"},"modified":"2026-07-30T16:12:38","modified_gmt":"2026-07-30T10:42:38","slug":"data-types-in-java","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/data-types-in-java\/","title":{"rendered":"Java Data Types: Primitive + Non-Primitive with Code Examples (2026)"},"content":{"rendered":"\n<p>Every variable in Java needs a data type. It tells the compiler how much memory to set aside and what kind of operations are allowed on that data. If you&#8217;re learning Java in 2026, understanding data types in Java properly is one of the first real milestones \u2014 get this wrong and you&#8217;ll spend hours debugging things that should have been obvious.<\/p>\n\n\n\n<p>Data types in Java fall into two big buckets: <strong>primitive<\/strong> and <strong>non-primitive<\/strong> (also called reference types). Let&#8217;s go through both, with code you can actually run.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li>Data types in Java define what kind of value a variable can hold and how much memory it needs, and they split into primitive and non-primitive types.<\/li>\n\n\n\n<li>Primitive data types in Java include byte, short, int, long, float, double, char, and boolean \u2014 all 8 store actual values directly and don&#8217;t have methods.<\/li>\n\n\n\n<li>Non-primitive data types in Java, like String, arrays, classes, interfaces, and enums, store references to objects on the heap and can be null.<\/li>\n\n\n\n<li>Autoboxing and unboxing let Java convert automatically between primitive and non-primitive data types in Java, which matters for performance and avoiding NullPointerExceptions with collections.<\/li>\n\n\n\n<li>Understanding data types in Java is a common interview topic for freshers, especially around defaults, ranges, and wrapper classes.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Are Primitive Data Types in Java?<\/strong><\/h2>\n\n\n\n<p>Primitive types are the most basic data types built directly into the <a href=\"https:\/\/www.guvi.in\/blog\/introduction-to-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java language<\/a>. They&#8217;re not objects; they don&#8217;t have methods, and they hold their values directly in memory\u2014which makes them fast and lightweight.<\/p>\n\n\n\n<p>Java has <strong>8 primitive types<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Data Type<\/strong><\/th><th><strong>Size<\/strong><\/th><th><strong>Range<\/strong><\/th><th><strong>Default Value<\/strong><\/th><th><strong>Wrapper Class<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>byte<\/code><\/td><td>1 byte<\/td><td>-128 to 127<\/td><td>0<\/td><td><code>Byte<\/code><\/td><\/tr><tr><td><code>short<\/code><\/td><td>2 bytes<\/td><td>-32,768 to 32,767<\/td><td>0<\/td><td><code>Short<\/code><\/td><\/tr><tr><td><code>int<\/code><\/td><td>4 bytes<\/td><td>-2^31 to 2^31-1<\/td><td>0<\/td><td><code>Integer<\/code><\/td><\/tr><tr><td><code>long<\/code><\/td><td>8 bytes<\/td><td>-2^63 to 2^63-1<\/td><td>0L<\/td><td><code>Long<\/code><\/td><\/tr><tr><td><code>float<\/code><\/td><td>4 bytes<\/td><td>~6-7 decimal digits precision<\/td><td>0.0f<\/td><td><code>Float<\/code><\/td><\/tr><tr><td><code>double<\/code><\/td><td>8 bytes<\/td><td>~15 decimal digits precision<\/td><td>0.0d<\/td><td><code>Double<\/code><\/td><\/tr><tr><td><code>char<\/code><\/td><td>2 bytes<\/td><td>single 16-bit Unicode character<\/td><td>&#8216;\\u0000&#8217;<\/td><td><code>Character<\/code><\/td><\/tr><tr><td><code>boolean<\/code><\/td><td>1 bit (JVM-dependent)<\/td><td>true or false<\/td><td>false<\/td><td><code>Boolean<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Code Example: Primitive Types<\/strong> <\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class PrimitiveTypesDemo {\n    public static void main(String&#91;] args) {\n        byte age = 25;\n        short year = 2026;\n        int population = 1_400_000_000;\n        long distanceToSun = 149_600_000_000L;\n        float price = 19.99f;\n        double pi = 3.14159265358979;\n        char grade = 'A';\n        boolean isJavaFun = true;\n\n        System.out.println(\"Age: \" + age);\n        System.out.println(\"Year: \" + year);\n        System.out.println(\"Population: \" + population);\n        System.out.println(\"Distance to Sun: \" + distanceToSun + \" meters\");\n        System.out.println(\"Price: $\" + price);\n        System.out.println(\"Pi: \" + pi);\n        System.out.println(\"Grade: \" + grade);\n        System.out.println(\"Is Java fun? \" + isJavaFun);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Age: 25\nYear: 2026\nPopulation: 1400000000\nDistance to Sun: 149600000000 meters\nPrice: $19.99\nPi: 3.14159265358979\nGrade: A\nIs Java fun? true\n<\/code><\/pre>\n\n\n\n<p>A few things worth remembering:<\/p>\n\n\n\n<ul>\n<li>Use <code>L<\/code> suffix for <code>long<\/code> literals that exceed the <code>int<\/code> range.<\/li>\n\n\n\n<li>Use <code>f<\/code> suffix for <code>float<\/code> literals \u2014 without it, Java assumes <code>double<\/code>.<\/li>\n\n\n\n<li>Underscores in numeric literals (like <code>1_400_000_000<\/code>) are purely for readability; Java ignores them.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><em><strong>Stop just reading about data types in Java and start actually building with them. HCL GUVI&#8217;s <a href=\"https:\/\/www.guvi.in\/courses\/programming\/java-beginners\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=data-types-in-java\" target=\"_blank\" rel=\"noreferrer noopener\">Complete Java Development Course<\/a> covers everything from the basics to OOP, collections, JDBC, and design patterns through real hands-on practice, not just theory. Get 20 hours of content and an industry-recognized certificate, and pick up skills companies like Flipkart and PayPal look for. Enroll now and take your Java skills to the next level!<\/strong><\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Are Non-Primitive (Reference) Data Types in Java?<\/strong><\/h2>\n\n\n\n<p>Non-primitive types don&#8217;t store the value directly \u2014 they store a <strong>reference (memory address)<\/strong> pointing to where the actual object lives on the heap. They&#8217;re created by the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Programmer\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">programmer<\/a> (except <code>String<\/code>, which is built-in) and can be <code>null<\/code>, unlike primitives.<\/p>\n\n\n\n<p>Common non-primitive types include:<\/p>\n\n\n\n<ul>\n<li><strong>String<\/strong> \u2013 sequence of characters<\/li>\n\n\n\n<li><strong>Arrays<\/strong> \u2013 fixed-size collections of the same type<\/li>\n\n\n\n<li><strong>Classes<\/strong> \u2013 user-defined blueprints for objects<\/li>\n\n\n\n<li><strong>Interfaces<\/strong> \u2013 contracts that classes implement<\/li>\n\n\n\n<li><strong>Enums<\/strong> \u2013 fixed sets of constants<\/li>\n\n\n\n<li><strong>Wrapper classes<\/strong> \u2013 object versions of primitives (<code>Integer<\/code>, <code>Double<\/code>, <code>Character<\/code>, etc.)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Code Example: Non-Primitive Types <\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\npublic class NonPrimitiveTypesDemo {\n\n    \/\/ Custom class\n    static class Car {\n        String model;\n        int topSpeed;\n\n        Car(String model, int topSpeed) {\n            this.model = model;\n            this.topSpeed = topSpeed;\n        }\n    }\n\n    \/\/ Enum\n    enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY }\n\n    public static void main(String&#91;] args) {\n        \/\/ String\n        String greeting = \"Hello, Java in 2026!\";\n\n        \/\/ Array\n        int&#91;] scores = {90, 85, 78, 92};\n\n        \/\/ ArrayList (a common built-in class)\n        ArrayList&lt;String&gt; languages = new ArrayList&lt;&gt;();\n        languages.add(\"Java\");\n        languages.add(\"Kotlin\");\n        languages.add(\"Python\");\n\n        \/\/ Custom object\n        Car myCar = new Car(\"Tesla Model 5\", 250);\n\n        \/\/ Enum\n        Day today = Day.WEDNESDAY;\n\n        \/\/ Wrapper class\n        Integer boxedNumber = 42;\n\n        System.out.println(greeting);\n        System.out.println(\"First score: \" + scores&#91;0]);\n        System.out.println(\"Languages: \" + languages);\n        System.out.println(\"Car model: \" + myCar.model + \", Top speed: \" + myCar.topSpeed);\n        System.out.println(\"Today is: \" + today);\n        System.out.println(\"Boxed number: \" + boxedNumber);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Java in 2026!\nFirst score: 90\nLanguages: &#91;Java, Kotlin, Python]\nCar model: Tesla Model 5, Top speed: 250\nToday is: WEDNESDAY\nBoxed number: 42\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Primitive vs Non-Primitive Data Types in Java: Key Differences<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Aspect<\/strong><\/th><th><strong>Primitive<\/strong><\/th><th><strong>Non-Primitive<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Definition<\/strong><\/td><td>Built into the language<\/td><td>Created by programmer (mostly)<\/td><\/tr><tr><td><strong>Storage<\/strong><\/td><td>Stores actual value<\/td><td>Stores reference to object<\/td><\/tr><tr><td><strong>Default value<\/strong><\/td><td>Type-specific (0, false, etc.)<\/td><td><code>null<\/code><\/td><\/tr><tr><td><strong>Memory location<\/strong><\/td><td>Stack<\/td><td>Heap (object), reference on stack<\/td><\/tr><tr><td><strong>Methods<\/strong><\/td><td>None<\/td><td>Has methods (via class)<\/td><\/tr><tr><td><strong>Size<\/strong><\/td><td>Fixed, known in advance<\/td><td>Depends on the object<\/td><\/tr><tr><td><strong>Example<\/strong><\/td><td><code>int<\/code>, <code>char<\/code>, <code>boolean<\/code><\/td><td><code>String<\/code>, <code>Array<\/code>, <code>ArrayList<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Autoboxing and Unboxing in Java \u2014 Why It Matters<\/strong><\/h2>\n\n\n\n<p>Since Java is partly <a href=\"https:\/\/www.guvi.in\/blog\/oops-concepts-in-java-4-basic-concepts\/\" target=\"_blank\" rel=\"noreferrer noopener\">object-oriented<\/a>, it often needs to convert between primitives and their wrapper objects automatically. This is called <strong>autoboxing<\/strong> (primitive \u2192 wrapper) and <strong>unboxing<\/strong> (wrapper \u2192 primitive).<\/p>\n\n\n\n<p>Here&#8217;s why this actually matters in practice, not just as trivia: Java&#8217;s collections (<code>List<\/code>, <code>Map<\/code>, <code>Set<\/code>, etc.) and generics only work with objects \u2014 they were never built to hold raw primitives. <\/p>\n\n\n\n<p>So the moment you put an <code>int<\/code> into an <code>ArrayList&lt;Integer&gt;<\/code>, Java is quietly boxing it into an <code>Integer<\/code> behind the scenes, and unboxing it back to <code>int<\/code> when you pull it out. If you didn&#8217;t know this was happening, two things can bite you. <\/p>\n\n\n\n<p>First, performance \u2014 boxing millions of values in a tight loop creates millions of tiny objects, and that&#8217;s real garbage-collector overhead you didn&#8217;t ask for. <\/p>\n\n\n\n<p>Second, and more critical, <code>NullPointerException<\/code>s that seem to come out of nowhere: if a wrapper object is <code>null<\/code> and Java tries to unbox it into a primitive, it crashes, because there&#8217;s no such thing as a &#8220;null int&#8221;. Knowing when boxing happens is what lets you write code that doesn&#8217;t randomly fail in production or slow down under load.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class AutoboxingDemo {\n    public static void main(String&#91;] args) {\n        int primitiveNum = 10;\n        Integer boxedNum = primitiveNum; \/\/ autoboxing\n        int unboxedNum = boxedNum;       \/\/ unboxing\n\n        System.out.println(\"Primitive: \" + primitiveNum);\n        System.out.println(\"Boxed: \" + boxedNum);\n        System.out.println(\"Unboxed: \" + unboxedNum);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>This is especially handy when working with collections like <code>ArrayList<\/code>, which can only hold objects \u2014 not primitives.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Quick Tips for 2026 Java Developers<\/strong><\/h2>\n\n\n\n<ol>\n<li><strong>Prefer primitives for performance-critical code<\/strong> \u2014 they avoid the overhead of object creation and garbage collection.<\/li>\n\n\n\n<li><strong>Use wrapper classes when working with collections<\/strong> (<code>List&lt;Integer&gt;<\/code> instead of <code>int[]<\/code>), since generics don&#8217;t support primitives directly.<\/li>\n\n\n\n<li><strong>Watch out for <code>null<\/code> with wrapper classes<\/strong> \u2014 unboxing a <code>null<\/code> <code>Integer<\/code> throws a <code>NullPointerException<\/code>.<\/li>\n\n\n\n<li><strong>Use <code>var<\/code> for local type inference<\/strong> (available since Java 10) to reduce boilerplate, but keep the underlying type in mind.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>var count = 10;          \/\/ inferred as int\nvar name = \"Java 2026\";  \/\/ inferred as String\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Java Data Types Interview Questions for Freshers<\/strong><\/h2>\n\n\n\n<p>If you&#8217;re prepping for a <a href=\"https:\/\/www.guvi.in\/blog\/interview-questions-for-java-developer-roles-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java interview<\/a>, data types in Java are one of the first topics that comes up \u2014 it&#8217;s basic enough to ask everyone, but it separates people who&#8217;ve actually written code from people who&#8217;ve just read about it. Here are the ones that come up most often:<\/p>\n\n\n\n<p><strong>1. What is the difference between primitive and non-primitive data types in Java?<\/strong> <\/p>\n\n\n\n<p>Primitive types store the actual value directly and are built into the language (<code>int<\/code>, <code>char<\/code>, <code>boolean<\/code>, etc.). Non-primitive types store a reference to an object on the heap and are created by the programmer (with <code>String<\/code> being a notable built-in exception).<\/p>\n\n\n\n<p><strong>2. Why doesn&#8217;t Java have unsigned data types like <code>unsigned int<\/code> in C?<\/strong> <\/p>\n\n\n\n<p>Java deliberately left them out to keep the language simpler and more portable across platforms. This is also why <code>byte<\/code> and <code>short<\/code> are signed even though it can feel limiting.<\/p>\n\n\n\n<p><strong>3. What&#8217;s the default value of a <code>boolean<\/code> and an <code>int<\/code> in Java?<\/strong> <\/p>\n\n\n\n<p><code>boolean<\/code> defaults to <code>false<\/code>, <code>int<\/code> defaults to <code>0<\/code>. But this only applies to instance and static fields \u2014 local variables in Java are never auto-initialized and must be assigned before use.<\/p>\n\n\n\n<p><strong>4. Can you explain autoboxing with a real example?<\/strong> <\/p>\n\n\n\n<p>Autoboxing is when Java automatically converts a primitive to its wrapper class, like assigning an <code>int<\/code> to an <code>Integer<\/code> variable, or adding an <code>int<\/code> directly into a <code>List&lt;Integer&gt;<\/code>. It happens implicitly, without you writing any conversion code.<\/p>\n\n\n\n<p><strong>5. Why is <code>String<\/code> not a primitive type even though it behaves like one?<\/strong> <\/p>\n\n\n\n<p><code>String<\/code> is a class in Java, so it&#8217;s a non-primitive\/reference type. It just feels primitive because Java gives it special treatment \u2014 like literal syntax (<code>\"hello\"<\/code>) and operator overloading for <code>+<\/code>.<\/p>\n\n\n\n<p><strong>6. What happens if you try to store 130 in a <code>byte<\/code> variable?<\/strong> <\/p>\n\n\n\n<p>It won&#8217;t compile, because <code>byte<\/code> can only hold values from -128 to 127. You&#8217;d get a compile-time error unless you explicitly cast it, and even then the value will overflow and wrap around.<\/p>\n\n\n\n<p><strong>7. Is <code>char<\/code> a numeric type in Java?<\/strong> <\/p>\n\n\n\n<p>Yes \u2014 under the hood, <code>char<\/code> is a 16-bit unsigned integer representing a Unicode character, so it can actually be used in arithmetic operations.<\/p>\n\n\n\n<p>These are the kind of data types in Java questions interviewers ask to check whether you understand what&#8217;s happening under the hood, not just whether you can recite definitions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Primitive types give you speed and simplicity for basic values, while non-primitive types give you flexibility, structure, and behavior through objects. A solid grip on both \u2014 and when to use each \u2014 is foundational for writing clean, efficient Java code in 2026 and beyond. Whether you&#8217;re building something real or prepping for interviews, understanding data types in Java properly will save you time down the road.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong> <\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1785348228325\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">1. What are data types in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Data types in Java define the kind of value a variable can hold, like numbers, characters, or objects.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1785348232347\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">2. How many primitive data types does Java have?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Java has 8 primitive data types: byte, short, int, long, float, double, char, and boolean.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1785348233074\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">3. What is the difference between primitive and non-primitive data types in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Primitive types store actual values, while non-primitive types store references to objects.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1785348234216\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">4. Is String a primitive or non-primitive data type in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>String is a non-primitive data type, even though it behaves like one due to special syntax support.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1785348277978\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">5. What is the default value of an int in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The default value of an int is 0 for instance and static fields.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1785348291315\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">6. Why should freshers learn data types in Java for interviews?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Understanding data types in Java helps freshers explain memory usage, defaults, and wrapper classes confidently in interviews.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Every variable in Java needs a data type. It tells the compiler how much memory to set aside and what kind of operations are allowed on that data. If you&#8217;re learning Java in 2026, understanding data types in Java properly is one of the first real milestones \u2014 get this wrong and you&#8217;ll spend hours [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":128331,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,720],"tags":[],"views":"16847","authorinfo":{"name":"Abhishek Pati","url":"https:\/\/www.guvi.in\/blog\/author\/abhishek-pati\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/09\/java-data-types-300x116.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/12975"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=12975"}],"version-history":[{"count":61,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/12975\/revisions"}],"predecessor-version":[{"id":128323,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/12975\/revisions\/128323"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/128331"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=12975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=12975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=12975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}