BlueJ Programs

Data Types

byte

The byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127 (inclusive). We can save memory in large arrays using byte. We can also use byte instead of int  to increase the limit of the code.



short

The short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to 32,767. short is used to save memory in large arrays.




int

It is a 32-bit signed two's complement integer data type. It ranges from -2,147,483,648 to 2,147,483,647. This data type is used for integer values.



long

The long data type is a 64-bit signed two's complement integer. It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger range of values.



float

The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float (instead of double) to save memory in large arrays.


double

This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). This data type is generally the default choice for decimal values.


char

The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535. They are not same as ints, shorts etc.

Boolean
The boolean data type is 1-bit  and has only two values: true and false. We use this data type for conditional statements. true and false are not the same as True and False. They are defined constants of the language. 

The following table shows the default values for the data types: 

 Keyword  Description   Size/Format
 byte  Byte-length integer  8-bit two's complement
 short   Short integer  16-bit two's complement
 int  Integer   32-bit two's complement
 long   Long integer   64-bit two's complement
 float  Single-precision floating point  32-bit IEEE 
 double  Double-precision floating point  64-bit IEEE 
 char   A single character   16-bit Unicode character
 boolean   A boolean value (true or false)   true or false


When we declare a field it is not always essential that we initialize it too. The compiler sets a default value to the fields which are not initialized which might be zero or null. However this is not recommended. 

1 comment: