10x Blogs Directory

Wednesday, February 25, 2009

Logical Operators

  • Logical operators have one or two boolean operands that yield a boolean result.
  • There are six logical operators:
    - && (logical AND)‏
    -& (boolean logical AND)‏
    - (logical OR)‏
    - (boolean logical inclusive OR)‏
    -^ (boolean logical exclusive OR)‏
    -! (logical NOT)‏


&&(logical) and &(boolean logical) AND

  • The basic difference between && and & operators :
    -&& supports short-circuit evaluations (or partial evaluations), while & doesn't.
  • Given an expression:exp1 && exp2
    -&& will evaluate the expression exp1, and immediately return a false value is exp1 is false. -If exp1 is false, the operator never evaluates exp2 because the result of the operator will be false regardless of the value of exp2.
  • In contrast, the & operator always evaluates both exp1 and exp2 before returning an answer.


(logical) and (boolean logical) inclusive OR

  • The basic difference between and I operators :
    - supports short-circuit evaluations (or partial evaluations), while doesn't.
  • Given an expression:exp1 exp2
    - will evaluate the expression exp1, and immediately return a true value is exp1 is true
    -If exp1 is true, the operator never evaluates exp2 because the result of the operator will be true regardless of the value of exp2.
    -In contrast, the operator always evaluates both exp1 and exp2 before returning an answer.


^ (boolean logical exclusive OR)

  • The result of an exclusive OR operation is TRUE, if and only if one operand is true and the other is false.
  • Note that both operands must always be evaluated in order to calculate the result of an exclusive OR.


! ( logical NOT)‏

  • The logical NOT takes in one argument, wherein that argument can be an expression, variable or constant.


Conditional Operator (?:)‏

  • The conditional operator ?:
    -is a ternary operator.
  • This means that it takes in three arguments that together form a conditional expression.
    -The structure of an expression using a conditional operator isexp1?exp2:exp3
    wherein,
    exp1 - is a boolean expression whose result must either be true or false
    -Result:
    If exp1 is true, exp2 is the value returned.
    If it is false, then exp3 is returned.


Relational Operators



  • Relational operators compare two values and determines the relationship between those values.

  • The output of evaluation are the boolean values true or false.

Monday, February 23, 2009

Increment and Decrement Operators



  • unary increment operator (++)
  • unary decrement operator (--)‏
  • Increment and decrement operators increase and decrease a value stored in a number variable by 1.
  • For example, the expression, count=count + 1;//increment the value of count by 1 is equivalent to, count++;
  • The increment and decrement operators can be placed before or after an operand.
  • When used before an operand, it causes the variable to be incremented or decremented by 1, and then the new value is used in the expression in which it appears.
  • For example,
int i = 10;
int j = 3;
int k = 0;
k = ++j + i; //will result to k = 4+10 = 14


  • When the increment and decrement operators are placed after the operand, the old value of the variable will be used in the expression where it appears.
  • For example,
    int i = 10;
    int j = 3;
    int k = 0;
    k = j++ + i; //will result to k = 3+10 = 13

Arithmetic Operators

  • Note:
    -When an integer and a floating-point number are used as operands to a single arithmetic operation, the result is a floating point. The integer is implicitly converted to a floating-point number before the operation takes place.

Operators

  • Different types of operators:
    -arithmetic operators
    -relational operators
    -logical operators
    -conditional operators
  • These operators follow a certain kind of precedence so that the compiler will know which operator to evaluate first in case multiple operators are used in one statement.

Variables

  • A variable is an item of data used to store the state of objects.
  • A variable has a:
    -data type
  • The data type indicates the type of value that the variable can hold.
    -name
  • The variable name must follow rules for identifiers.

Declaring Variables

  • Declare a variable as follows:
    [=initial value];
  • Note: Values enclosed in <> are required values, while those values in [] are optional.

Coding Guidelines

  • It always good to initialize your variables as you declare them.
  • Use descriptive names for your variables. Like for example, if you want to have a variable that contains a grade for a student, name it as, grade and not just some random letters you choose.
  • Declare one variable per line of code. For example, the variable declarations,

double exam=0;

double quiz=10;

double grade = 0;
is preferred over the declaration,
double exam=0, quiz=10, grade=0;



Saturday, February 21, 2009

Primitive Data Types


  • The Java programming language defines eight primitive data types.
    -boolean (for logical)‏
    -char (for textual)‏
    -byte
    -short
    -int
    -long (integral)‏
    -double
    -float (floating point).




Logical-boolean

  • A boolean data type represents two states: true and false.

  • An example is, boolean result = true;

  • The example shown above, declares a variable named result as boolean type and assigns it a value of true.
Textual-char
  • A character data type (char), represents a single Unicode character.

  • It must have its literal enclosed in single quotes(’ ’).

  • For example: ‘a’ //The letter a ‘\t’ //A tab

  • To represent special characters like ' (single quotes) or " (double quotes), use the escape character '\'.

  • For example, '\'' //for single quotes '\"' //for double quotes

  • Although, String is not a primitive data type (it is a Class), we will just introduce String in this section.

  • A String represents a data type that contains multiple characters. It is not a primitive data type, it is a class.

  • It has its literal enclosed in double quotes(“”).

  • For example: String message=“Hello world!”;
Integral – byte, short, int & long
  • Integral data types in Java uses three forms – decimal, octal or hexadecimal.

  • Examples are, 2 //The decimal value 2 077 //The leading 0 indicates an octal value 0xBACC //The leading 0x indicates a hex value

  • Integral types has int as default data type.

  • You can define its long value by appending the letter l or L.

  • For example:10L

Floating Point – float and double

  • Floating point types has double as default data type.

  • Floating-point literal includes either a decimal point or one of the following, E or e //(add exponential value) F or f //(float) D or d //(double)

  • Examples are:

3.14 //A simple floating-point value (a double)

6.02E23 //A large floating-point value

2.718F //A simple float size value

123.4E+306D//A large double value with redundant D