CodeLangs AISoftware Training Institute
Java for Beginners (Marathi)/Java Data Types
Dashboard
Chapter 6 · Java for Beginners

Java Data Types

3,820
words
18
min read
50
practice items
Interactive learning

Practice lab

Question 1 of 24 00:00

Learning Outcomes#

हा chapter पूर्ण केल्यानंतर तुम्ही independently:

  • Java मधील primitive type आणि reference type यांमधला फरक explain करू शकाल.
  • Java चे सर्व 8 primitive data types identify करू शकाल.
  • byte, short, int, long यांपैकी requirement नुसार योग्य integer type निवडू शकाल.
  • float आणि double approximate decimal values का store करतात हे explain करू शकाल.
  • char हा फक्त "एक visible character" नसून UTF-16 code unit आहे हे समजू शकाल.
  • boolean कधी वापरायचा आणि त्याबद्दलचा common "1 bit / 1 byte" misconception ओळखू शकाल.
  • String, arrays आणि custom objects हे primitive नसून reference types आहेत हे identify करू शकाल.
  • चुकीचा data type निवडल्यामुळे होणाऱ्या compile-time किंवा design problems ओळखू शकाल.
  • real project requirement पाहून practical data-type decision घेऊ शकाल.
  • beginner-level Java interview मध्ये data types बद्दल strong answer देऊ शकाल.
Version context: हा chapter Java SE 26 च्या current language specification ला baseline मानून तयार केला आहे. Java SE 26 March 2026 मध्ये release झाला आहे; Java 25 हा current LTS release आहे. या chapter मधील core primitive-type rules अनेक Java versions पासून stable आहेत.

Data Type म्हणजे काय?#

Programming करताना आपण फक्त values store करत नाही. त्या value चा meaning काय आहे, ती value कोणत्या प्रकारची आहे आणि तिच्यावर कोणत्या प्रकारचे operations logically लागू होतात हे language ला समजले पाहिजे.

उदाहरण:

JAVA
int studentCount = 50;
boolean courseActive = true;
char grade = 'A';

इथे:

  • 50 → whole number
  • true → yes/no state
  • 'A' → character-related value

या तीनही values चा meaning वेगळा आहे. त्यामुळे Java त्यांना वेगवेगळ्या types मध्ये represent करते.

Definition: A data type defines the kind of value a variable can represent and determines the operations that are valid for that value.

म्हणजे variable मध्ये कोणत्या प्रकारची value ठेवायची हे data type ठरवतो.

Simplified view:

TEXT
Variable
   ↓
Data Type
   ↓
What kind of value is allowed?
   ↓
How can that value be used?

Java मध्ये types चे दोन मोठे groups आहेत:

TEXT
Java Types
│
├── Primitive Types
│   ├── byte
│   ├── short
│   ├── int
│   ├── long
│   ├── float
│   ├── double
│   ├── char
│   └── boolean
│
└── Reference Types
    ├── Classes
    ├── Interfaces
    └── Arrays

Java Language Specification types ना primitive आणि reference types मध्ये divide करते. Java मध्ये exactly आठ primitive types आहेत.


Primitive Data Types#

Definition: A primitive type is a data type predefined by the Java programming language and identified by a Java keyword.

Primitive type म्हणजे Java language ने आधीपासून define केलेला basic type.

आपल्याला int किंवा boolean स्वतः create करावे लागत नाहीत.

Java मध्ये हे built-in आहेत.

TEXT
Primitive Types

Numeric
├── Integral
│   ├── byte
│   ├── short
│   ├── int
│   ├── long
│   └── char
│
└── Floating Point
    ├── float
    └── double

Non-numeric logical type
└── boolean

Java SE specification नुसार byte, short, int, long हे signed two's-complement integer types आहेत; char हा 16-bit unsigned integral type आहे जो UTF-16 code units represent करतो; float आणि double IEEE 754 binary floating-point types आहेत; boolean ला फक्त true आणि false values आहेत.


Primitive Types — Big Picture#

TypeCategoryValue size / modelTypical purpose
byteInteger8-bit signedbinary data, tightly constrained values
shortInteger16-bit signedspecific 16-bit data requirements
intInteger32-bit signednormal whole numbers
longInteger64-bit signedvery large whole numbers
floatFloating pointIEEE 754 binary32approximate decimals where float precision/storage is intentional
doubleFloating pointIEEE 754 binary64normal approximate decimal calculations
charIntegral / character-related16-bit unsigned UTF-16 code unitone UTF-16 code unit
booleanLogicaltrue / falseconditions and states

Important: या bit sizes वरून Java object किंवा variable JVM memory मध्ये नेमके तेवढेच bytes घेईल असा निष्कर्ष काढू नका. येथे आपण language-level value representation/range शिकत आहोत.


1. byte#

Definition: byte is an 8-bit signed integer primitive type with values from -128 to 127.

