Learning Outcomes#
हा chapter पूर्ण केल्यानंतर तुम्ही independently:
- Java मधील primitive type आणि reference type यांमधला फरक explain करू शकाल.
- Java चे सर्व 8 primitive data types identify करू शकाल.
byte,short,int,longयांपैकी requirement नुसार योग्य integer type निवडू शकाल.floatआणिdoubleapproximate 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 ला समजले पाहिजे.
उदाहरण:
int studentCount = 50;
boolean courseActive = true;
char grade = 'A';इथे:
50→ whole numbertrue→ 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:
Variable
↓
Data Type
↓
What kind of value is allowed?
↓
How can that value be used?Java मध्ये types चे दोन मोठे groups आहेत:
Java Types
│
├── Primitive Types
│ ├── byte
│ ├── short
│ ├── int
│ ├── long
│ ├── float
│ ├── double
│ ├── char
│ └── boolean
│
└── Reference Types
├── Classes
├── Interfaces
└── ArraysJava 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 आहेत.
Primitive Types
Numeric
├── Integral
│ ├── byte
│ ├── short
│ ├── int
│ ├── long
│ └── char
│
└── Floating Point
├── float
└── double
Non-numeric logical type
└── booleanJava 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#
| Type | Category | Value size / model | Typical purpose |
|---|---|---|---|
byte | Integer | 8-bit signed | binary data, tightly constrained values |
short | Integer | 16-bit signed | specific 16-bit data requirements |
int | Integer | 32-bit signed | normal whole numbers |
long | Integer | 64-bit signed | very large whole numbers |
float | Floating point | IEEE 754 binary32 | approximate decimals where float precision/storage is intentional |
double | Floating point | IEEE 754 binary64 | normal approximate decimal calculations |
char | Integral / character-related | 16-bit unsigned UTF-16 code unit | one UTF-16 code unit |
boolean | Logical | true / false | conditions 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:
Minimum: -128
Maximum: 127हा range Java specification ने निश्चित केलेला आहे.
Example#
public class ByteExample {
public static void main(String[] args) {
byte signalStrength = 95;
System.out.println(signalStrength);
}
}Expected output:
95byte कुठे useful असतो?#
Normal business application मध्ये एखादी value छोटी आहे म्हणून लगेच byte वापरणे आवश्यक नसते.
उदाहरण:
byte age = 35;Technically काही ages साठी हे चालेल.
पण practical application मध्ये:
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:
byte[] fileData = {10, 20, 30, 40};इथे एक important distinction आहे:
byte → primitive type
byte[] → reference typeArray बद्दल detailed chapter नंतर असू शकतो; इथे फक्त type distinction लक्षात ठेवा.
Decision Rule#
Value छोटी आहे म्हणूनbyteवापरू नका. Requirement genuinely 8-bit range/data मागत असेल तेव्हाbyteconsider करा.
2. short#
Definition: short is a 16-bit signed integer primitive type with values from -32,768 to 32,767.
Range:
Minimum: -32768
Maximum: 32767Example:
public class ShortExample {
public static void main(String[] args) {
short sensorReading = 25000;
System.out.println(sensorReading);
}
}Expected output:
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#
Student count maximum 500 आहे.
↓
500 byte मध्ये बसत नाही.
↓
म्हणून short वापरतो.हे technically possible आहे.
पण practical question आहे:
short वापरण्याची actual requirement आहे का?नसेल तर:
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:
Minimum: -2147483648
Maximum: 2147483647Beginner Java development मध्ये whole-number values साठी int हा सर्वात important type आहे.
Examples:
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:
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:
Minimum: -9223372036854775808
Maximum: 9223372036854775807Example:
public class LongExample {
public static void main(String[] args) {
long videoViews = 5000000000L;
System.out.println(videoViews);
}
}Expected output:
5000000000इथे L दिसत आहे.
आत्ता फक्त एवढे समजा:
compiler ला हा whole-number valuelongम्हणून 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 ठरेल.
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 नाही.
longहा अजूनही whole-number integer type आहे.
Integer Types Comparison#
| Type | Bits | Minimum | Maximum |
|---|---|---|---|
byte | 8 | -128 | 127 |
short | 16 | -32,768 | 32,767 |
int | 32 | -2,147,483,648 | 2,147,483,647 |
long | 64 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
Practical Selection#
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 → short5. float#
आता whole numbers सोडून decimal values कडे येऊ.
उदाहरण:
36.5
4.7
12.75
98.6Java मध्ये decimal-like approximate numeric calculations साठी मुख्य primitive types:
float
doubleDefinition: 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:
public class FloatExample {
public static void main(String[] args) {
float temperature = 36.5f;
System.out.println(temperature);
}
}Expected output:
36.5इथे f का आहे याची literal-level detail पुढच्या chapter मध्ये येईल.
आत्ता एवढे लक्षात ठेवा:
float temperature = 36.5f;हा valid beginner form आहे.
float approximate का आहे?#
Computer binary system वापरतो.
आपण decimal मध्ये सहज लिहू शकणारी प्रत्येक value binary floating-point मध्ये exact represent होईलच असे नाही.
Conceptual view:
Decimal value
↓
Binary floating-point representation
↓
Some values exact
Some values approximatefloat 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:
public class DoubleExample {
public static void main(String[] args) {
double distanceKm = 125.75;
System.out.println(distanceKm);
}
}Expected output:
125.75double roughly 15–16 significant decimal digits precision देतो.
Normal approximate decimal calculations मध्ये double हा float पेक्षा अधिक common choice आहे.
float vs double#
| Point | float | double |
|---|---|---|
| Format | IEEE 754 binary32 | IEEE 754 binary64 |
| Size | 32-bit | 64-bit |
| Precision | Lower | Higher |
| Approx decimal precision | ~6–7 significant digits | ~15–16 significant digits |
| General decimal calculations | Less common | Usually preferred |
| Large memory-sensitive numeric arrays | Can be useful | Higher memory requirement |
| Exact currency | Not appropriate | Not appropriate |
Practical Decision#
Requirement:
User ने किती kilometers travel केले ते approximate measurement store करायचे आहे.
double distanceKm = 238.75;Reason:
- decimal measurement
- high precision useful
doublenatural choice
Floating-Point Precision — Very Important#
हा code पहा:
public class PrecisionExample {
public static void main(String[] args) {
double result = 0.1 + 0.2;
System.out.println(result);
}
}Expected output:
0.30000000000000004Beginner 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:
₹10.10 + ₹20.20Exact 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:
0 to 65535किंवा:
'\u0000' to '\uffff'Example:
public class CharExample {
public static void main(String[] args) {
char grade = 'A';
System.out.println(grade);
}
}Expected output:
AMarathi character:
public class MarathiCharExample {
public static void main(String[] args) {
char letter = 'म';
System.out.println(letter);
}
}Expected output:
मchar हा numeric family मध्ये का आहे?#
Java specification मध्ये char हा integral type आहे.
तो signed नाही.
Range:
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:
Common BMP character
↓
one UTF-16 code unit
↓
one char
Some supplementary Unicode character
↓
two UTF-16 code units
↓
two char valuesBeginner म्हणून आत्ता deep Unicode processing करण्याची गरज नाही.
फक्त misconception टाळा:
char म्हणजे प्रत्येक possible visible Unicode symbol साठी guaranteed one-value container नाही.char vs String#
char grade = 'A';
String courseName = "Java";Difference:
char
↓
primitive type
↓
one UTF-16 code unit
String
↓
reference type
↓
sequence of characters/textString ला आपण reference type introduction मध्ये लगेच पाहणार आहोत.
Single quotes आणि double quotes चे complete literal rules पुढच्या Literals and Constants chapter मध्ये येतील.
8. boolean#
Application मध्ये खूप values numeric नसतात.
उदाहरण:
Is user logged in?
Is payment successful?
Is email verified?
Is course active?या प्रश्नांची natural answers:
Yes / No
True / FalseJava मध्ये यासाठी boolean.
Definition: boolean is a primitive type that has exactly two possible values: true and false.
Example:
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:
true
falseWrong Mental Model#
काही languages किंवा low-level systems मुळे beginners असे assume करतात:
1 = true
0 = falseJava मध्ये:
boolean active = true;valid.
पण:
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 करते:
true
falseJVM/internal implementation representation वेगळा विषय आहे.
म्हणून beginner interview साठी strong answer:
Java defines the values ofbooleanastrueandfalse; the Java Language Specification does not define a fixed memory size for a boolean variable.
Complete Primitive Type Comparison#
| Type | Category | Language-level representation | Important use |
|---|---|---|---|
byte | Signed integer | 8-bit | raw/binary or constrained 8-bit data |
short | Signed integer | 16-bit | specific 16-bit requirements |
int | Signed integer | 32-bit | normal whole numbers |
long | Signed integer | 64-bit | very large whole numbers |
float | Floating point | 32-bit IEEE 754 binary32 | approximate lower-precision decimals |
double | Floating point | 64-bit IEEE 754 binary64 | approximate higher-precision decimals |
char | Unsigned integral | 16-bit UTF-16 code unit | character code unit |
boolean | Logical | true / false; fixed storage size not specified by JLS | states/conditions |
Reference Data Type Introduction#
आत्तापर्यंत आपण values पाहिल्या:
int quantity = 10;
double rating = 4.8;
boolean active = true;हे primitive types आहेत.
पण application मध्ये आपण complex things सुद्धा represent करतो:
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#
String studentName = "Sneha";String primitive नाही.
तो class-based reference type आहे.
Simplified mental model:
studentName
│
│ reference
▼
String object representing "Sneha"Language-level perspective:
Primitive variable
int age = 25;
age
↓
25
Reference variable
String name = "Sneha";
name
↓
reference
↓
String objectImportant 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#
| Feature | Primitive Type | Reference Type |
|---|---|---|
| Defined examples | int, double, char, boolean | String, arrays, classes |
| Value | primitive value | reference to object/array |
| Built-in primitive keywords | Yes | Not generally primitive keywords |
Can represent null | No | Yes |
| Example | int age = 25; | String name = "Asha"; |
| Methods directly belonging to value | Primitive itself is not an object | Objects can expose methods |
| Range | Defined for numeric primitives | Depends 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 करायचे आहेत.
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:
Asha
42
250000
87.5
A
trueData-type reasoning:
| Requirement | Type | Reason |
|---|---|---|
| Student name | String | text, so reference type |
| Completed lessons | int | normal whole-number count |
| Total watch seconds | long | potentially very large lifetime counter |
| Average score | double | decimal measurement |
| Grade | char | one code unit such as A |
| Email verified | boolean | true/false state |
हीच professional thinking हवी:
Value पाहून type निवडू नका; requirement + meaning + range + precision पाहून type निवडा.
Data Type Selection Framework#
Requirement वाचल्यावर हे questions विचारा.
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 type2. Practical / Real-World Application#
Scenario 1 — Course Platform#
Requirement:
Course ID
Course title
Number of chapters
Course rating
Published or notPossible design:
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 आहे:
Quantity → numeric type
Identifier → often String or domain-specific typeScenario 2 — Massive Video Platform Counter#
Requirement:
एका popular video चा view count 3,800,000,000 आहे.
int maximum:
2147483647तो पुरेसा नाही.
Better:
long videoViews = 3800000000L;Scenario 3 — Raw Image Data#
Image bytes process करायचे आहेत.
byte[] imageData = {10, 20, 30};इथे:
imageData
↓
reference variable
byte[]
↓
array reference type
each element
↓
byte primitiveहा primitive + reference relationship चा excellent example आहे.
Scenario 4 — Temperature Measurements#
Single measurement:
double temperature = 36.75;Millions of scientific/graphics values आणि application explicitly 32-bit float precision/storage design वापरत असेल:
float measurement = 36.75f;Important:
float फक्त "value लहान आहे" म्हणून निवडत नाही.Precision आणि data representation requirement महत्त्वाची आहे.
Scenario 5 — Payment Status#
Bad design:
int paymentStatus = 1;जर requirement फक्त:
paid / not paidअसेल, तर:
boolean paymentCompleted = true;अधिक expressive आहे.
Code स्वतः meaning सांगतो.
Scenario 6 — Money#
Weak design:
double accountBalance = 100.10;जर exact accounting calculation करायची असेल तर binary floating-point precision problem होऊ शकतो.
Professional direction:
Exact financial decimal arithmetic
↓
BigDecimalBigDecimal reference type आहे.
Details पुढच्या/advanced chapters मध्ये.
Requirement → Type Thinking#
| Requirement | Recommended direction | Why |
|---|---|---|
| Number of attempts | int | normal whole number |
| Global event count > 2.1B | long | larger integer range |
| Raw network bytes | byte / byte[] | binary data |
| 16-bit protocol field | short | protocol explicitly requires signed 16-bit |
| Average response time | double | decimal measurement |
| Millions of float-format coordinates | float | explicit float representation requirement |
Grade A | char | single UTF-16 code unit |
| Is account enabled? | boolean | two-state value |
| Full name | String | text/reference type |
3. Common Mistakes & Misconceptions#
Mistake 1 — "Small number म्हणजे byte"#
Why it sounds believable#
byte छोटा आहे आणि 25 सहज range मध्ये बसतो.
Weak thinking#
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 साठी:
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/shortuse करा जेव्हा data requirement justify करते; imaginary micro-optimization साठी नाही.
Mistake 3 — float आणि double exact decimal types आहेत#
Example:
double value = 0.1 + 0.2;
System.out.println(value);Output:
0.30000000000000004Root Cause#
Binary floating-point representation.
Prevention#
Exact financial decimal arithmetic साठी floating-point primitives blindly वापरू नका.
Mistake 4 — float declaration#
Incorrect:
float temperature = 36.5;हे compile होत नाही कारण 36.5 बद्दल literal typing rules लागू होतात.
Current chapter मध्ये working form:
float temperature = 36.5f;f suffix चे full rules पुढच्या chapter मध्ये.
Mistake 5 — Large long value लिहिताना literal issue#
Intent:
long population = 5000000000;हा source form compile होत नाही कारण integer literal rules लागू होतात.
Working form:
long population = 5000000000L;L बद्दल full discussion पुढच्या chapter मध्ये.
Mistake 6 — boolean = 0 or 1#
Incorrect:
boolean active = 1;Correct:
boolean active = true;Java boolean numeric primitive नाही.
Mistake 7 — boolean is always 1 bit#
Java language specification fixed boolean storage size define करत नाही.
Correct answer:
boolean has two language-level values:
true
falseMistake 8 — char म्हणजे कोणताही Unicode symbol exactly#
Correct:
char = one UTF-16 code unitकाही Unicode characters साठी दोन code units लागतात.
Mistake 9 — String primitive आहे#
Incorrect classification:
String → primitiveCorrect:
String → reference typeJava primitive list fixed आहे:
byte
short
int
long
float
double
char
booleanString त्या list मध्ये नाही.
Mistake 10 — Reference variable म्हणजे object स्वतःच#
Simplified correct model:
String name
↓
reference value
↓
String objectReference variable आणि referenced object हे conceptually वेगळे आहेत.
4. Hands-On Practice#
Practice 1 — Choose the Correct Type#
प्रत्येक requirement साठी योग्य type निवडा.
A#
एका classroom मध्ये students ची संख्या store करायची आहे.
Maximum expected:
500Options:
byte
short
int
longAttempt first.
Hint 1: 500 byte मध्ये बसत नाही.
Hint 2: short मध्ये बसतो, पण special 16-bit requirement नाही.
Solution:
int studentCount = 500;Why?#
Normal application whole-number count असल्यामुळे int practical choice आहे.
B#
System lifetime event count:
6,000,000,000Solution:
long eventCount = 6000000000L;Reason:
int range insufficient.
C#
User email verified आहे का?
Solution:
boolean emailVerified = true;D#
Student grade:
Aजर requirement exactly one UTF-16 code unit असेल:
char grade = 'A';E#
Student full name:
Asha PatilSolution:
String studentName = "Asha Patil";String reference type आहे.
Practice 2 — Find the Problem#
Code#
byte score = 200;Problem काय?
Hint: byte maximum check करा.
Solution:
byte maximum 127 आहे.
If score 200 valid असू शकतो:
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:
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:
public class DecimalPractice {
public static void main(String[] args) {
double result = 0.1 + 0.2;
System.out.println(result);
}
}Expected output:
0.30000000000000004Question:
हा Java bug आहे का?
Answer:
No.
हा binary floating-point representation चा expected consequence आहे.
Practice 5 — Primitive or Reference?#
Classify:
int
String
boolean
byte[]
double
char
String[]Solution:
| Type | Classification |
|---|---|
int | Primitive |
String | Reference |
boolean | Primitive |
byte[] | Reference |
double | Primitive |
char | Primitive |
String[] | Reference |
Practice 6 — Explain Your Choice#
Requirement:
A warehouse stores quantity from 0 to 100,000.
Which is better?
short
int
longAnswer:
intWhy?
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:
byte
short
int
long
float
double
char
booleanQ3. 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:
true
falseJava 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#
Java Types
├── Primitive
└── ReferenceEight Primitive Types#
byte
short
int
long
float
double
char
booleanInteger Types#
byte → 8-bit
short → 16-bit
int → 32-bit
long → 64-bitNormal Whole Number#
int count = 100;Very Large Whole Number#
long views = 5000000000L;Decimal#
double temperature = 36.75;Explicit Float#
float temperature = 36.75f;Character Code Unit#
char grade = 'A';Logical State#
boolean active = true;Text#
String name = "Asha";String → reference type.
Key Rules#
byterange:-128to127shortrange:-32768to32767intrange:-2147483648to2147483647long→ larger 64-bit signed integerfloat→ IEEE 754 binary32double→ IEEE 754 binary64- floating-point values can be approximate
char→ 16-bit UTF-16 code unitboolean→ onlytrue/false- JLS does not define fixed boolean storage size
Stringand 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,longcompare करणे. - normal whole-number values साठी
intका practical आहे ते explain करणे. intrange पेक्षा मोठ्या values साठीlongनिवडणे.floatआणिdoubleमधला precision difference explain करणे.- floating-point decimal approximation explain करणे.
- exact money calculation साठी
doubleblindly का वापरू नये हे सांगणे. chartechnically UTF-16 code unit आहे हे explain करणे.booleanला0किंवा1assign करता येत नाही हे सांगणे.- boolean fixed storage size बद्दलचा misconception correct करणे.
Stringprimitive नाही हे explain करणे.- primitive आणि reference type classify करणे.
byte[]मध्ये array reference type आणिbyteelement primitive type यातील distinction explain करणे.- business requirement वरून योग्य data type reason करून select करणे.
8. Final Challenge#
Requirement#
तुम्ही online learning platform चा Learner Progress Summary design करत आहात.
खालील information store करायची आहे:
- Learner full name
- Learner age
- Completed lessons
- Lifetime watched seconds — future मध्ये 3 billion पेक्षा जास्त होऊ शकतात
- Average assessment percentage
- Current grade such as
A - Email verified or not
- Raw profile image bytes
Your Task#
प्रत्येक field साठी:
- Java type निवडा.
- तो primitive आहे की reference ते सांगा.
- choice चे reason सांगा.
Attempt केल्याशिवाय solution पाहू नका.
Final Challenge Solution#
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#
| Field | Type | Primitive / Reference | Reason |
|---|---|---|---|
| Learner name | String | Reference | text |
| Age | int | Primitive | normal whole number |
| Completed lessons | int | Primitive | normal counter |
| Watch seconds | long | Primitive | may exceed int range |
| Percentage | double | Primitive | approximate decimal |
| Grade | char | Primitive | one UTF-16 code unit |
| Email verified | boolean | Primitive | two-state value |
| Image data | byte[] | Reference | array 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 आहे.