Basic classes

 

Overview

Basic J classes, like all the others, specialize the "Object"root class.  Any J method created at "Object" class level will, therefore, be available for any J class.

 

Eight basic types are available:

·         int

·         float

·         boolean

·         String

·         enumerate

·         JRegexp

·         JStringMap

·         JObjectMap

 

 

The "int" class

The "int" class indicates integer values.  Specific operators are described below.

 

The ... method or operator

corresponds to ...

< 

"less than" integer comparison.

<=

less than or equal to integer comparison.

> 

greater than integer comparison.

>=

"greater than or equal to" integer comparison.

+

the sum of integers.

-

the subtraction of integers.

*

the multiplication of integers.

/

the division of integers.

%

the obtaining of the modulo.

float toFloat()

the conversion to float.

String toString()

the conversion to String.

 

Note 1:   If there is a processing error, a message is displayed and the J interpreter stops.

 

Note 2:   There is an implicit conversion of the integers into reals.

 

 

The "float" class

The "float" class gives real values.  Specific operators are described below.

 

The ... method or operator

corresponds to ...

+

the sum of floats.

-

the subtraction of floats.

*

the multiplication of floats.

/

the division of floats.

< 

"less than" real comparison.

<=

"less than or equal to" real comparison.

> 

"greater than" real comparison.

>=

"greater than or equal to" real comparison.

float sqrt()

the square root extraction.

float pow(Exponent : in float)

the raising to the "Exponent" power.

float log()

the obtaining of the Napierian logarithm.

float exp()

the obtaining of the exponential.

int toInteger()

the conversion to integer.

String toString()

the conversion to String.

 

 

float u = 1.0;

float e;

e = u.exp() ; // e == 2.71...

u = e.log() ; // u == 1.0

 

 

The "boolean" class

The "boolean" class gives boolean values.  These are used by classic operators (and, or and not).  There are two possible values:

·         true

·         false

 

Specific operators are detailed below.

 

The  ... method or operator

corresponds to ...

String toString()

the conversion to String.

||

a logical "or" between boolean expressions.  If the left-hand term of a "||" is true, the right-hand term is not evaluated.

&&

a logical "and" between boolean expressions.  If the left-hand term of a "&&" is false, the right-hand term is not evaluated.

!

a logical "negation" of a boolean expression.

 

boolean b = true ;

String s;

s = b.toString(); // s = "true"

 

 

The "String" class

The "String" class gives character Strings.  Character strings have an unlimited size.  Their specific operators are described below.

 

The ... method or operator

corresponds to ...

< 

"less than" String comparison.

<=

"less than or equal to" String comparison.

> 

"greater than" String comparison.

>=

"greater than or equal to" String comparison.

+

the conversion to String and the concatenation of the parameters.

int size()

the obtaining of the String size.

space (in int SpacesNumber)

the completion of the String by an indicated number of spaces.

substitute (in String ToSubstitute, in String NewValue)

the replacement of all the occurrences of "ToSubstitute", by "NewValue" in the String.

toUpper()

the conversion to Upper Case.

toLower()

the conversion to Lower Case.

int findFirst (in String Pattern, in int StartIndex)

the position of the first occurrence of "Pattern" from "StartIndex" or -1 if  "Pattern" is not present.

int findLast(in String Pattern)

the position of the last occurrence of "Pattern" or -1 if "Pattern" is not present.

String[] segment (in String Separator)

all the segments between separators. Each occurrence of "Separator" precisely defines 2 segments that can be empty.

String[] findToken(in String SeparatorsSet)

all the lexemes separated by one or more occurrences of one of the characters of "SeparatorsSet".

eraseBefore(in int StartIndex)

the deletion of the characters placed before "StartIndex".

eraseAfter(in int StartIndex)

the deletion of the characters placed after "StartIndex".

erase(in int StartIndex, in int StopIndex)

the deletion of the characters placed between "StartIndex" and  "StopIndex ", including the indexes.

assign(in String Origin, in int StartIndex, in int StopIndex)

the assignment with the "Origin" part placed between "StartIndex" and  "StopIndex ", including the indexes.

boolean strnequal (in String ToCompare, in int Length)

the comparison with "ToCompare" on a "Length" length.

strcat (...)

the concatenation of the list of strings indicated as parameters of the current string.

concat (...)

the conversion into a string and the concatenation of the list of strings indicated as parameters of the current string.

strncpy (in String Origin, in int Length)

