Primitives

Primitive Types: Boolean, Integer, Double, Float, Long, String

Boolean

The most basic datatype is the Boolean value. It simply return true or false.

var Condition = false; // Condition now has a boolean value of false

The Boolean Class includes the following method:

  • static parseBoolean(text: String): Boolean

// Sample input string
var text = "true";

// Using parseBoolean to convert the string to a Boolean
var result = Boolean.parseBoolean(text);

// Now 'result' will contain the Boolean value 'true'

This static method takes a string as input and returns a Boolean object representing the parsed boolean value.


Integer

Another essential primitive type is the integer. It allows it to hold values from approximately -2,147,483,648 to 2,147,483,647. It can represent numbers with up to 10 digits, both positive and negative.

The Integer Class includes the following methods:

  • static parseInt(text: String): Integer

This static method takes a string as input and returns an Integer object representing the parsed integer value.

  • static sum(num1: Integer, num2: Integer): Integer

This static method takes two Integer objects and returns their sum as an Integer.


Double

The double data type is a numeric type designed to store real numbers with decimal precision. It is a fundamental data type suitable for a wide range of calculations, making it a crucial component for working with decimal values in B2Data.

The Double Class includes the following methods:

  • static parseDouble(text: String): Double

This static method takes a string as input and returns a Double object representing the parsed double value.


Long

The long data type is a numeric type specifically designed to handle large integer values with extended range. It is invaluable when you need to work with integers beyond the capabilities of standard integer types.

The Long Class includes the following methods:

  • static parseLong(text: String): Long

This static method takes a string as input and returns a Long object representing the parsed long integer value.


Float

The float data type is a numeric type optimized for representing real numbers with decimal precision. While similar to the double data type, it typically consumes less memory and provides sufficient accuracy for many computations.

The Float Class includes the following methods:

  • static parseFloat(text: String): Float

This static method takes a string as input and returns a Float object representing the parsed float value.


String

In our programming language, the string data type serves as a fundamental way to handle sequences of characters. It is essential for tasks like representing text, processing user input, and managing textual data within your applications. Strings provide a versatile and indispensable tool for working with character-based information.

  • toString(): String

This method returns a string representation of the String object.

  • isEmpty(): Boolean

Returns true if the string is empty, otherwise false.

  • charAt(pos: Integer): String

Returns the character at the specified index.

  • matches(regexp: String): RegExpMatchArray | null

This method matches the string with a regular expression and returns a boolean value to indicate whether the string matches the pattern. Here are two examples showcasing the use of the matches method for validation:

Example I: Email validation

In this example, we use the matches method to validate an email address by checking if it matches a common email pattern.

Example II: File Name Validation

In this example, we use the matches method to validate a file name by checking if it matches a basic file name pattern.

  • replace(searchValue: String, replaceValue: String): String

This method replaces text in the string using a search string.

  • replaceAll(searchValue: String, replacer: (match: String) => String): String

This method replaces all substrings in a string that match a specified regular expression with a given replacement string.

Example: Sanitize Data

In this example, we use the replaceAll method to protect customer privacy in a business context. The original customer data contains sensitive information, including customer IDs and email addresses. We apply two consecutive method calls to achieve this:

  • contains(str: String): Boolean

Checks if the string contains a specific substring.

  • split(separator: String, limit: Integer): Array<String>

Splits the string into substrings using the specified separator.

Example: Suppose you have a CSV (Comma-Separated Values) string representing sales data, and you want to split it into an array to work with the individual sales records:

  • toLowerCase(): String

Converts all alphabetic characters to lowercase.

  • toUpperCase(): String

Converts all alphabetic characters to uppercase.

  • trim(): String

Removes leading and trailing white space and line terminator characters.

Learn about more advanced Data Types such as array and list:

Was this helpful?