Index

chapter1

chapter2page1

chapter2page2

chapter2page3

chapter2page5

chapter2page6

chapter2page7

chapter2page8

chapter2page9

chapter2page10

chapter2page11

chapter2page12

chapter2page13

chapter2page14

chapter2page15

chapter2page16

chapter2page17

chapter2page18

chapter2page19

chapter2page20

chapter2page21

chapter2page22

chapter2page23

chapter2page24

chapter2page25

chapter2page26

chapter2page27

chapter2page28

chapter2page29

chapter2page30

chapter2page31

chapter2page32

chapter2page33

chapter2page34

chapter3

link one

link one

link one

link one

link one

link one

2.4 - Literals

The world literal comes from latin lettera which means something written. Its meaning in programming languages is approximately the same. The values entered from the keyboard or read from a data file are called literals. Literals are sometimes obvious like -34 but there is more on literals in Java.

2.4.1 - Binary Literals

Binary numbers have only two digits, 0 and 1. Binary numbers are the only digits that can be applied in machine codes. Machine code literals or binary literals are prefixed by 0b (zero b) and continue with a sequence of 1 and 0's. As an example, 0b1001001 is a binary literal.

Support of binary literals came with Java Se 7. Any program including binary literals is not compatible with previous releases of JVM.

2.4.2 - Octal Literals

Java supports octal (base 8) , system integers. Octal base integers begin with a prefix zero. Any number beginning with zero is interpreted as an octal number like 023. Octal numbers only include numbers from 0 to 7. There is some use of octal numbers in electonic circuit design.

2.4.3 - Hex Literals

Hex base integers are expressed with prefix 0x. Hex numbers include digits from 0 to 9 and characters from A to F (iclusive). 0x7FFA is a valid hex literal. The main usage of the hex numbers is in the memory manipulations in low- level programming. Java programming language, by definition do not allow users to interact with memory alocations. This reduce to need of the hex literals in, Java programming language.

2.4.4 - Integer Literals

Integer literals of the type long is marked with an L or l at the end like 78L . Using uppercase L is preferable in order to avoid confusion with 1. Integer literals without the suffix L or l are accepted as belonging to the type int.

Java SE 7 supports underscore characters in integer literals as 67_8654. There not any restriction about the number of characters in the parts separated with underscores. But underscores can not be put;

This is an useful feature which facilitate data entry, but it is not backwards compatible. That means when underscores are applied in a program, only the compilers of Java SE 7 or above may compile or interpret the codes. If the these codes will be ported to a computer with an older Java compiler, a runtime error will be generated.

2.4.5 - Real Literals

Floating point literals of type float, has f or F as a suffix like 5.34f or 0.56F. Floating point literals of type double, has also D or d as a suffix but it is omitted by default. When a floating point literal is expressed without a suffix, it is considered as being of type double.

Floating point literals may be expressed with a scientific notation using the suffix E or e like 3.56E-2 which corresponds to the literal 0.0356.

2.4.6 - Character Literals

Java char data type literals are introduced in single quotes like 'w' , '\u0065' or '\n' . Java chartype data were 2 bytes (16 bit) Unicode data before Java SE 7 release. They stillremain at Java SE 7 but it is also added a new surrogate char type that is beyond 16 bit limit up to 32 bit long. Surrogate characters are not backwards compatible with releases earlier than Java Se 7. This will not impact really the developers since char type data beyond 16 bit range is not likely to be used in common practice.

The character between single quotes may be a character in the keyboard or an escaped Unicode entry point or an escaped character which as a specialised meaning, all kinds of legal character literals will be evaluated properly by the Java compiler. Entries of Unicode escape sequences may be tiresome if they have to be entered very frequently. Fortunately, there is localised keyboards for all the countries having their own alphabet and character data is normally entered as keyboard strokes, instead of time and effort consuming Unicode escaped literals. But sometimes, there might be a need for a non-common character which may not be present at the keyboard. In that case, we will have to search the Unicode entry point of this character from the Unicode character charts and enter this character as an escaped Unicode character sequence. But we won't need this very frequently.

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table from official Java documentation shows the Java escape sequences:

Escape Sequences
Escape Sequence Definition
\t Insert a tab in the text at this point7
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.

2.4.7 -Boolean Literals

A boolean literal may be false or true, without quotes.

These are the literals belonging to the primitive types. But there is one literal of a predefined object type String which is very much applied in applications. For this reason, we will also introduce the basic information about predefined String type and String literals.

2.4.8 - String Literals

String literals do not belong to any of the 8 predefined primitive types. Strings are succession of characters and they are objects. But string literals may be considered as string constants whose behaviour is similar to the primitive data types, i.e. string literals are of value data type.

String literals are expressed between double quotes like "Hello World". Strings may be of arbitrary length. Numerical data may be converted to strings and vice-versa, sometimes automatically. Strings may contain escaped characters (escape sequences) like "Present Day : \n Friday". If this string will be displayed in an environment which supports escape sequences, Friday will be displayed in the next line.

String objects are immutable. That means once defined the content of a string can not be changed. This is in contrast to the common objects which are comprising from different parts and they can be altered after the creation by changing some of its parts. This is not the case with string objects which are comprised only from characters. Defining a String object, is like defining a numerical variable, one value by definition and inherently immutable. This make strings behave closer to the primitive types.

Strings may be concatenated, i.e. assembled by using + operator. Also, Java has a wealth of predefined classes for application in the string manipulations. We will cover these methods in detail, after we have studied Object Oriented Programming.

2.4.9 - null Literal

This literal has only one value null. It only used to give initial null values to some variables.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)