- Computers & Software»
- Computer Science & Programming»
- Computer Programming Tutorials
ECLIPSE IDE Tutorial - Experimenting with JAVA Expressions and Statements with Snapshot
JAVA Primitive Data Types
Before experimenting with expressions, it might be a good idea to see what the primitive (basic) data types exist in the JAVA language.
The first thing to note that JAVA is a static typed language, that is a variable must first be declared before it is used.
For example, an integer is one of the basic JAVA data types. Suppose we want to have a class field for the year a person is born. We first must have a statement,
defined before we can use it in a program. This statement is known as a declaration.
It is permissible to have the statement also perform an assignment, so
is a legal statement. Also, multiple variables can be combined on a single statement.
The primitive types in the JAVA language are:
- byte - is an 8-bit signed two's complement integer. It value range is -128 (minimum) and to 127 (maximum). The byte is an historic artifact in that at one time computers had a word length of 8-bits. Its use is rather infrequent today but can be used in applications which have a large number of occurrences with a limited range of values. Example assignment; variableByte = 0;
- word - is a 16-bit signed two's complement integer. It value range is -32,768 (minimum) to 32,768 (maximum). Example assignment: variableWord = 0;
- int - is a 32-bit signed two's complement integer. It value range is -231 (minimum) and to 231 (maximum).In Java SE 8 and later, int can be used to represent an unsigned 32-bit integer, its value range is 0 (minimum) and 232-1 (maximum). Example assignment: variableInt = 0;
- long - is a 64-bit two's complement integer. Its value range is -263 (minimum) to 263-1 (maximum). In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, with a value range of 0 (minimum) to 264-1 (maximum). Example assignment: variableLong = 0L;
- float: - is a single-precision 32-bit IEEE 754 floating point value. It should never be used where precise values are required. Example assignment: variableFloat = 0.0f;
- double - is a double-precision 64-bit IEEE 754 floating point value. For decimal values this is generally the default choice. Example assignment: variableDouble = 0.0d;
- boolean - has a value of true or false. This type is generally used for conditions which are either true or false. It's size is not predefined. Example assignment: variableBoolean = false;
- char - is a 16-bit Unicode character whose values range from 0 ( \u0000) through 65,535 (\uffff) inclusive. Example assignment: variableChar = 'A';
If a field is declared but not initialized for numeric field, 0 is generally stored. For character variables an uninitialized field is null and Boolean fields are false. Local variables are treated differently, in that the compiler never assigns a value to an uninitialized variable.
With that out of the way, let's look at how ECLIPSE can help you test code snippets by utilizing the scrapbook page feature.
Setting Up A Scrapbook Page
A scrapbook page is an ideal way of testing out an unfamiliar expression or statement. It's kind of like using a scratch pad to jot down an idea. To create a scrapbook page, you start at New on the tool bar then select other. At the other menu you select Java Run/Debug and click on scrapbookpage. You choose a parent folder (which is important if you will be working on testing expressions for this project on multiple occasions. You enter a name to identify the scrapbook page. Choosing a good name is important, it will help jog your memory as to what types of statements you were working with. The following screenshots should serve as guideposts to get there.
Steps to Open a Scrapbook Page
Entering and Evaluating Expressions and Statements
An important point to note is you must highlight the line or lines you wish to inspect or execute. If you don't ECLISPE will be unaware of what expression or statements you wish to inspect or run.
The following three snapshots illustrate inspecting or running three code snippets.
In the first you are merely evaluating a simple mathematical expression. Namely, multiplying 3700 by 5. When the chosen text is merely an expression. To see the result of the expression you can either Choose Run followed by Inspect or click on the magnifying glass icon.
The second screenshot is a a multi-line example. Test snippets can be as involved as you have a need for them to be. Again, to evaluate you must highlight all of the lines in the snippet, then either click on the magnifying glass or choose run followed by inspect.
The third example illustrates having a JAVA statement as the code snippet. In this case you Choose run followed by execute. We will revisit the scrapbook in future to illustrate examples which involve methods from multiple packages.