Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Types. Primitive types.

Programs and algorithms | Algorithms and programming languages | Fundamentals of programming I | Student is obliged to write down and run this program by himself | Data in a program (variables and literals) | Operations on data (operators and expressions) | Fundamentals of programming II | Write down and run this program on your computer | Write down and run this program on your computer | Introduction to objects |


Читайте также:
  1. Classification of Head Types. General Principles.
  2. II. Primitive Religion
  3. Make up Conditional sentences of all types.
  4. Stages of primitive community.
  5. XXVIII. The notion of syntactic relations. Their main types.

Execution of a program results in the processing of data. The data is stored in computer's memory. Each data unit must have a type which specifies how the data is stored in memory, how much space it occupies and how operations are carried out upon that data. This information helps a compiler to build binary code properly.

 

The type of data is the set of permissible values for that data plus the set of operations which can be carried out upon this data.
It specifies the size of memory needed to store the data and the manner of storing it in the computer's memory.

 

For example, data of integral type may have values which are integer numbers from a definite interval. The size of the interval depends on the size of data and the manner in which data is stored in memory.

Data in a program is represented with the help of literals, variables and constants.
Thus, the values of literals, variables and constants have types.
We say: a variable of... type, a constant of... type, a literal of... type.
Just because expressions are made of variables, constants, literals and operations carried out upon them (defined by the operators), as well as function (method) invocations - the result of evaluation of an expression has some definite type too.

Each type has its name.
A programming language has a number of so called primitive types the names of which are reserved keywords of the language.

Type name byte count Permissible values The meaning
byte   -128...127 integer numbers    
short   -32768...32767
int   -2147483648... 2147483647
long   -9223372036854775808....9223372036854775807
float   -3.4028234663852886E38......-1.401298464324817E-45 1.401298464324817E-45......3.4028234663852886E38 real numbers
double   -1.7976931348623157E308......- 4.9E-324 4.9E-324...... 1.7976931348623157E308
char   0...65556 Unicode characters
boolean   true | false logical values: true or false


The types: byte, short, int, long, float, double and char are called numeric types. They represent data which are numbers.
In particular, characters (the type char) are represented by unsigned (nonnegative) integer numbers. There is a number of different character coding systems (for example, an ASCII character is a nonnegative number less than 256). Java uses Unicode characters which are nonnegative numbers less than 65557 (not all of them are defined yet). Unicode allows representing characters of almost all languages (including simplified Chinese).
Results of arithmetical operations are values of numeric types. Values of the type char are elements of strings.
Arithmetical operations specified with the help of operators include: addition (+), multiplication (*), subtraction (-), division (/), remainder (%), incrementation (++) and decrementation (--) by 1. For operands of integral type, the division operation is the integer division, i.e. its result is of integral type (thus, the value of the expression 1/3 is 0 as the operands are integers and the result is rounded to an integer).
The values of numeric types are subject to comparisons:< =, == (equal?),!= (not equal?).

The boolean type represents a logical quantity with two possible values, indicated by the literals true and false Values of this type are used in logical expressions as operands and results. Boolean expressions determine the control flow in several kinds of statements.
For example, the expression a > b has the value of the boolean type specifying whether the result of comparison is true or false. The logical expression "x and y" is noted in Java as x && y. It is syntactically correct only if x and y are of boolean type. Its value (specifying whether x and y are true) is of boolean type too.

 

Literals


Types of literals are determined by the compiler applying some rules:

The programmer may change the default interpretation of numerical literals with the help of specific modifiers:

An integer literal may be expressed in:

Real number literals may be expressed in scientific notation: 2e+9 denotes 2 multiplied by 10 raised to the 9th power whereas 3e-11 denotes 3 multiplied by 10 raised to the -11th power

In expressions of the form 10/3 (division of integers), where both operands are of type int, the type of the result is int too. As a consequence its value turns out to be 3 (not 3.33333....). To enforce treating it as a real value, the suffix d must be appended to at least one of the operands (specifying literal of the type double). The other solution is to use real number literal as an operand by including the decimal point in its notation (for example 10.).

The following program illustrates the various possibilities discussed above:

public class LitLicz { public static void main(String[] args) { System.out.println(10 + 0x10); System.out.println(10/3); System.out.println(10./3); System.out.println(10d/3); System.out.println(2147483648L); System.out.println(2147483647 + 1); System.out.println(2147483647L + 1); }}


26
3
3.3333333333333335
3.3333333333333335
2147483648
-2147483648
2147483648

The result of the execution of this program is shown on the figure.


Note that:

 

Character literals (of the type char) are noted as single characters enclosed in apostrophes. String literals are sequences of characters enclosed in quotation marks.

String literals are objects of the class String.

 


An escape sequence is a sequence of characters starting with the escape character (backslash). Escape sequences allow for the representation of some nongraphic characters as well as the single quote, double quote, and backslash characters in character literals

Escape sequence Notation
Linefeed - LF \n
Horizontal tab - HT \t
Backspace - BS \b
Carriage return - CR \r
Form feed - FF \f
Single quote - ' \'
Double quote - " \"
Backslash - \ \\
Unicode escape - arbitrary character which Unicode encoding is NNNN (N is a hexadecimal digit) \uNNNN


Note:


The following test demonstrates the usage of character and string literals. Note alternative way of displaying data to the command prompt: the method System.out.println(...) prints its argument and terminates the current line by writing the line separator string, whereas the method System.out.print(...) prints its argument without terminating the line.

public class LitZn { public static void main(String[] args) { System.out.print('a'); System.out.print('\u0061'); System.out.print('\\'); System.out.print('\''); System.out.println(" First Second Third "); System.out.println("All work and no play\nMakes Jack a dull boy"); System.out.println("c:\\util\\bak"); System.out.println(""); System.out.println("\""); System.out.print("abcd\r12\n1234\rab"); }}


aa\' First Second Third
All work and no play
Makes Jack a dull boy
c:\util\bak

"
12cd
ab34

The output of the above program is shown on the nearby figure. Note that
'\u0061' is the hexadecimal Unicode encoding of the character 'a'.
The single quote and the backslash characters in literals are prefixed with backslash: \' and \\.
Escape sequences may occur in string literals: the '\n' character inside the string "All work and no play\nMakes Jack a dull boy" is the linefeed character which terminates the current line. That is why the substring "Makes Jack a dull boy" is printed in a new line.
A pair of backslashes contained in a string literal is printed as a single backlash.
Double quotes must be prefixed with escape character (backslash).
The last instruction of the program acts as follows:

  1. firstly, the string "abcd" is printed
  2. then the carriage return character ('\r') is printed what results in drawing back the caret to the beginning of the line.
  3. at the beginning of the line the characters 1 and 2 are printed resulting in overwriting of the characters a and b
  4. the linefeed character '\n' terminates the current line, thus the following characters are printed on a new line
  5. the string 1234 is printed on the new line
  6. the carriage return character draws back the carriage to the beginning of the line
  7. the characters a and b are printed overwriting the two initial characters 1 and 2 of the previously printed string 1234.

 


Дата добавления: 2015-11-16; просмотров: 61 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
The first program| Types of variables. Declarations.

mybiblioteka.su - 2015-2024 год. (0.007 сек.)