Professional Documents
Culture Documents
1 Identifiers
Symbolic names such as variables, labels, class names are called identifiers.
In Java, an identifier
Note
Java definition of an identifier is much more general than described here. But, we
will keep this definition.
2 Keywords
The following is the list of keywords in Java. Keywords are reserved - you cannot
use these as your identifiers.
Note:
• true and false are boolean literals (constants). They are not keywords; still
they cannot be used as identifiers.
3 Data Types
All the data a program uses is divided into different categories. These categories
are called data types.
Basic data types in Java are divided into two kinds: primitive type and reference
types. In this chapter we will discuss primitive data types in detail. We will not
discuss reference data types here.
The primitive types are: Integral type, Floating-point type and Boolean type.
Integral type:
A whole number, without a decimal point is called an integer. A variable or an
expression whose value is an integer is said to be of integer type. No commas are
allowed in representing an integer constant. An integer can internally be stored
using one, two, four or eight bytes. Depending on the storage size (1, 2, 4, or 8
bytes), integers are divided into four types: byte, short, int, and long. They
respectively take 1, 2, 4, and 8 bytes in memory.
Character type:
Internally, a character is stored using two bytes. The character can be one
character from the Unicode list. The name for the character type is char. For all
practical purposes, char is considered an integer type. Character constants are
typed using single quotes (' ').
Floating-point type:
A number represented using a decimal point or represented in the scientific
notation is called a floating-point number. A variable or an expression whose
value is a floating-point number is said to be of floating-point type. No commas
are allowed in representing a floating-point constant. A floating-point number is
internally stored using either 4 or 8 bytes. Depending on the storage size (4
bytes or 8 bytes) floating-point is divided into two types: float and double. The
data type float takes 4 bytes and double takes 8 bytes in memory.
Boolean type:
The truth-values true and false are called boolean constants. A variable or an
expression whose value is one of these two is said to be of type boolean.
Note:
Bits and bytes are introduced in Appendix A.
The following table lists the size and range of values for all the primitive data
types.
Note:
• For all practical purposes, the string null is considered a reference literal.
• A bit stands for a binary digit. There are two binary digits: 0 and 1.
Please see appendix A for a detailed discussion on number systems
•
• Octal notation
Practice Example 4
Practice Exercise 4
• Hexadecimal notation
Practice Example 4A
Note:
• Scientific notation
The scientific format for a floating-point number is NeP, where N is any number
(integer or floating-point) and P is an integer (negative, zero or positive).
e or E can be used.
Practice Example 4B
4.21e3 is 4210.0
Practice Example 4C
4.21E-2 is 0.0421
Practice Exercise 4B
Practice Exercise 4C
The suffixes can be used with constants. The following table shows permitted
suffixes and their meaning:
Note:
6 Named constants
You can give a particular value a name. Every time you want to use the value,
you can just use the name. To make the value a constant you declare the field
as final. Once you declare a field final its value immutable, its value cannot be
changed.
Example:
Some of the constants such as maximum int, minimum int, have symbolic
names associated with them. Java predefines these names. The following table
lists the symbolic names, and their values.
Note:
The significance of Integer, Long, Float and Double in the table will be made
clear in a later chapter.
16G Answer0207
8 Escape sequences
The following are special characters, which are called escape sequences. The
following table gives the meaning of these special characters.
Please read the table for now. You may have to come back to this later when we
use these notations.
Note:
• The usual escape sequences \a (bell) and \v (vertical tab) are not available in
Java.
•
Java provides a built-in String type. As you will see later, String is a predefined
class. A character string enclosed within double quotes (" ") is really an object of
the String type. We will discuss String class, its characteristics in detail later.
For now, we will use String as a data type.
Example:
Even though a string, such as "421" looks like a number, we cannot use it as a
number. Internally it will not be represented as a number instead it is a string.
If you want to use a string "which looks like" a number, as a number, first you
need to convert the string into a number.
In the following table string is the String value you are trying to convert.
Note:
• You will get an error message, if the string has a non-numeric character.
11 Declaring variables
Syntax:
type listOfVariables;
Example:
int a,b,c;
Three variables, a, b, c are declared as int type. We can now save int data in
the variables a, b, and c.
Example:
float x, y;
Two variables x and y are declared as float type. We can now save float data in
the variables x and y.
Example:
String name;
The name is declared as String type. We can now save String data in the
variable name.
Example:
Several variables of the same type can be declared in one declaration or several
declarations:
float p, q, r;
is equivalent to
float p;
float q;
float r;
12 Initializing variables
A declaration of a variable just makes the variable ready to receive data of the
declared type. You can save data in a variable at the time of declaration.
Syntax:
As a rule, the value on the right side must be the same type as the type of the
variable.
Example:
Here the variable number1 is not only declared as an int but a value 14 is saved
in it.
16G Answer0207
Example:
Here variables score1, score2 and score3 are declared and initialized at the same
time.
Example:
Here the variable city is declared as a String variable and also it is assigned the
string New York.
If both operands are numbers, the operator + gives the sum of the numbers.
On the other hand, if one or both operands are of type String, the operator +
concatenates the two strings. The result is of type String in this case.
Example:
• 2 + 4.2 is 6.2.
• "abc"+"xyz" is "abcxyz".
Example:
int x = 15;
String sam = "New York";
String result;
result = x+sam;
The value of result is "15New York".
Example:
Notice that the value of result is not 27. If you really like to add values of value1
and value2 as numbers, first you need to convert the string values into int values
using the function Integer.parseInt() as described earlier.
As a general rule, spaces do not matter in the code. Of course, there cannot be
spaces within keywords and identifiers. Where you can put one space, you can
put more spaces.
Example:
int x = 4;
You can rewrite the statement as follows:
int x=4;
Example:
int x=4;
float y=3.4f;
You can rewrite them as follows:
int x=4; float y=3.4f;
15 Programming Examples
Programming Example 1
This examples assigns values to variables in octal and hexadecimal format and
prints them in the usual decimal format.
Output:
Value of a is = 171
Value of b is = 2635
Value of c is = 511
Value of d is = 43981
16G Answer0207
Programming Example 2
In this example we assign values to variables in scientific notation and print them
in the usual decimal format.
Output:
Value of a is = 4210.0
Value of b is = 0.534
Value of c is = 5.346
Value of d is = 546.0
Programming Example 3
The following example prints the largest and the smallest values in several of the
data types. Also notice how tabs ('\t') are used.
OUTPUT:
Programming Example 4
Output:
Note:
The parentheses around a+b are required. Observe that the following code will not
produce the required result:
System.out.println("Sum of a and b \t"+a+b);
16 Chapter 2 Exercises
Problem 1:
Indicate if the following identifiers are valid or invalid. Explain why invalid
identifiers are so.
Problem 2
Problem 3
Write the values of each of the following numbers in the usual decimal format:
2.43e3 Answer
12.003e-2 Answer
0.0034E4 Answer
0234 Answer
0xABC Answer
0Xa2c Answer
0134 Answer
53.4e0 Answer
Problem 4
Write the declaration for each of the following:
In each of the following problems, write the value of the variable answer. If
there is something wrong in the code, explain a possible fix for the mistake and
then write the value of the variable answer.
Problem 6
Write a program to assign 0647, 0x5AB, 0XAAA, 4.32e4, 0.0035e3, 0.345e-2 to
variables and print the values of the variables in the decimal format.
Problem 7
Assign "24.35" and "87.23" to two variables and print their sum (i) as strings and
(ii) as numbers.
16G Answer0207
class Exercise0207
{
public static void main(String args[])
{
String x = "24.35";
String y = "87.23";
String a = x+y;//adding as strings
double b = Double.parseDouble(x) +Double.parseDouble(y);
System.out.println("x and y added as strings "+a);
System.out.println("x and y added as numbers "+b);
}
}