
Play Store Application link – Java to TypeScript in 14 Steps – App on Google Play
Understanding Basic Data Types
In TypeScript, basic data types help define what kind of values your variables can hold. Think of these data types as categories for the data, similar to Java where you have int
, String
, boolean
, and so on. Here’s how TypeScript’s basic data types compare and how you use them:
Number
The number
data type in TypeScript is used for both integer and floating-point numbers. This is similar to Java’s int
, double
, or float
.
let count: number = 10; // An integer
let price: number = 9.99; // A floating-point number
In Java, it would be:
int count = 10; // An integer
double price = 9.99; // A floating-point number
String
The string
data type is used for text, just like String
in Java.
let message: string = "Hello, TypeScript!";
In Java, it would be:
String message = "Hello, Java!";
Boolean
The boolean
data type represents true/false values, similar to Java’s boolean
.
let isCompleted: boolean = false;
In Java, it would be:
boolean isCompleted = false;
Null
The null
data type represents the absence of any value. It’s akin to using null
in Java.
let result: null = null;
In Java, it would be:
Object result = null;
Undefined
The undefined
data type is used for variables that haven’t been assigned a value yet. It’s somewhat like an uninitialized variable in Java.
let score: undefined = undefined;
In Java, this concept is less explicit, as uninitialized variables default to specific values depending on their type, but you might see it as:
Integer score = null; // Not exactly the same but similar in concept
Any
The any
data type allows a variable to hold any type of value. It’s similar to using Object
in Java, where any type of data can be stored.
let value: any = "Hello";
value = 10; // Can be reassigned to any type
In Java:
Object value = "Hello";
value = 10; // Can be reassigned to any type
Conclusion
In TypeScript, the basic data types—number
, string
, boolean
, null
, undefined
, and any
—allow you to define and manage different kinds of data in your code. These types are very similar to those in Java, helping you leverage your existing knowledge while adding TypeScript’s features to catch errors early and improve code clarity.