At the end of this chapter, you should be able to
Write Java programs with good grasp and understanding of fundamentals.
Understand data types and their usage.
Use compound statements and increment and decrement operators.
Understand operators and their precedence and association rules.
Use relational and equality operators.
Understand and use control loops.
In this chapter, we introduce you to Java programming fundamentals like the data types permitted by Java language, together with various operators like logical operators, arithmetic operators, etc. In Chapter 15, you have of course used some of these features, but in this chapter we will provide you with underlying syntax, grammar and theory of the Java language. We have shown working of various types of operators such as binary operators and unary operators and bit-wise operators with sample programs. The precedence and association of operators are presented.
There are basically two types of statements, viz., sequential and control statements. Control statements alter the sequence of execution of statements. Repetitive execution of a block of statements is controlled by counter controlled types of statements executed by commands like for, while, do-while and switch statements.
They do not change their value during running of the program. Note that constants are also called literal constants and are presented in Figure 16.1.
Figure 16.1 Java literal constants
They can be subdivided into
Example: int myAge=56; // 56 is in decimal
Octal integer constants: Only digits between 0 and 7 are allowed. All octal numbers must start with 0 digit to identify as octal number.
Allowed octal constants: 0777, 001, 0116, 07565L (octal long)
Illegal octal constants are: 089 − 8 is illegal, 777 − does not start with 0
: -0675.76 − . is illegal
Example: int myAgeOctal=070; // 70 is octal equivalent of 56
Hexadecimal constants: A hexadecimal number must start with 0x or 0X followed by digits 0 to 9 or alphabets a to f, both uppercase or lowercase allowed.
Allowed hexadecimal constants are: 0xffff, 0xa11f, 0x65000UL
Illegal hexadecimal constants are: 0x14.55, illegal character “.”
Example: int myAgeHexa=0x38; // 38 is Hexa equivalent of 56
They are base – 10 number that can be represented either in exponent form or decimal point representation.
Valid floating point constants are: 0.01, 789.89765, 5E-5, 1.768E+9
Invalid floating point declarations are:
6 invalid . must contain exponent or float point.
5E+12.5 Invalid as exponent cannot be float.
6,789.00 Invalid character “,”
Example: float area = 567.89; // float value
double area2 = 567.89; //double value
double area3 = 5.6789e2 ; // scientific notation
Character constants can be declared based on the character set followed in a computer. ANSI have standardized these values as shown below.
In addition, Java allows Unicode characters like ISO Latin for example: u0042,u0043 etc. for Latin character set a, b, etc. A character constant contains a single character enclosed within a pair of single quote marks. Examples of character constants are: ‘5’ ‘X’ ‘;’ ‘ ‘
Note that the character constant ‘5’ is not the same as the number 5. The last constant is a blank space. Character constants have integer values known as ASCII values. Special characters that cannot be printed normally, double quote (“), apostrophe (‘), question mark (?) and backslash () can be represented by using escape sequences. An escape sequence always starts with followed by special character stated above.
String constants can contain any number of characters in sequence but enclosed in double quotation marks.
“new delhi” , “14 Nov 1954” , an empty string is “”.
Please note that NULL character indicates NULL character and is used by Java language to indicate the end of a string.
A few of the backlash character constants that are supported by Java and extensively used in output programming are shown in Table 16.1.
Table 16.1 Precedence and association rules for the operators
Special Character | Escape Sequence |
---|---|
Bell | a |
Back space | |
Horizontal tab | |
Vertical tab | v |
Form feed | f |
New line | |
Carriage return | |
Double Quote | ” |
Apostrophe/Single quote | ’ |
Backslash | \ |
Null |