byte हा Java मधील सर्वात छोट्या range चा signed integer primitive type आहे.

Range:

TEXT
Minimum: -128
Maximum:  127

हा range Java specification ने निश्चित केलेला आहे.

Example#

JAVA
public class ByteExample {
    public static void main(String[] args) {
        byte signalStrength = 95;

        System.out.println(signalStrength);
    }
}

Expected output:

TEXT
95

byte कुठे useful असतो?#

Normal business application मध्ये एखादी value छोटी आहे म्हणून लगेच byte वापरणे आवश्यक नसते.

उदाहरण:

JAVA
byte age = 35;

Technically काही ages साठी हे चालेल.

पण practical application मध्ये:

JAVA
int age = 35;

हा approach अधिक natural असतो.

byte विशेष useful होतो जेव्हा requirement स्वतः 8-bit data बद्दल असते.

उदाहरण:

  • binary file content
  • image bytes
  • network packet bytes
  • compressed data
  • byte arrays
  • low-level encoded data

Example:

JAVA
byte[] fileData = {10, 20, 30, 40};

इथे एक important distinction आहे:

TEXT
byte        → primitive type
byte[]      → reference type

Array बद्दल detailed chapter नंतर असू शकतो; इथे फक्त type distinction लक्षात ठेवा.

Decision Rule#

Value छोटी आहे म्हणून byte वापरू नका. Requirement genuinely 8-bit range/data मागत असेल तेव्हा byte consider करा.

2. short#

Definition: short is a 16-bit signed integer primitive type with values from -32,768 to 32,767.

Range:

TEXT
Minimum: -32768
Maximum:  32767

Example:

JAVA
public class ShortExample {
    public static void main(String[] args) {
        short sensorReading = 25000;

        System.out.println(sensorReading);
    }
}

Expected output:

TEXT
25000

मग short का आहे?#

short हा byte पेक्षा मोठा आणि int पेक्षा छोटा integer range देतो.

तो useful असू शकतो जेव्हा:

  • external data format specifically signed 16-bit value वापरतो
  • protocol field 16-bit आहे
  • मोठ्या primitive arrays मध्ये representation requirement महत्त्वाची आहे
  • hardware/sensor-related values 16-bit format मध्ये येतात

Normal application code मध्ये मात्र int अधिक commonly used असतो.

Weak Thinking#

TEXT
Student count maximum 500 आहे.
↓
500 byte मध्ये बसत नाही.
↓
म्हणून short वापरतो.

हे technically possible आहे.

पण practical question आहे:

short वापरण्याची actual requirement आहे का?

नसेल तर:

JAVA
int studentCount = 500;

हा clearer choice असू शकतो.


3. int#

Definition: int is a 32-bit signed integer primitive type with values from -2,147,483,648 to 2,147,483,647.

Range:

TEXT
Minimum: -2147483648
Maximum:  2147483647

Beginner Java development मध्ये whole-number values साठी int हा सर्वात important type आहे.

Examples:

JAVA
int studentCount = 120;
int totalLessons = 85;
int quantity = 25;
int retryCount = 3;

Practical Example#

Requirement:

एका course मध्ये किती students enrolled आहेत हे store करायचे आहे.

Analysis:

  • decimal नाही
  • yes/no नाही
  • character नाही
  • normal whole number आहे
  • expected range billions पेक्षा कमी आहे

Choice:

JAVA
int enrolledStudents = 85000;

Why not byte?#

कारण enrollment 127 पेक्षा जास्त जाऊ शकतो.

Why not short?#

32,767 पेक्षा मोठी संख्या possible आहे.

Why not long?#

वापरू शकतो, पण जर requirement int range मध्ये comfortably बसत असेल तर int simpler आणि natural choice आहे.

Must Know#

Whole-number requirement पाहिल्यावर first practical question:

int range पुरेशी आहे का?

जर हो — सामान्यतः int strong default choice आहे.

जर नाही — long consider करा.


4. long#

Definition: long is a 64-bit signed integer primitive type used for whole-number values that may exceed the range of int.

Range:

TEXT
Minimum: -9223372036854775808
Maximum:  9223372036854775807

Example:

JAVA
public class LongExample {
    public static void main(String[] args) {
        long videoViews = 5000000000L;

        System.out.println(videoViews);
    }
}

Expected output:

TEXT
5000000000

इथे L दिसत आहे.

आत्ता फक्त एवढे समजा:

compiler ला हा whole-number value long म्हणून treat करायचा आहे हे L दर्शवते.

Integer literal rules आपण Literals and Constants chapter मध्ये detail मध्ये शिकणार आहोत.

long कधी वापरायचा?#

उदाहरण requirement:

Platform वरील lifetime API requests count store करायचा आहे. Count 2.1 billion पेक्षा जास्त जाऊ शकतो.

int unsafe choice ठरेल.

JAVA
long totalApiRequests = 5000000000L;

