STUDYECRAT Java : Data Types
60s
Java Challenge
Prove your skills in this interactive quiz
Live Code
Run snippets directly
Timed
60s per question
Scored
Earn 3D badges
★ Java Data Types: Key Interview Points
1. Primitive vs Wrapper Classes
- Memory: Primitives use stack memory (efficient), wrappers use heap
- Default Values: Primitives have defaults (0, false), wrappers are
null
- Autoboxing: Automatic conversion between primitives ↔ wrappers (Java 5+)
2. Integer Caching (-128 to 127)
Integer a = 100;
Integer b = 100;
System.out.println(a == b); // true (cached)
Integer x = 200;
Integer y = 200;
System.out.println(x == y); // false (new objects)
Integer b = 100;
System.out.println(a == b); // true (cached)
Integer x = 200;
Integer y = 200;
System.out.println(x == y); // false (new objects)
This optimization exists for Byte
, Short
, Integer
, and Long
.
3. Floating-Point Precision
0.1 + 0.2 ≠ 0.3
due to binary floating-point representation- Use
BigDecimal
for financial calculations double
(64-bit) vsfloat
(32-bit) - default isdouble
4. Type Conversion Rules
Widening (Automatic)
byte → short → int → long → float → double
Narrowing (Manual)
Requires explicit cast:
double d = 10.5;
int i = (int)d; // i=10
5. String Immutability
String s1 = "Hello"; // String pool
String s2 = new String("Hello"); // Heap memory
s1.concat(" World"); // Returns new string
System.out.println(s1); // "Hello" (original unchanged)
String s2 = new String("Hello"); // Heap memory
s1.concat(" World"); // Returns new string
System.out.println(s1); // "Hello" (original unchanged)
All modification operations return new String
objects.
🔍 SEO Tip for Java Interviews
These concepts frequently appear in Google, Amazon, and Microsoft interviews. Bookmark this page and practice the interactive quiz above to reinforce your understanding. Search engines prioritize content with interactive elements like quizzes!