Null Type (null) in SwaziLang
What Does "Nothing" Mean?
Think about this: What do you have in your pocket if it's empty?
You have... nothing. Not zero items counted. Not "no pocket." Just... nothing. Emptiness.
In programming, we sometimes need to represent this idea of "nothing" or "no value". In SwaziLang, we call this null, which means "empty" or "nothing".
The null Value
null is a data type that has exactly one value: null itself.
data x = null;
chapisha x; // Prints: nullThat's it. That's the whole thing.
When Do You Use null?
1. Uninitialized Variables
When you declare a variable without giving it a value, it becomes null:
data jina;
chapisha jina; // Prints: null (nothing assigned yet)
// Later, you give it a value
jina = "Hassan";
chapisha jina; // Prints: Hassan2. Representing "Nothing" Intentionally
Sometimes you want to say "there is no value here":
data simu_number = null; // Person doesn't have a phone number
data website = null; // Company doesn't have a website3. Placeholder Values
data input = null;
// Later, user provides input
// ... after getting input ...
input = soma("Ingiza kitu: ");Checking for null
You can compare values with null:
data x = null;
chapisha x sawa null; // kweli (it is nothing)
data y = "something";
chapisha y sawa null; // sikweli (it's not nothing)Real Example: Checking if a Value Exists
data email = null;
chapisha email sawa null; // kweli (no email provided)
// If you wanted to check if email is NOT nothing:
chapisha email sisawa null; // sikweliThe Difference Between Similar Concepts
Let's be clear about what null is NOT:
null vs 0 (Zero)
data empty = null;
data zero = 0;
chapisha empty sawa zero; // sikweli (they're different!)null= no value at all0= a numeric value (zero)
null vs "" (Empty String)
data nothing = null;
data blank = "";
chapisha nothing sawa blank; // sikweli (they're different!)null= no value at all""= text that's empty (still text though)
null vs sikweli (False)
data nothing = null;
data false_val = sikweli;
chapisha nothing sawa false_val; // sikweli (they're different!)null= no valuesikweli= a boolean value meaning false
Important Notes
1. null Has No Methods
Unlike numbers and strings, null doesn't have any methods or properties you can use. It's just nothing:
data x = null;
// x.herufi won't work
// x.herufiKubwa() won't work
// Can't do anything with null except check if it equals null2. You Can Reassign null
data value = null;
chapisha value; // null
value = 42;
chapisha value; // 42
value = null; // Back to nothing
chapisha value; // null3. Converting null to Boolean
If you convert null to a boolean, it becomes sikweli (false):
chapisha Bool(null); // sikweliRemember from the Boolean chapter? Empty/nothing values are "falsy".
Simple Examples
Example 1: Checking Missing Information
data jina = "Hassan";
data umri = null;
data jiji = "Dar es Salaam";
chapisha jina sawa null; // sikweli (has name)
chapisha umri sawa null; // kweli (age is missing!)
chapisha jiji sawa null; // sikweli (has city)Example 2: Optional Data
data username = "hassan123";
data bio = null; // User didn't write a bio
kama bio sawa null {
chapisha "Hakuna biography";
} vinginevyo {
chapisha `Biography: ${bio}`;
}Example 3: Placeholder
data result = null;
// Do some work...
// If successful:
result = "Success!";
// If we never changed it:
chapisha result sawa null; // Check if work was doneThat's It!
The null type is really simple - it just represents "nothing". There's not much more to it!
Key Takeaways
nullmeans "nothing" - It's a value representing absence of dataIt's different from zero or false - They're actual values;
nullis no valueNo methods - You can't call methods on
nulllike you can with numbers/stringsUsed for uninitialized or missing data - When something hasn't been set or doesn't exist
Check with
sawa null- Compare values to see if they're nothing
What's Next?
You now understand all the primitive data types in SwaziLang:
- Numbers (Namba)
- Strings (Neno)
- Booleans (Bool)
- Null (null)
Next, you'll move into Operations - learning how to work with and combine these data types in powerful ways!
Remember: null represents nothing - it's the absence of a value, not a value itself.