Common Real-World Areas#

  • very large counters
  • large file sizes
  • timestamps represented as integer units
  • massive transaction/event counts
  • database identifiers जे BIGINT सारख्या range शी map होतात

Important#

long म्हणजे decimal type नाही.

JAVA
long

हा अजूनही whole-number integer type आहे.


Integer Types Comparison#

TypeBitsMinimumMaximum
byte8-128127
short16-32,76832,767
int32-2,147,483,6482,147,483,647
long64-9,223,372,036,854,775,8089,223,372,036,854,775,807

Practical Selection#

TEXT
Need a whole number?
        ↓
Is it normal application data within int range?
        ├── Yes → int
        │
        └── No
             ↓
Does it need a larger integer range?
             ├── Yes → long
             │
             └── Is there a specific 8/16-bit requirement?
                    ├── 8-bit  → byte
                    └── 16-bit → short

5. float#

आता whole numbers सोडून decimal values कडे येऊ.

उदाहरण:

TEXT
36.5
4.7
12.75
98.6

Java मध्ये decimal-like approximate numeric calculations साठी मुख्य primitive types:

TEXT
float
double

Definition: float is a 32-bit IEEE 754 binary floating-point primitive type used for approximate numeric values.

float 32-bit IEEE 754 binary32 format वापरतो.

Example:

JAVA
public class FloatExample {
    public static void main(String[] args) {
        float temperature = 36.5f;

        System.out.println(temperature);
    }
}

Expected output:

TEXT
36.5

इथे f का आहे याची literal-level detail पुढच्या chapter मध्ये येईल.

आत्ता एवढे लक्षात ठेवा:

JAVA
float temperature = 36.5f;

हा valid beginner form आहे.

float approximate का आहे?#

Computer binary system वापरतो.

आपण decimal मध्ये सहज लिहू शकणारी प्रत्येक value binary floating-point मध्ये exact represent होईलच असे नाही.

Conceptual view:

TEXT
Decimal value
     ↓
Binary floating-point representation
     ↓
Some values exact
Some values approximate

float roughly 6–7 significant decimal digits precision देतो.

म्हणून:

float म्हणजे "decimal number stored perfectly" असा अर्थ नाही.

6. double#

Definition: double is a 64-bit IEEE 754 binary floating-point primitive type that provides more precision than float.

double IEEE 754 binary64 format वापरतो.

Example:

JAVA
public class DoubleExample {
    public static void main(String[] args) {
        double distanceKm = 125.75;

        System.out.println(distanceKm);
    }
}

Expected output:

TEXT
125.75

double roughly 15–16 significant decimal digits precision देतो.

Normal approximate decimal calculations मध्ये double हा float पेक्षा अधिक common choice आहे.


float vs double#

Pointfloatdouble
FormatIEEE 754 binary32IEEE 754 binary64
Size32-bit64-bit
PrecisionLowerHigher
Approx decimal precision~6–7 significant digits~15–16 significant digits
General decimal calculationsLess commonUsually preferred
Large memory-sensitive numeric arraysCan be usefulHigher memory requirement
Exact currencyNot appropriateNot appropriate

Practical Decision#

Requirement:

User ने किती kilometers travel केले ते approximate measurement store करायचे आहे.
JAVA
double distanceKm = 238.75;

Reason:

  • decimal measurement
  • high precision useful
  • double natural choice

Floating-Point Precision — Very Important#

हा code पहा:

JAVA
public class PrecisionExample {
    public static void main(String[] args) {
        double result = 0.1 + 0.2;

        System.out.println(result);
    }
}

Expected output:

TEXT
0.30000000000000004

Beginner reaction:

Java calculation चुकीची करते का?

नाही.

Problem Java arithmetic मध्ये नाही.

Reason:

0.1 आणि 0.2 सारख्या काही decimal fractions चे exact finite representation binary floating-point format मध्ये होत नाही.

म्हणून result जवळचा representable binary value असतो.

Real-World Consequence#

Financial requirement:

TEXT
₹10.10 + ₹20.20

Exact monetary calculations मध्ये float किंवा double वर blindly depend करणे योग्य नाही.

Java projects मध्ये exact decimal monetary calculations साठी सामान्यतः java.math.BigDecimal सारखा decimal arithmetic type वापरला जातो.

BigDecimal हा primitive नाही — तो reference type आहे.

त्याची complete usage हा या beginner data-type chapter चा विषय नाही.

Rule#

float आणि double हे approximate floating-point calculations साठी आहेत; exact financial decimal arithmetic साठी नाहीत.

7. char#

Beginner courses मध्ये char ला अनेकदा फक्त:

"single character store करतो"

इतकेच सांगितले जाते.

ते beginner-level intuition म्हणून useful आहे, पण complete technical understanding नाही.

Definition: char is a 16-bit unsigned integral primitive type whose values represent UTF-16 code units.

char range:

TEXT
0 to 65535

