Nice book written by Ivor Horton.
Below are few quotations i found from this book from the first 3 chapters.
1) Java and Unicode
Programming to support languages that use anything other than the Latin character set has always been a major problem. There are a variety of 8-bit character sets defined for many national languages, but if you want to combine the Latin character set and Cyrillic in the same context, for example, things can get difficult. If you want to handle Japanese as well, it becomes impossible with an 8-bit character set because with 8 bits you have only 256 different codes so there just aren’t enough character codes to go round.Unicode is a standard character set that was developed to allow the characters necessary for almost all languages to be encoded. It uses a 16-bit code to represent a character (so each character occupies 2 bytes),
and with 16 bits up to 65,535 non-zero character codes can be distinguished. With so many character codes available, there is enough to allocate each major national character set its own set of codes, including character sets such as Kanji, which is used for Japanese and which requires thousands of character codes. It doesn’t end there though. Unicode supports three encoding forms that allow up to a million
additional characters to be represented.
Java source code is in Unicode characters. Comments, identifiers, and character and string literals can all use any characters in the Unicode set that represent letters. Java also supports Unicode internally to represent characters and strings, so the framework is there for a comprehensive international language capability in a program. The normal ASCII set that you are probably familiar with corresponds to the first 128 characters of the Unicode set.Apart from being aware that each character usually occupies 2 bytes, you can ignore the fact that you are handling Unicode characters in the main, unless of course you are building an application that supports multiple languages from the outset.
I say each Unicode character usually occupies 2 bytes because Java supports Unicode 4.0, which allows 32-bit characters called surrogates. You might think that the set of 64K characters that you can represent with 16 bits would be sufficient, but it isn’t. Far-eastern languages such as Japanese, Korean, and Chinese alone involve more than 70,000 ideographs, and surrogates are used to represent characters that are not contained within the basic multilingual set that is defined by 16-bit characters.
2) Variables with a Fixed Set of Integer Values
We will often need variables that can have values only from a predefined fixed set. For example, suppose you want to define an integer variable with the name weekday, which will store an integer value representing a day of the week. The variable ideally needs to be limited to seven possible values, one for each of Monday through Sunday. This is a situation where a facility called an enumeration is a natural choice. You could define an enumeration for this situation with the following declaration statement:
enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
Example:
public class TryEnumeration {
// Define an enumeration type for days of the week
enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
public static void main(String[] args) {
// Define three variables of type Day
Day yesterday = Day.Thursday;
Day today = Day.Friday;
Day tomorrow = Day.Saturday;
// Output the values of the Day variables
System.out.println("Today is " + today);
System.out.println("Tomorrow will be " + tomorrow);
System.out.println("Yesterday was " + yesterday);
}
}
3) It is good practice to always put opening and closing braces around the code dependent on an if condition,even when there is only a single action statement. This helps to make the code easier to follow and will minimize the possibility of the program logic being confused.
4) We can use relational operators to compare values, and such comparisons result in values of either true or false.
5) We can combine basic comparisons and logical variables in more complex logical expressions by using logical operators.
6) The if statement is a basic decision-making tool in Java. It enables us to choose to execute a block of statements if a given logical expression has the value true. us can optionally execute another block of statements if the logical expression is false by using the else keyword.
7) We can use the conditional operator to choose between two expressions depending on the value of a logical expression.
8) We can use the switch statement to choose from a fixed number of alternatives.
9) The variables in a method come into existence at the point at which us declare them and cease to exist after the end of the block that immediately encloses their declaration. The program extent where the variable is accessible is the scope of the variable.
10) We have four ways of repeating a block of statements: a numerical for loop, a collection-based for loop, a while loop, or a do while loop.
11) The continue statement enables us to skip to the next iteration in the loop containing the continue statement.
12) The labeled continue statement enables us to skip to the next iteration in a loop enclosing the labeled continue that is identified by the label. The labeled loop need not be that immediately enclosing the labeled continue.
13) The break statement enables us to break out of a loop or block of statements in which it appears.
About the Author
Ivor Horton started out as a mathematician, but shortly after graduating, he was lured into messing about with computers by a well-known manufacturer. He has spent many happy years programming occasionally useful applications in a variety of languages as well as teaching mainly scientists and engineers to do likewise. He has extensive experience in applying computers to problems in engineering design and to manufacturing operations in a wide range of industries. He is the author of a number of tutorial books on programming in C, C++, and Java. When not writing programming books or providing advice to others, he leads a life of leisure.
No comments:
Post a Comment