Primitive Data Types

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.

var num = 2023; // num has an integer value of 2023

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.

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

// Using parseInt to parse the string to an Integer
var parsedInt = Integer.parseInt(text);

// Now 'parsedInt' contains the Integer value 42
  • static sum(num1: Integer, num2: Integer): Integer

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

// Sample Integer values
var num1 = Integer.valueOf(5);
var num2 = Integer.valueOf(7);

// Using sum to calculate the sum of num1 and num2
var result = Integer.sum(num1, num2);

// Now 'result' contains the Integer value 12 (5 + 7)

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.

// Double data type usage
var pi = 3.14159265359;
var radius = 5.0;
var area = pi * (radius * radius);

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.

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

// Using parseDoubleto parse the string to a Double
var parseDouble= Double.parseDouble(text);

// Now 'parseDouble' contains the Double value 4.2

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.

var bigNumber = 12345678912; 
// bigNumber now has a long value of 12345678912

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.

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

// Using parseLongto parse the string to a Long
var parseLong = Long.parseLong(text);

// Now 'parseLong' contains the Long value 123456789

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.

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

// Using parseFloatto parse the string to a Float
var parseFloat = Float.parseFloat(text);

// Now 'parseFloat' contains the Float value 2.159

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.

var company = "NAZDAQ";
// Now 'Company' contains the string value "NAZDAQ"
  • toString(): String

This method returns a string representation of the String object.

var text = "Hello, World!";
string result = text.toString();
// 'result' now contains the string "Hello, World!"
  • isEmpty(): Boolean

Returns true if the string is empty, otherwise false.

string emptyString = "";
string nonEmptyString = "B2DATA";

bool isEmpty1 = emptyString.isEmpty(); // Returns true
bool isEmpty2 = nonEmptyString.isEmpty(); // Returns false
  • charAt(pos: Integer): String

Returns the character at the specified index.

var text = "Hello";
varcharacter = text.charAt(2);
// 'character' now contains the string "l"
  • 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.

// Email to be validated
var email = "example@email.com";

// Define a regular expression pattern for basic email validation
var emailPattern = "^[A-Za-z0-9+_.-]+@(.+)$";

// Use the matches() method to check if the email matches the pattern
var isValid = email.matches(emailPattern); 

return isValid; // true (if email is valid according to the 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.

// File name to be validated
var fileName = "example_file.txt";

// Define a regular expression pattern for basic file name validation
var filePattern = "^[a-zA-Z0-9_\\-\\.]+$";

// Use the matches() method to check if the file name matches the pattern
var isValid = fileName.matches(filePattern);

return isValid; // true (if fileName is valid according to the pattern)
  • replace(searchValue: String, replaceValue: String): String

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

// Original string
var originalString = "NAZDAQ just introducted its latest product: B2Output";

// Use the replace method to replace "B2Output" with "B2Data"
var newString = originalString.replace("B2Output", "B2Data");

return newString;
// 'newString' contains: "NAZDAQ just introducted its latest product: B2Data."
  • 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:

var customerData = "Customer ID: 12345, Email: john.doe@email.com, 
                                Phone: 555-555-5555";

// Using replaceAll to replace customer IDs and email addresses with "X"
var sanitizedData = customerData.replaceAll("\\d+", "X").replaceAll(
                        "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}", "X");

return "Sanitized Data: " + sanitizedData;
// Sanitized Data: Customer ID: X, Email: X, Phone: X-X-X
  • contains(str: String): Boolean

Checks if the string contains a specific substring.

// Create a string
var text = "Hello, World!";

// Check if it contains "World"
var containsWorld = text.contains("World"); // Returns true
var containsUniverse = text.contains("Universe"); // Returns false
  • 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:

// CSV data containing sales records separated by newlines
var csvData = "2023-01-05,ProductA,500.00\n2023-01-10,
                                ProductB,750.00\n2023-01-15,ProductC,300.00";

// Use the split method to split the CSV data into an array of sales records
var salesRecords = csvData.split('\n');

// Now, salesRecords is an array where each element represents a single sales record.

// For example, salesRecords[0] contains "2023-01-05,ProductA,500.00", 
// salesRecords[1] contains "2023-01-10,ProductB,750.00", and so on.

// The '\n' was used to split the CSV data, resulting in an array of sales records.

return salesRecords;
  • toLowerCase(): String

Converts all alphabetic characters to lowercase.

// Create a string
var text = "Hello, World!";

// Convert all alphabetic characters to lowercase
var lowerCaseText = text.toLowerCase();
// 'lowerCaseText' contains the string "hello, world!"
  • toUpperCase(): String

Converts all alphabetic characters to uppercase.

// Create a string
var text = "Hello, World!";

// Convert all alphabetic characters to uppercase
var upperCaseText = text.toUpperCase();
// 'upperCaseText' contains the string "HELLO, WORLD!"
  • trim(): String

Removes leading and trailing white space and line terminator characters.

// Create a string with leading and trailing whitespace
var fullname = "   Hello, World!   ";

// Remove leading and trailing whitespace and line terminator characters
var trimmedText = text.trim();
// 'trimmedText' contains the string "Hello, World!"

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