किंवा:

TEXT
'\u0000' to '\uffff'

Example:

JAVA
public class CharExample {
    public static void main(String[] args) {
        char grade = 'A';

        System.out.println(grade);
    }
}

Expected output:

TEXT
A

Marathi character:

JAVA
public class MarathiCharExample {
    public static void main(String[] args) {
        char letter = 'म';

        System.out.println(letter);
    }
}

Expected output:

TEXT

char हा numeric family मध्ये का आहे?#

Java specification मध्ये char हा integral type आहे.

तो signed नाही.

Range:

TEXT
0 – 65535

म्हणून technically char value UTF-16 code unit number represent करतो.


char आणि Unicode बद्दल Professional Understanding#

Common statement:

One char = one Unicode character.

हे नेहमी true नाही.

Java char एक UTF-16 code unit represent करतो.

अनेक common characters एका char मध्ये represent होतात.

पण काही Unicode characters — विशेषतः अनेक emoji आणि supplementary characters — represent करण्यासाठी दोन UTF-16 code units लागतात.

Conceptually:

TEXT
Common BMP character
        ↓
one UTF-16 code unit
        ↓
one char

Some supplementary Unicode character
        ↓
two UTF-16 code units
        ↓
two char values

Beginner म्हणून आत्ता deep Unicode processing करण्याची गरज नाही.

फक्त misconception टाळा:

char म्हणजे प्रत्येक possible visible Unicode symbol साठी guaranteed one-value container नाही.

char vs String#

JAVA
char grade = 'A';
String courseName = "Java";

Difference:

TEXT
char
↓
primitive type
↓
one UTF-16 code unit

String
↓
reference type
↓
sequence of characters/text

String ला आपण reference type introduction मध्ये लगेच पाहणार आहोत.

Single quotes आणि double quotes चे complete literal rules पुढच्या Literals and Constants chapter मध्ये येतील.


8. boolean#

Application मध्ये खूप values numeric नसतात.

उदाहरण:

TEXT
Is user logged in?
Is payment successful?
Is email verified?
Is course active?

या प्रश्नांची natural answers:

TEXT
Yes / No
True / False

Java मध्ये यासाठी boolean.

Definition: boolean is a primitive type that has exactly two possible values: true and false.

Example:

JAVA
public class BooleanExample {
    public static void main(String[] args) {
        boolean courseActive = true;
        boolean paymentCompleted = false;

        System.out.println(courseActive);
        System.out.println(paymentCompleted);
    }
}

Expected output:

TEXT
true
false

Wrong Mental Model#

काही languages किंवा low-level systems मुळे beginners असे assume करतात:

TEXT
1 = true
0 = false

Java मध्ये:

JAVA
boolean active = true;

valid.

पण:

JAVA
boolean active = 1;

valid Java नाही.

Java मध्ये boolean आणि numeric primitive types वेगळे आहेत.


boolean किती bytes असतो?#

हा interview मध्ये trick question होऊ शकतो.

Common weak answer:

boolean is 1 bit.

दुसरा weak answer:

boolean is always 1 byte.

Java Language Specification boolean साठी language-level fixed storage size define करत नाही.

ती फक्त दोन values define करते:

TEXT
true
false

JVM/internal implementation representation वेगळा विषय आहे.

म्हणून beginner interview साठी strong answer:

Java defines the values of boolean as true and false; the Java Language Specification does not define a fixed memory size for a boolean variable.

Complete Primitive Type Comparison#

TypeCategoryLanguage-level representationImportant use
byteSigned integer8-bitraw/binary or constrained 8-bit data
shortSigned integer16-bitspecific 16-bit requirements
intSigned integer32-bitnormal whole numbers
longSigned integer64-bitvery large whole numbers
floatFloating point32-bit IEEE 754 binary32approximate lower-precision decimals
doubleFloating point64-bit IEEE 754 binary64approximate higher-precision decimals
charUnsigned integral16-bit UTF-16 code unitcharacter code unit
booleanLogicaltrue / false; fixed storage size not specified by JLSstates/conditions

Reference Data Type Introduction#

आत्तापर्यंत आपण values पाहिल्या:

JAVA
int quantity = 10;
double rating = 4.8;
boolean active = true;

हे primitive types आहेत.

पण application मध्ये आपण complex things सुद्धा represent करतो:

TEXT
Student
Course
Order
Customer
String
Array

यासाठी reference types येतात.

Definition: A reference type is a Java type whose values are references to objects or arrays rather than primitive values.

Java specification मध्ये class types, interface types आणि array types reference types मध्ये येतात.


First Reference Example — String#

JAVA
String studentName = "Sneha";

String primitive नाही.

तो class-based reference type आहे.

Simplified mental model:

TEXT
studentName
     │
     │ reference
     ▼
String object representing "Sneha"

Language-level perspective:

TEXT
Primitive variable
int age = 25;

age
 ↓
25


