Posts

Showing posts from December, 2025

Null Safety in Java and Kotlin

Null values are one of the most common causes of runtime crashes on the JVM. Java allows null freely, while Kotlin was designed to eliminate NullPointerException at compile time . This topic is extremely important for real-world development, JVM understanding, and interviews . Table of Contents The Null Problem Null Handling in Java Null Safety in Kotlin Safe Calls & Elvis Operator Not-null Assertion (!!) Java and Kotlin Comparison Interview Questions Conclusion 1. The Null Problem A null reference means a variable points to no object. Accessing methods or properties on a null reference causes NullPointerException . 2. Null Handling in Java In Java, any reference type can be null . The compiler does not enforce null checks. Java Example String name = null; System.out.println(name.length()); // NullPointerException To avoid crashes, developers must manually add null checks everywhere. if (name != null) { System.out....

Type Checking and Smart Casts in Java and Kotlin

In real-world applications, variables often refer to objects whose exact type is known only at runtime. Type checking allows programs to safely identify an object’s actual type before using it. Kotlin goes a step further by introducing smart casts , which automatically handle casting after a successful type check. This reduces boilerplate code and prevents common runtime errors. Table of Contents What Is Type Checking? Type Checking in Java Type Checking in Kotlin Smart Casts in Kotlin Java and Kotlin Comparison Interview Questions Conclusion 1. What Is Type Checking? Type checking is the process of verifying an object’s actual runtime type before performing operations specific to that type. This is commonly required when: A variable is declared as a parent type (e.g., Object or Any ) Objects come from external libraries or APIs Working with inheritance or polymorphism Without proper type checking, programs may crash with Clas...

Type Conversion and Casting in Java and Kotlin

Type conversion and casting define how one data type is converted into another. This topic is critical for interviews, JVM understanding, and bug-free coding , especially when working with primitives, wrappers, and mixed-type expressions. Table of Contents Type Conversion Basics Implicit Conversion Explicit Casting Kotlin Type Rules Java and Kotlin Interview Questions Conclusion 1. Type Conversion Basics Type conversion is the process of converting one data type into another. It usually happens when assigning values or performing operations. There are two main types: Implicit (Widening) – automatic Explicit (Narrowing) – manual casting 2. Implicit (Widening) Conversion Implicit conversion happens when a smaller data type is converted to a larger one. There is no data loss . Java Example int a = 10; long b = a; // int → long double c = b; // long → double Kotlin Example val a: Int = 10 val b: Long = a.toLong() val c:...

Wrapper Classes & Boxing in Java & Kotlin

Primitive data types are fast and memory-efficient, but modern JVM programming often requires objects. Wrapper classes, boxing, and unboxing bridge this gap. This topic is extremely important for interviews, JVM internals, and performance discussions . Table of Contents Wrapper Classes Boxing & Unboxing Kotlin Internal Handling Java & Kotlin Interview Questions Conclusion 1. Wrapper Classes Wrapper classes are object representations of primitive data types . They allow primitives to work with collections, generics, and frameworks. Primitive Wrapper (Java) Kotlin Type Description int Integer Int Whole numbers, most commonly boxed type double Double Double Decimal values with double precision boolean Boolean Boolean True/false values for conditions char Character Char Single Unicode character 📌 In Java, primitives and wrappers are separate types. 📌 In Kotlin, this distinction is hidden but still exists in...

Kotlin Special Data Types (Any, Unit, Nothing, Nullable Types) Explained

Table of Contents Introduction Any Unit Nothing Nullable Types (?) Java vs Kotlin Differences Interview Questions Conclusion Introduction Kotlin introduces several **special data types** that extend Java's basic types. These include Any , Unit , Nothing , and **nullable types**, which enhance type safety, null handling, and code clarity. 1. Any Any is the root type of all **non-nullable types** in Kotlin. It is similar to Java's Object , but **does not include null** by default. Kotlin Example val a: Any = "Hello" val b: Any = 123 val c: Any = true println(a) println(b) println(c) --- 2. Unit Unit represents **no meaningful return value**, similar to Java's void . A function that returns Unit does not need to explicitly return anything. Kotlin Example fun greet(name: String): Unit { println("Hello, $name") } greet("CodeCrush") --- 3. Nothing Nothing represents a value...

Array Data Types in Java and Kotlin with Examples and Differences

Table of Contents Introduction Array Overview Array in Java Array in Kotlin Java vs Kotlin Differences Interview Questions Conclusion Introduction An array is a collection used to store multiple values of the same type. Arrays are fixed-size structures in both Java and Kotlin, and they provide fast access to elements using an index. Array Overview Feature Java Kotlin Description Type Array of primitives or objects (int[], String[]) IntArray, Array<String> Stores multiple elements of the same type Size Fixed Fixed Size determined at creation and cannot be changed Access Index-based (0 to n-1) Index-based (0 to n-1) Elements accessed using indices Mutable Yes (can change element values) Yes Elements can be updated but array size is fixed 1. Array in Java Java arrays can hold either primitives or objects . Array size is fixed, but elements can be updated. Java Example – Integer Array Note: In Jav...

