Sabado, Enero 26, 2013

Arithmetic Operators and Operator Precedence


Arithmetic Operators and Operator Precedence

There are five arithmetic operators:

+          addition
-           subtraction
*          multiplication
/           division
%         mod (modulus) operator

You can use these operators with both integral and floating-point data types. Integral division truncates any fractional part; there is no rounding.

An arithmetic expression is constructed by using arithmetic operators and numbers. The numbers in the expression are called operands. Moreover, the numbers used to evaluate an operator are called the operands for that operator. Operators that have only one operand are called unary operators.  Operators that have two operands are called binary operators.


Order of Precedence

Java uses operator precedence rules to determine the order in which operations are performed to evaluate the expression.  According to the order of precedence rules for arithmetic operators, *, /, and % have a higher level of precedence than + and -. 

Because arithmetic operators are evaluated from left to right, unless parentheses are present, the associativity of arithmetic operators is said to be from left to right.

Since the char data type is also an integral data type, Java allows you to perform arithmetic operations on char data.


Expressions

If all operands in an expression are integers, the expression is called an integral expression. If all operands in an expression are floating-point numbers, the expression is called a floating-point or decimal expression

Evaluating an integral or a floating-point expression is straightforward. When operators have the same precedence, the expression is evaluated from left to right. To avoid confusion you can always use parentheses to group operands and operators.

boolean Data Type, Floating-Point Data Types


boolean Data Type

The data type boolean has only two values: true and false. These are called thelogical (Boolean) values. The central purpose of this data type is to manipulate logical (Boolean) expressions.

An expression that evaluates to true or false is called a logical (Boolean) expression.


Floating-Point Data Types

The floating-point data type deals with decimal numbers.  To represent real numbers, Java uses a form of scientific notation called floating-point notation

The data types float and double are used to manipulate decimal numbers. Floats represent any real number between –3.4E+38 and 3.4E+38.  The memory allocated for the float data type is 4 bytes. Doubles are used to represent any real number between –1.7E+308 and 1.7E+308. The memory allocated for the double data type is 8 bytes.

The maximum number of significant digits in float values is 6 or 7, while the maximum number of significant digits in double values is 15.

int Data Type, char Data Type


int Data Type

Positive integers do not have to have a + sign in front of them. No commas are used within an integer.


char Data Type

The char data type is used to represent single characters such as letters, digits, and special symbols. It can represent any key on your keyboard. Each character represented is enclosed within single quotation marks. Only one symbol can be placed between the single quotation marks.

Java uses the Unicode character set, which contains 65536 values numbered 0 to 65535. Each of the 65536 values of the Unicode character set represents a different character. Each character has a predefined ordering, which is called acollating sequence, in the set.  This is used when you compare characters

Primitive Data Types,


Primitive Data Types

There are three primitive data types: integral, floating-point, and boolean.

The integral data type deals with integers, or numbers without a decimal part.  It is classified into five categories: char, byte, short, int, long. The int data type can represent integers between
-2147483648 and 2147483647. The data type short is used to represent integers between –32768 and 32767.

The floating-point data type deals with decimal numbers.

The boolean data type deals with logical values.

Identifiers, and Data Types


Identifiers


Identifiers are names of things, such as variables, constants, and methods, that appear in programs. A Java identifier consists of letters, digits, the underscore character ( _), and the dollar sign ($), and must begin with a letter, underscore, or the dollar sign.


Data Types

The objective of a Java program is to manipulate data.  Data type is a set of values together with a set of operations. Only certain operations can be performed on a particular type of data.

Special Symbolsm and Word Symbols


Special Symbols

The following are special symbols in Java.

+          -           *          /
.           ;           ?          ,
<=        !=         ==        >=

The first row of symbols includes mathematical symbols for addition, subtraction, multiplication, and division. In Java, commas are used to separate items in a list. Semicolons are used to end a Java statement.  The symbols in the third row are used for comparisons.


Word Symbols

Word symbols are called reserved words or keywords. Examples include int, float, double, char, void, public, static, throws, return.

The Basics of a Java Program


The Basics of a Java Program


A programming language is a set of rules, symbols, and special words.  Thesyntax rules of a language determine which instructions are valid.  The semantic rules determine the meaning of the instructions.  Together these rules enable you to write programs to solve problems.

The smallest individual unit of a program written in any programming language is called a token. Java’s tokens are divided into special symbols, word symbols, and identifiers.