Reference variable
String name = "Sneha";

name
 ↓
reference
 ↓
String object

Important Accuracy Note#

"Reference stores memory address directly" असे beginner explanation म्हणून बोलणे tempting आहे.

पण Java language specification programmer ला raw machine memory address expose करत नाही.

Strong terminology:

A reference variable holds a reference value to an object or array.

Raw pointer/address असा शब्द वापरण्याची गरज नाही.


Primitive vs Reference Type#

FeaturePrimitive TypeReference Type
Defined examplesint, double, char, booleanString, arrays, classes
Valueprimitive valuereference to object/array
Built-in primitive keywordsYesNot generally primitive keywords
Can represent nullNoYes
Exampleint age = 25;String name = "Asha";
Methods directly belonging to valuePrimitive itself is not an objectObjects can expose methods
RangeDefined for numeric primitivesDepends on referenced type/object

Reference variables can also hold null, meaning they currently refer to no object. null itself is a literal, त्यामुळे त्याचे literal rules पुढच्या chapter मध्ये detail मध्ये शिकू.


A Connected Example#

एक online learning system imagine करा.

Requirement:

Student चे basic learning statistics store करायचे आहेत.
JAVA
public class StudentDataExample {
    public static void main(String[] args) {
        String studentName = "Asha";
        int completedLessons = 42;
        long totalWatchSeconds = 250000L;
        double averageScore = 87.5;
        char grade = 'A';
        boolean emailVerified = true;

        System.out.println(studentName);
        System.out.println(completedLessons);
        System.out.println(totalWatchSeconds);
        System.out.println(averageScore);
        System.out.println(grade);
        System.out.println(emailVerified);
    }
}

Expected output:

TEXT
Asha
42
250000
87.5
A
true

Data-type reasoning:

RequirementTypeReason
Student nameStringtext, so reference type
Completed lessonsintnormal whole-number count
Total watch secondslongpotentially very large lifetime counter
Average scoredoubledecimal measurement
Gradecharone code unit such as A
Email verifiedbooleantrue/false state

हीच professional thinking हवी:

Value पाहून type निवडू नका; requirement + meaning + range + precision पाहून type निवडा.

Data Type Selection Framework#

Requirement वाचल्यावर हे questions विचारा.

TEXT
1. Value whole number आहे का?
   │
   ├── Yes
   │    ↓
   │   int range पुरेशी आहे?
   │    ├── Yes → int
   │    └── No  → long
   │
   │   Special 8/16-bit requirement?
   │    ├── Yes → byte / short
   │
   └── No
        ↓
2. Decimal measurement आहे?
        ├── Yes
        │    ├── Normal higher precision → double
        │    └── Explicit float requirement → float
        │
        ↓
3. True/False state आहे?
        └── boolean

4. One UTF-16 code unit आहे?
        └── char

5. Text/object/array आहे?
        └── reference type

2. Practical / Real-World Application#

Scenario 1 — Course Platform#

Requirement:

TEXT
Course ID
Course title
Number of chapters
Course rating
Published or not

Possible design:

JAVA
String courseId = "JAVA-BEG-101";
String courseTitle = "Java Beginner Course";
int chapterCount = 24;
double rating = 4.8;
boolean published = true;

Why courseId String?#

ID मध्ये letters आणि hyphen आहेत.

तो calculation करण्यासाठी number नाही.

ID technically digits-only असला तरी काही systems मध्ये ID हा business identifier असतो, mathematical quantity नाही.

हा difference important आहे:

TEXT
Quantity → numeric type
Identifier → often String or domain-specific type

Scenario 2 — Massive Video Platform Counter#

Requirement:

एका popular video चा view count 3,800,000,000 आहे.

int maximum:

TEXT
2147483647

तो पुरेसा नाही.

Better:

JAVA
long videoViews = 3800000000L;

Scenario 3 — Raw Image Data#

Image bytes process करायचे आहेत.

JAVA
byte[] imageData = {10, 20, 30};

इथे:

TEXT
imageData
↓
reference variable

byte[]
↓
array reference type

each element
↓
byte primitive

हा primitive + reference relationship चा excellent example आहे.


Scenario 4 — Temperature Measurements#

Single measurement:

JAVA
double temperature = 36.75;

Millions of scientific/graphics values आणि application explicitly 32-bit float precision/storage design वापरत असेल:

JAVA
float measurement = 36.75f;

Important:

float फक्त "value लहान आहे" म्हणून निवडत नाही.

Precision आणि data representation requirement महत्त्वाची आहे.


Scenario 5 — Payment Status#

Bad design:

JAVA
int paymentStatus = 1;

जर requirement फक्त:

TEXT
paid / not paid

असेल, तर:

JAVA
boolean paymentCompleted = true;

अधिक expressive आहे.

Code स्वतः meaning सांगतो.


Scenario 6 — Money#

Weak design:

JAVA
double accountBalance = 100.10;

