Primitive Types
MultiMiniJ has three primitive types: boolean, int, and string.
Predefined Methods
Primitive types support the following predefined methods.
boolean
- string toString(): returns a string representation of this boolean value.
{ boolean b = true; string s = b.toString(); // assigns "true" into s. }
int
- string toString(): returns a string representation of this integer value.
{ int i = 23; string s = i.toString(); // assigns "23" into s. }
string
- string concat(string s): returns a new string value which represents the concatenation of this string with s.
{ string s1 = "Hello"; string s2 = s1.concat(" world!"); // assigns "Hello world!" into s2. } - string get(int index): returns a string of one character containing representing the index'th character of this string. The first character of a string has index 0.
{ string s1 = "Hello world!"; string s2 = s1.get(6); // assigns "w" into s2. } - int length(): returns the length of this string.
{ string s1 = "Hello world!"; int len = s1.length(); // assigns 12 in len } - boolean isInt(): returns true if this string is a number.
{ boolean b1 = "-12".isInt(); // assigns 'true' in b1 boolean b2 = "12".isInt(); // assigns 'true' in b2 boolean b3 = "-1".isInt(); // assigns 'true' in b3 boolean b4 = "NotANumber".isInt(); // assigns 'false' in b4 } - int toInt(): converts this string to an integer. Aborts if something fails. Should be used along with isInt() to ensure that the result is valid and that the program executes normally.
{ int i1 = "-12".toInt(); // assigns -12 in i1 int i2 = "12".toInt(); // assigns 12 in i2 int i3 = "-1".toInt(); // assigns -1 in i3 int i4 = "NotANumber".toInt(); // software abort }