Some definitions…

Posted on December 5th, 2004 by marjorie.
Categories: very old stuff.

Named constant

Some data must not be changed. For example the pay rate might be the same for all part-time employees. The value in a conversion formula that converts inches into centimeters is fixed because 1 inch is always equal to 2.54 cm. When stored in memory, this type of data must be protected from accidental changes during program execution. In Java you can use a named constant to instruct a program to mark those memory locations in which data is fixed throughout program execution. A named constant is a memory location whose content is not allowed to change during the program execution and to declare it you use the following statement:

static final dataType IDENTIFIER = value;

static may or may not appear when a constant is declared

final double CONVERSION = 2.54;

final double PAY_RATE = 15.75;

What all of this mean? First it tells the compiler to allocate enough memory to store a value of type double and to call this memory space CONVERSION and store the value 2.54.

Variables

Obviously certain data  must be modified  during program execution and this type of data must be stored in memory cells whose contents can be modified during program execution, they are called variables. The syntax will be the following:

dataType identifier1, identifier2, …;

double amountDue;

int counter;

What all this means? First it tells the compiler to allocate enough memory to store a value of type double and call it amountDue. Variable means a variable memory location. And to assign a value here it is :

variable = expression;

Let's do it right this time…

Posted on December 4th, 2004 by marjorie.
Categories: very old stuff.

The basics of a Java Program:

There are 2 types of Java programs – Java applets and the plain Java applications. Java applets are programs are designed to run on a web browser and the Java programs are stand-alone application.

Primitive Data Types:

The primitive data types are fundamental data types in Java and there are 3 categories of primitive data types:

Integral which is a data type that deals with integers, or numbers without a decimal part. Then you have floating-Point, which is a data type that deals with decimal numbers. And then Boolean which is a data type that deals with logical values. Note that there is 2 floating-point data types; float and double. Float value are called single precision(-3.4E+38 to 3.4E+38) and values of type double are called double precision(-1.7E+308 to 1.7E+308)

Integral Data Type:

Why so many categories of integral data types? Only a limited amount of memory is available to execute programs and manipulate data. As a result programmers optimized the use of memory. Here are the values and memory allocation for integral Data Types:

DataType

Values

Storage

char

0 to 65535

2 byte

byte

-128 to 127

1 byte

short

-32768 to 32767

2 byte

int

2147483648 to 2147483647

4 byte

long

-9222337203..to 92337203…

8 byte

The most commonly used integral data types are int and char .

Expressions:

If all operand (that is numbers) in an expression  are integers (int) the expression is called integral expression if all operands in an expression are floating-point numbers(float) the expression is called a floating-point expression or a decimal expression.

Such integer will yield integer results meaning a floating point expression will output a floating point result…common sense…

Mixed Expressions:

If the operator has both types of operands (that is , one is an integer and the other is a floating-point number), during calculation the integer is changed to a floating-point number. Before evaluating this operator you must convert the integer operand to a floating-point number with a decimal part of zero.

Type Conversion - Casting

Since when evaluating arithmetic expression if the operator has mixed operand, the integer value is changed to a floating-point. In Java to avoid implicit type coercion a cast operator called the type conversion or type casting can be used  and it take the following form:

(dataType)expression

(int) (7.9)  => 7

You can also use cast operator to explicitly convert char data values into int data value or vice-versa:

in the Unicode character (int) (‘A’) is 65 and (char) (65) is ‘A’

The class String

What about data values as person’s name? A name contains more then one character. Strings in Java are enclosed in double quotation marks (not in single quotation marks as are the char data types). The String is process as a single unit and when determining the lengh of a String you must count the spaces.

For example the length of the following string is 22 “It is a beautiful day.”

Parsing Numeric String

Here is a numeric String “6723” To process this string as numbers for addition or multiplication we must first converts them into numeric form. A special method to convert numeric strings into their equivalent numeric form:

Integer.parseInt(“6723”) = 6723

Float.parseFloat(“34.56”) = 34.56

Double.parseDouble(“345.78”) = 345.78

You need to know how to parse and convert a String because Java only accepts strings and characters as input to a program.