TopUsing objectdrawPre-Defined Data types in Java

Pre-Defined Data types in Java

The built-in data types in Java are more complex than those in Grace. For example, Grace had only 1 type to hold numbers, Number. Java has at least four. Worse luck, Java has two different kinds of data types: primitive types and object types, which behave differently. The primitive types are included in the language for efficiency. Most of the time the differences won't make much of a difference, but sometimes they are critical. The main difference is that you cannot make method requests of primitive objects. They have no methods associated with them. All primitive types are also immutable, but a lot of the object types are immutable as well, so that is not a clear distinguishing factor.

Numeric types

Let's begin with the numeric types. We will focus on only the two that are most common, int and double. The type int includes all integers, positive, negative, and 0. Their range extends from -231 to 231-1. (If you need to represent larger numbers, use the type long, which has the same operations, but holds more values.) Elements of type int are written as in Grace and use the same arithmetic operations: +, -, *, /, and % (for the "mod" or remainder operator). The only significant difference from Grace is the operation of the meaning of the divide operator. It gives you the same integer result as you would get by doing long division. Thus 17 / 3 gives an answer of 5. The remainder is what you get from %, so 17 % 3 is 2. Integer arithmetic operations only return integer values!

There are four other operations on integer variables n that are useful: n++, ++n, n-, and -n. The first two are used to increment the value of n by 1. I.e., if n has the value 4 before the operation, then it has the value 5 after executing the operation. The second two are used to decrease the value of n by 1. If that is all that is going on, then why have four operations rather than 2?

It turns out that these operations also return a value as well as update n. The operations ++n and -n return the value of n after the update has completed, while n++ and n- return the value of n before the update. Thus if n has value 4, then ++n updates n to 5 and returns 5. On the other hand, n++ updates the value of n to 5, but returns the value 4. Thus if n starts out with the value of 4, then (n++) + n updates n to 5, while returning the value 9, while ++n + n also updates n to 5, but returns the value 10.

To avoid this confusion we strongly recommend that you do not embed statements with ++ in the middle of arithmetic expressions. Use them as stand alone expressions to update n or in for loops (see below) only.

If you want fractional values, then you should use the type double, which includes values with decimal points like 34.125 and -17.0. (The name double comes from the fact that it takes twice as much space as the other type that holds numbers with decimal point, float - which we won't be using.) Numbers of type double are essentially held in scientific notation internally, e.g. .34125 * 102. However, you write them, and they are generally printed out, in the standard way without powers of 10. The arithmetic operations work in the usual way for double, with one exception: there is no operator % - that is only available on int. The results of arithmetic operations on elements of type double are generally always elements of type double. Thus the result of evaluating 1.5/.5 is the double 3.0, not the int 3. When you write a constant that you want to be a double, you must include the decimal point. If you do include a decimal point, then you must have a digit on both sides. Thus write 3.0 not 3. and write 0.34 rather then .34.

Both int and double values can be compared with operators ==, !=, <, <=, >, and >=.

As a convenience, Java will automatically convert ints to doubles if you are performing "mixed mode" arithmetic, i.e., numeric operations that include both ints and doubles. Thus to perform 3+2.5, Java will first convert 3 to 3.0 and then do the addition. However, you must be careful, as it will only make the conversion when it needs to. Thus if you evaluate, 75.5*(1/2), the result will be the double 0.0. This is because the system first calculates 1/2, which is 0 since it is doing integer division. It then multiplies the result by 75.5, giving the final result of 0.0. To force it to do the division as decimal or "floating point" division, you could write it as any one of 1.0/2, or 1/2.0, or 1.0/2.0.

Java will NOT convert automatically in the other direction, however. You may not assign a double value to an int variable or let you use a double in any other context that expects an int. If you want to convert a double to an int, then you must tell Java by writing (int) before the expression. Doing so will truncate the double, returning the integer part of the value. Thus (int)(2.7/.7) will return the int 3. If you wish to round to the nearest int instead, use the static round method from the Java Math library. Thus Math.round(2.7/.7) will return the int 4.

Boolean type

Java has boolean types like Grace, with values true and false. Because they are primitive values rather than objects, you can not make method requests of them, but they do have the usual pre-defined operations !, &&, and ||. The big difference from Grace is that the &&, and || operations are short-circuit: if their final value is determined by the value of the first argument, then they do not bother to evaluate the second argument. Thus, if bval1 evaluated to false, then the condition val2 will not be evaluated in bval1 && bval2.

Primitive data types in Java are written with initial letter lower case. Thus we use int, double, and boolean. There are corresponding object types Integer, Double, and Boolean. We will not be using them, however.

String type

The type String is built-in for Java and is an object type, so the type name starts with a capital letter and you can send messages to it. As in Grace, strings are immutable, so methods return new strings rather than modifying the receiver. It has many of the methods of String in Grace, as well as many more. A complete listing of the methods is available at Java API's. Strings may be specified by surrounding them with double quotes as in Grace. However there is not string interpolation to insert other values into strings. Instead the concatenation operator, + (note this is a single +, not Grace's ++), can be used to glue together strings. For example, if x is an int variable then the string "x = x" in Grace would be written in Java as "x = "+x.toString or just "x = "+x. As you can see in this example, toString (rather than asString is used to convert a value to a string. If you concatenate a string with a non-string then the non-string will be converted to a string before concatenating it with the string.

Strings can be compared with the methods equals and compareTo. The method equals returns true if the expressions represent the same sequence of characters. The result of s1.compareTo(s2) returns an int that is negative if s1 comes before s2 in lexicographic (dictionary) order, 0 if they represent the same string, and a positive number if s2 comes after s1 is lexicographic order.

You must be careful about comparing strings. The next section explains why you should never use == to compare two strings. However you must also be aware that all of the capital letters come before all of the lower case letters (thus "Z" comes before "a"). Therefore usually you convert strings either to all caps (using method toUpperCase()or all lower case (using toLowerCase before making the comparison.


TopUsing objectdrawPre-Defined Data types in Java