The Basics of a Java Program
A programming language is a set of rules, symbols, and special words. The syntax rules of a language determine which instructions are valid. The semantic rules determine the meaning of the instructions. Together these rules enable you to write programs to solve problems.
The smallest individual unit of a program written in any programming language is called a token. Java’s tokens are divided into special symbols, word symbols, and identifiers.
Identifiers
Identifiers are
names of things, such as variables, constants, and methods, that appear
in programs. A Java identifier consists of letters, digits, the
underscore character ( _), and the dollar sign ($), and must begin with a
letter, underscore, or the dollar sign.
Data Types
The objective of a Java program is to manipulate data. A Data type is a set of values together with a set of operations. Only certain operations can be performed on a particular type of data.
Primitive Data Types
There are three primitive data types: integral, floating-point, and boolean.
The integral data type deals
with integers, or numbers without a decimal part. It is classified
into five categories: char, byte, short, int, long. The int data type
can represent integers between
-2147483648 and 2147483647. The data type short is used to represent integers between –32768 and 32767.
-2147483648 and 2147483647. The data type short is used to represent integers between –32768 and 32767.
The floating-point data type deals with decimal numbers.
The boolean data type deals with logical values.
int Data Type
Positive integers do not have to have a + sign in front of them. No commas are used within an integer.
char Data Type
The
char data type is used to represent single characters such as letters,
digits, and special symbols. It can represent any key on your keyboard.
Each character represented is enclosed within single quotation marks.
Only one symbol can be placed between the single quotation marks.
Java
uses the Unicode character set, which contains 65536 values numbered 0
to 65535. Each of the 65536 values of the Unicode character set
represents a different character. Each character has a predefined
ordering, which is called a collating sequence, in the set. This is used when you compare characters.
boolean Data Type
The data type boolean has only two values: true and false. These are called the logical (Boolean) values. The central purpose of this data type is to manipulate logical (Boolean) expressions.
An expression that evaluates to true or false is called a logical (Boolean) expression.
Floating-Point Data Types
The
floating-point data type deals with decimal numbers. To represent real
numbers, Java uses a form of scientific notation called floating-point notation.
The
data types float and double are used to manipulate decimal numbers.
Floats represent any real number between –3.4E+38 and 3.4E+38. The
memory allocated for the float data type is 4 bytes. Doubles are used to
represent any real number between –1.7E+308 and 1.7E+308. The memory
allocated for the double data type is 8 bytes.
The
maximum number of significant digits in float values is 6 or 7, while
the maximum number of significant digits in double values is 15.
Input
Storing data in the computer’s memory is a two-step process:
1. Instruct the computer to allocate memory.
2. Include statements in the program to put the data into the allocated memory.
Allocating Memory with Named Constants and Variables
When
you instruct the computer to allocate memory, you tell it what names to
use for each memory location, and what type of data to store in those
memory locations. Knowing the location of data is essential because data
stored in one memory location might be needed at several places in the
program. It is also critical to know whether your data must remain
fixed throughout program execution or whether it should change.
A named constant is a memory location whose content is not allowed to change during program execution.
Declaration statements allocate memory. The syntax to declare a named constant is:
static final dataType IDENTIFIER = value;
In
Java, static and final are both reserved words. The reserved word final
specifies that the value stored in the identifier is fixed and cannot
be changed. The reserved word static may or may not appear when a named
constant is declared.
Using
a named constant to store fixed data, rather than using the data value
itself, has one major advantage. If the fixed data changes, you do not
need to edit the entire program and change the old value to the new
value. Instead, you can make the change at just one place, recompile the
program, and execute it using the new value throughout. In addition, by
storing a value and referring to that memory location whenever the
value is needed, you avoid typing the same value again and again and you
prevent typos.
Walang komento:
Mag-post ng isang Komento