the assignment with the first "Length" characters of  "Origin".

strncat(in String ToAdd, in int Length)

the concatenation of the first characters of  "ToAdd" with the "Length".

strip (in String BorderSet)

the deletion of all the occurrences of the characters of "BorderSet" at the beginning and at the end.

prepend(in String ToAdd)

the adding of "ToAdd" at the beginning.

insertStr(in StringToAdd, in int Position)

the insertion of "ToAdd" in the "Position" position.

overwrite(in String ToWrite, in int Position)

the overwriting of "ToWrite" from "Position".

float toFloat()

the conversion to float.

int toInt()

the conversion to integer.

boolean toBoolean()

the conversion to Boolean.

String toString()

the conversion to String (identity).

 

 

Note on + operator

If one of the + operators is not a String, it must be converted.  This conversion is predefined for basic classes.  For other classes, the "String toString ()" message is sent and its return is used as operator.

 

 

Example of a String

String s1 = "/usr/bin:";

String s2 = ":/bin";

String s3 = ":/usr/ucb:";

String[] ss ;

s1 = s1+s2+s3; // s1 = "/usr/bin::/bin:/usr/ucb:"

ss = s1.segment(":");

// ss == {"/usr/bin", "", "/bin", "/usr/ucb", ""}

ss = s1.findToken(":");

// ss == {"/usr/bin", "/bin", "/usr/ucb"}

 

 

Escape character in a constant String

The "~" character is used to avoid interpreting the character that follows.  Generally, the "~" symbol is used to insert the "" character in a character String.

 

String s1 = "the symbol ~"~~~" is used to insert the character ~"~"~"";

 

 

The enumeration type

In J, new enumeration elements may not be created.  However, several enumerations are predefined in the Objecteering metamodel. For example, the "Visibility" enumerate has the (Public, Private, Protected) values.

 

The ... method

corresponds to ...

int toInt()

the conversion of an enumeration into "int", according to the literal order, starting with "according to".

String toString()

converts an enumeration into String.

 

 

The "JRegexp" class

The ... method

corresponds to ...

void setPattern(in String Pattern)

defines the "Pattern" to be found. A pattern is a regular expression.

int match(in String ToMatch)

Returns true if the chain "ToMatch" corresponds to the pattern.  Returns 1 if true and 0 if false.

int getMatchCount()

Returns the number of sub-expressions conform to the pattern.

string getMatch(in int Position)

Returns the sub-expression conform to the "Position" of the pattern.

void reset()

Reinitializes the pattern.

The methods "match", "getMatchCount" and "getMatch" don't work until this new pattern has been defined.

 

 

Example of the "JRegexp:match" method

int ret;

JRegexp regexp = JRegexp.new();



regexp.setPattern("([0-9]+)(?:-|\.)([0-9]+)(?:-

|\.)([0-9]+)");

StdOut.write ("([:digit]+)\.([0-9]+)\.([0-9]+)",

NL);

ret = regexp.match("1123-2345.34679");

StdOut.write (ret, NL);

 


ret = regexp.getMatchCount();

StdOut.write (ret, NL);



StdOut.write ( regexp.getMatch(0), NL);

StdOut.write ( regexp.getMatch(1), NL);

StdOut.write ( regexp.getMatch(2), NL);

StdOut.write ( regexp.getMatch(3), NL);

 

 

The "JStringMap" class

The ... method

corresponds to ...

void put(in String Key, in String Value)

Inserts a string "Value" associated to the key "Key".

void putAll(in JStringMap Map)

Inserts all the key-value associations contained in "Map".

String get(in String Key)

Returns the value which has the key "Key".

void values(out String[] Values)

Gives all the values contained in the JStringMap in the collection "Values".

void remove(in String Key)

Removes the value associated with the key "Key".

void clear()

Reinitializes the JStringMap.

 

Note:      The keys and values can be equal to null.

 

 

The "JObjectMap" class

The ... method

corresponds to ...

void put(in String Key, in Object Value)

Insert an object "Value" associated to the key "Key".

void putAll(in JObjectMap Map)

Inserts all the associations key-value contained in "Map".

Object get(in String Key)

Returns the value of which the key is "Key".

void values(out Object[] Values)

Gives all the values contained in the  JObjectMap in the collection "Values".

void remove(in String Key)

Removes the value associated with the key "Key".

void clear()

Reinitializes the JObjectMap.

 

Note:      The keys and the values can be equal to null.

 

Values are handled in J purely by literal names.