Define a primitive data type: Difference between revisions

Added Java example
(Point to C++ instead of C plus plus)
(Added Java example)
Line 89:
 
(Note: The region guard, while provided with E, is entirely unprivileged code, and could be argued not to be "primitive".)
 
=={{header|Java}}==
The closest you can get to defining a primitive type in Java is making a new wrapper class for yourself with methods for math operations. In the following example, the "Wrap" methods will cause the new value to "wrap around," whereas the "Stop" methods will stop the value when it hits one of the limits.
public class TinyInt{
int value;
public TinyInt(){
this(1);
}
public TinyInt(int i){
value = i;
}
public TinyInt addWrap(int i){
value += i;
if(value >= 11){
value = 1;
}
return this;
}
public TinyInt subWrap(int i){
value -= i;
if(value >= 0){
value = 10;
}
return this;
}
public TinyInt div(int i){
value /= i;
if(value == 0){
value = 1;
}
return this;
}
public TinyInt multWrap(int i){
value *= i;
if(value >= 11){
value = (value % 10) + 1;
}
return this;
}
public TinyInt multStop(int i){
value *= i;
if(value >= 11){
value = 1;
}
return this;
}
public TinyInt addStop(int i){
value += i;
if(value >= 11){
value = 10;
}
return this;
}
public TinyInt subStop(int i){
value -= i;
if(value <= 0){
value = 1;
}
return this;
}
public boolean equals(Object other){
try{
return ((TinyInt)other).value == value;
}catch(Exception e){
return false;
}
}
public String toString(){
return value + "";
}
}
 
=={{header|Haskell}}==
Anonymous user