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.