String Data Type in Java and Kotlin (Immutability, Null Safety, Examples)

Table of Contents Introduction String in Java String in Kotlin Java vs Kotlin Differences Interview Questions Conclusion Introduction The String data type is used to store a sequence of characters (text) . Strings are one of the most commonly used data types in both Java and Kotlin, especially for handling user input, messages, and data processing. String in Java In Java, String is a class , not a primitive type. Strings in Java are immutable , meaning once created, their value cannot be changed. Stored as objects in the heap Immutable by design Supports String Pool for memory optimization Java Example String name = "CodeCrush"; name = name + " Corner"; System.out.println(name); Although it looks like the string is modified, Java actually creates a new String object . String in Kotlin In Kotlin, String is also a class and is immutable . Kotlin adds powerful features such as string templates and null safety...

Char and Boolean Data Types in Java and Kotlin with Examples

Table of Contents Introduction Char Data Type Boolean Data Type Java vs Kotlin Differences Interview Questions Conclusion Introduction Char and Boolean are fundamental data types used for representing characters and logical values. Both Java and Kotlin support these types with small but important differences in syntax and safety. 1. Char Data Type The Char data type is used to store a single Unicode character such as letters, digits, or symbols. Memory Size: 16 bits Represents Unicode characters Written using single quotes ( 'A' ) Char Example Overview Feature Java Kotlin Keyword char Char Unicode Support Yes Yes Numeric Operations Allowed Not Allowed Kotlin Example val letter: Char = 'A' println(letter) Java Example char letter = 'A'; System.out.println(letter); 2. Boolean Data Type The Boolean data type is used to store true or false values. It is commonly used in condi...

Floating Point Data Types in Java and Kotlin (Float vs Double) Explained

Table of Contents Introduction Float Data Type Double Data Type Java vs Kotlin Differences Interview Questions Conclusion Introduction Floating-point data types are used to store numbers with decimal values . Java and Kotlin provide two floating-point types based on precision and memory size . Floating-Point Data Types Overview Type Java Kotlin Size Precision Float float Float 32 bits 6–7 decimal digits Double double Double 64 bits 15–16 decimal digits 1. Float Data Type The Float data type stores decimal numbers using single-precision . It consumes less memory but provides lower accuracy compared to Double. Memory Size: 32 bits Precision: 6–7 decimal digits Suffix Required: F Kotlin Example val pi: Float = 3.14F println(pi) Java Example float pi = 3.14f; System.out.println(pi); 2. Double Data Type The Double data type stores decimal numbers using double-precision . It is the default floating-po...

Integer Data Types in Java and Kotlin (Byte, Short, Int, Long) with Examples

Table of Contents Introduction Java vs Kotlin Differences Interview Questions Conclusion Introduction Integer data types are used to store whole numbers without decimal values . Both Java and Kotlin provide four integer data types based on memory size and value range . Integer Data Types Overview Type Java Kotlin Size Range Byte byte Byte 8 bits -128 to 127 Short short Short 16 bits -32,768 to 32,767 Int int Int 32 bits -2³¹ to 2³¹ - 1 Long long Long 64 bits -2⁶³ to 2⁶³ - 1 1. Byte Data Type The Byte data type is the smallest integer type. It is mainly used to save memory in large arrays or streams. Memory Size: 8 bits Minimum Value: -128 Maximum Value: 127 Kotlin Example val minByte: Byte = -128 val maxByte: Byte = 127 println(minByte) println(maxByte) Java Example byte minByte = -128; byte maxByte = 127; System.out.println(minByte); System.out.println(maxByte); 2. Short Data Type The...

Data Types - Java, Kotlin

Java & Kotlin Data Types Overview Table of Contents 1. Integer Data Types 2. Floating-Point Data Types 3. Character Data Type 4. Boolean Data Type 5. String Data Type 6. Array Data Type 7. Kotlin Special Data Types 1. Integer Data Types Learn more → Data Type Java Kotlin Size Min Value Max Value Description Byte byte Byte 8 bits -128 127 Stores very small whole numbers. Saves memory in large arrays. Short short Short 16 bits -32,768 32,767 Stores small integers larger than Byte but smaller than Int. Int int Int 32 bits -2 31 2 31 -1 Most commonly used for whole numbers. Long long Long 64 bits -2 63 2 63 -1 Used to store very large whole numbers. 2. Floating-Point Data Types Learn more → Data Type Java Kotlin Size Precision Description Float float Float 32 bits 6–7 digits Stores decimal numbers with single precision. Requires 'F' suffix in Kotlin. Double double Double 64 bits 15–...