जर exact accounting calculation करायची असेल तर binary floating-point precision problem होऊ शकतो.

Professional direction:

TEXT
Exact financial decimal arithmetic
↓
BigDecimal

BigDecimal reference type आहे.

Details पुढच्या/advanced chapters मध्ये.


Requirement → Type Thinking#

RequirementRecommended directionWhy
Number of attemptsintnormal whole number
Global event count > 2.1Blonglarger integer range
Raw network bytesbyte / byte[]binary data
16-bit protocol fieldshortprotocol explicitly requires signed 16-bit
Average response timedoubledecimal measurement
Millions of float-format coordinatesfloatexplicit float representation requirement
Grade Acharsingle UTF-16 code unit
Is account enabled?booleantwo-state value
Full nameStringtext/reference type

3. Common Mistakes & Misconceptions#

Mistake 1 — "Small number म्हणजे byte"#

Why it sounds believable#

byte छोटा आहे आणि 25 सहज range मध्ये बसतो.

Weak thinking#

JAVA
byte age = 25;
byte retryCount = 3;
byte quantity = 10;

Correct Understanding#

Data type फक्त current value वर निवडू नका.

Future valid range, API compatibility, readability आणि actual requirement पहा.

Normal application counters साठी:

JAVA
int retryCount = 3;

अनेकदा clearer आहे.


Mistake 2 — byte किंवा short ने application नेहमी faster होते#

Smaller numerical range म्हणजे automatically faster असा rule नाही.

JVM implementation, CPU operations, object layout, arrays, alignment आणि optimizations वेगळे topics आहेत.

या chapter मधील rule:

byte/short use करा जेव्हा data requirement justify करते; imaginary micro-optimization साठी नाही.

Mistake 3 — float आणि double exact decimal types आहेत#

Example:

JAVA
double value = 0.1 + 0.2;
System.out.println(value);

Output:

TEXT
0.30000000000000004

Root Cause#

Binary floating-point representation.

Prevention#

Exact financial decimal arithmetic साठी floating-point primitives blindly वापरू नका.


Mistake 4 — float declaration#

Incorrect:

JAVA
float temperature = 36.5;

हे compile होत नाही कारण 36.5 बद्दल literal typing rules लागू होतात.

Current chapter मध्ये working form:

JAVA
float temperature = 36.5f;

f suffix चे full rules पुढच्या chapter मध्ये.


Mistake 5 — Large long value लिहिताना literal issue#

Intent:

JAVA
long population = 5000000000;

हा source form compile होत नाही कारण integer literal rules लागू होतात.

Working form:

JAVA
long population = 5000000000L;

L बद्दल full discussion पुढच्या chapter मध्ये.


Mistake 6 — boolean = 0 or 1#

Incorrect:

JAVA
boolean active = 1;

Correct:

JAVA
boolean active = true;

Java boolean numeric primitive नाही.


Mistake 7 — boolean is always 1 bit#

Java language specification fixed boolean storage size define करत नाही.

Correct answer:

TEXT
boolean has two language-level values:
true
false

Mistake 8 — char म्हणजे कोणताही Unicode symbol exactly#

Correct:

TEXT
char = one UTF-16 code unit

काही Unicode characters साठी दोन code units लागतात.


Mistake 9 — String primitive आहे#

Incorrect classification:

TEXT
String → primitive

Correct:

TEXT
String → reference type

Java primitive list fixed आहे:

TEXT
byte
short
int
long
float
double
char
boolean

String त्या list मध्ये नाही.


Mistake 10 — Reference variable म्हणजे object स्वतःच#

Simplified correct model:

TEXT
String name
    ↓
reference value
    ↓
String object

Reference variable आणि referenced object हे conceptually वेगळे आहेत.


4. Hands-On Practice#

Practice 1 — Choose the Correct Type#

प्रत्येक requirement साठी योग्य type निवडा.

A#

एका classroom मध्ये students ची संख्या store करायची आहे.

Maximum expected:

TEXT
500

Options:

TEXT
byte
short
int
long

Attempt first.

Hint 1: 500 byte मध्ये बसत नाही.

Hint 2: short मध्ये बसतो, पण special 16-bit requirement नाही.

Solution:

JAVA
int studentCount = 500;

Why?#

Normal application whole-number count असल्यामुळे int practical choice आहे.


B#

System lifetime event count:

TEXT
6,000,000,000

Solution:

JAVA
long eventCount = 6000000000L;

Reason:

int range insufficient.


C#

User email verified आहे का?

Solution:

JAVA
boolean emailVerified = true;

D#

Student grade:

TEXT
A

जर requirement exactly one UTF-16 code unit असेल:

JAVA
char grade = 'A';

E#

Student full name:

TEXT
Asha Patil

Solution:

JAVA
String studentName = "Asha Patil";

String reference type आहे.


Practice 2 — Find the Problem#

Code#

JAVA
byte score = 200;

Problem काय?

Hint: byte maximum check करा.

Solution:

byte maximum 127 आहे.

If score 200 valid असू शकतो:

JAVA
int score = 200;

Practice 3 — Data Modeling#

Requirement:

A learning platform wants to store:

  • learner name
  • number of completed chapters
  • average quiz score
  • account active status
  • current grade
  • lifetime seconds watched

Try first.

Solution:

JAVA
String learnerName = "Rahul";
int completedChapters = 18;
double averageQuizScore = 82.75;
boolean accountActive = true;
char currentGrade = 'A';
long lifetimeWatchSeconds = 450000L;

Practice 4 — Precision Investigation#

Predict output before running mentally:

JAVA
public class DecimalPractice {
    public static void main(String[] args) {
        double result = 0.1 + 0.2;

        System.out.println(result);
    }
}

Expected output:

TEXT
0.30000000000000004

Question:

हा Java bug आहे का?

Answer:

No.

हा binary floating-point representation चा expected consequence आहे.


Practice 5 — Primitive or Reference?#

Classify:

TEXT
int
String
boolean
byte[]
double
char
String[]

Solution:

TypeClassification
intPrimitive
StringReference
booleanPrimitive
byte[]Reference
doublePrimitive
charPrimitive
String[]Reference

Practice 6 — Explain Your Choice#

Requirement:

A warehouse stores quantity from 0 to 100,000.

Which is better?

TEXT
short
int
long

Answer:

TEXT
int

Why?

short maximum 32,767 असल्यामुळे insufficient.

long works पण unnecessary range आहे.

int requirement comfortably cover करतो.


5. Interview Preparation#

Q1. What is a data type in Java?#

Answer: A data type defines the kind of value a variable can represent and determines how that value can be used by the Java language.

What the interviewer is testing: Basic understanding of Java's type system.

Common weak answer: "A data type tells how much memory a variable uses."

That answer is incomplete because a type is not only about memory; it also defines the value domain and valid language behavior.


Q2. What are the eight primitive data types in Java?#

Answer:

TEXT
byte
short
int
long
float
double
char
boolean

Q3. How are Java primitive types classified?#

Answer: The numeric primitive types are divided into integral and floating-point types. The integral types are byte, short, int, long, and char. The floating-point types are float and double. boolean represents logical truth values.


Q4. What is the difference between int and long?#

Answer: int is a 32-bit signed integer type with a range from -2,147,483,648 to 2,147,483,647. long is a 64-bit signed integer type and supports a much larger range. Use long when a valid value can exceed the int range.


Q5. When would you use byte instead of int?#

Answer: Use byte when the data itself has an 8-bit signed representation or when working with binary data such as byte arrays, encoded files, or protocol fields. A small business value alone is usually not enough reason to choose byte.

What the interviewer is testing: Whether you choose data types based on requirements instead of only their current value.


Q6. Is short commonly preferred for all small integer values?#

Answer: No. Although short supports smaller integer values than int, normal application code commonly uses int unless there is a specific 16-bit representation or storage requirement.


Q7. What is the difference between float and double?#

Answer: float uses the IEEE 754 binary32 format and provides lower precision. double uses binary64 and provides significantly higher precision. double is generally preferred for ordinary approximate decimal calculations unless there is a specific reason to use float.


Q8. Are float and double suitable for exact currency calculations?#

Answer: Not generally. Binary floating-point values cannot exactly represent many decimal fractions, so exact financial calculations usually require a decimal arithmetic type such as BigDecimal.

Common weak answer: "Use double because it has more precision."

More precision does not make binary floating-point exact for decimal money.


Q9. Why can 0.1 + 0.2 produce 0.30000000000000004?#

Answer: Because values such as 0.1 and 0.2 do not have exact finite representations in binary floating-point. Java stores nearby representable IEEE 754 values, so the calculation can expose a small representation difference.


Q10. What is char in Java?#

Answer: char is a 16-bit unsigned integral primitive type whose values represent UTF-16 code units.


Q11. Does one Java char always represent one complete Unicode character?#

Answer: No. Many characters fit in one UTF-16 code unit, but supplementary Unicode characters require two UTF-16 code units and therefore cannot be represented by a single char value.


Q12. What values can a boolean contain?#

Answer:

TEXT
true
false

Java does not treat numeric values such as 0 and 1 as boolean values.


Q13. What is the size of a boolean in Java?#

Answer: The Java Language Specification does not define a fixed storage size for a boolean variable. At the language level, it defines only the two values true and false.

What the interviewer is testing: Whether the candidate distinguishes language specification from JVM implementation details.


Q14. Is String a primitive type?#

Answer: No. String is a class and therefore a reference type.


Q15. What is a reference type?#

Answer: A reference type is a Java type whose values refer to objects or arrays rather than being primitive values themselves. Class types, interface types, and array types are reference types.


Q16. What is the key difference between a primitive variable and a reference variable?#

Answer: A primitive variable contains a primitive value of its type. A reference variable contains a reference value that refers to an object or array, or it can hold null.


Q17. Is an array primitive in Java?#

Answer: No. Arrays are reference types. For example, byte is primitive, but byte[] is a reference type whose elements are byte values.


Q18. Why should data types be chosen from requirements instead of the current sample value?#

Answer: Because the real requirement determines the valid range, precision, semantic meaning, interoperability needs, and future values. A sample value may fit into a smaller type even when future valid values do not.


Q19. Why is int commonly preferred for normal whole-number values?#

Answer: int provides a practical 32-bit signed range and naturally represents most ordinary application counters and quantities. Smaller types such as byte and short should generally be selected when their specific representation is meaningful to the requirement.


Q20. Are Java integer primitive sizes platform-dependent?#

Answer: No. Java defines the value ranges of its primitive integral types consistently across implementations. For example, int is defined as a 32-bit signed two's-complement integer type.


6. Quick Revision#

Java Type System#

TEXT
Java Types
├── Primitive
└── Reference

Eight Primitive Types#

TEXT
byte
short
int
long
float
double
char
boolean

Integer Types#

TEXT
byte  → 8-bit
short → 16-bit
int   → 32-bit
long  → 64-bit

Normal Whole Number#

JAVA
int count = 100;

Very Large Whole Number#

JAVA
long views = 5000000000L;

Decimal#

JAVA
double temperature = 36.75;

Explicit Float#

JAVA
float temperature = 36.75f;

Character Code Unit#

JAVA
char grade = 'A';

Logical State#

JAVA
boolean active = true;

Text#

JAVA
String name = "Asha";

String → reference type.

Key Rules#

  • byte range: -128 to 127
  • short range: -32768 to 32767
  • int range: -2147483648 to 2147483647
  • long → larger 64-bit signed integer
  • float → IEEE 754 binary32
  • double → IEEE 754 binary64
  • floating-point values can be approximate
  • char → 16-bit UTF-16 code unit
  • boolean → only true / false
  • JLS does not define fixed boolean storage size
  • String and arrays are reference types
  • exact financial decimal calculations should not blindly use float/double

7. You Should Now Be Able To#

तुम्हाला आता independently खालील tasks करता आले पाहिजेत:

  • आठही Java primitive types नावाने सांगणे.
  • integer requirement साठी byte, short, int, long compare करणे.
  • normal whole-number values साठी int का practical आहे ते explain करणे.
  • int range पेक्षा मोठ्या values साठी long निवडणे.
  • float आणि double मधला precision difference explain करणे.
  • floating-point decimal approximation explain करणे.
  • exact money calculation साठी double blindly का वापरू नये हे सांगणे.
  • char technically UTF-16 code unit आहे हे explain करणे.
  • boolean ला 0 किंवा 1 assign करता येत नाही हे सांगणे.
  • boolean fixed storage size बद्दलचा misconception correct करणे.
  • String primitive नाही हे explain करणे.
  • primitive आणि reference type classify करणे.
  • byte[] मध्ये array reference type आणि byte element primitive type यातील distinction explain करणे.
  • business requirement वरून योग्य data type reason करून select करणे.

8. Final Challenge#

Requirement#

तुम्ही online learning platform चा Learner Progress Summary design करत आहात.

खालील information store करायची आहे:

  1. Learner full name
  2. Learner age
  3. Completed lessons
  4. Lifetime watched seconds — future मध्ये 3 billion पेक्षा जास्त होऊ शकतात
  5. Average assessment percentage
  6. Current grade such as A
  7. Email verified or not
  8. Raw profile image bytes

Your Task#

प्रत्येक field साठी:

  1. Java type निवडा.
  2. तो primitive आहे की reference ते सांगा.
  3. choice चे reason सांगा.

Attempt केल्याशिवाय solution पाहू नका.


Final Challenge Solution#

JAVA
String learnerName = "Asha Patil";
int age = 25;
int completedLessons = 82;
long lifetimeWatchSeconds = 3500000000L;
double averagePercentage = 87.75;
char currentGrade = 'A';
boolean emailVerified = true;

byte[] profileImageData = {10, 20, 30};

Reasoning#

FieldTypePrimitive / ReferenceReason
Learner nameStringReferencetext
AgeintPrimitivenormal whole number
Completed lessonsintPrimitivenormal counter
Watch secondslongPrimitivemay exceed int range
PercentagedoublePrimitiveapproximate decimal
GradecharPrimitiveone UTF-16 code unit
Email verifiedbooleanPrimitivetwo-state value
Image databyte[]Referencearray of raw byte values

Professional Question#

Would you always store age directly?

In many real systems, date of birth may be the authoritative data and age may be calculated because age changes over time.

हा database/domain-design concern आहे; data types समजण्यासाठी इथे int age acceptable आहे.