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



Thursday, February 19, 2009

Literals

  • Literals are tokens that do not change - they are constant.
  • The different types of literals in Java are:
    -Integer Literals
    -Floating-Point Literals
    -Boolean Literals
    -Character Literals
    -String Literals

Integer Literals

  • Integer literals come in different formats:
    -decimal (base 10)‏
    -hexadecimal (base 16)‏
    -octal (base 8).
  • Special Notations in using integer literals in our programs:
    -Decimal
    No special notation
    example: 12
    -Hexadecimal
    Precede by 0x or 0X
    example: 0xC
    -Octal
    Precede by 0
    example: 014


Floating Point Literals

  • Represents decimals with fractional parts
    -Example: 3.1416
  • Can be expressed in standard or scientific notation
    -Example: 583.45 (standard), 5.8345e2 (scientific)‏


Boolean

  • Boolean literals have only two values, true or false.


Character Literals

  • Character Literals represent single Unicode characters.
  • Unicode character
    -a 16-bit character set that replaces the 8-bit ASCII character set.
    -Unicode allows the inclusion of symbols and special characters from other languages.
  • To use a character literal, enclose the character in single quote delimiter.
  • For example
    -the letter a, is represented as ‘a’.
    -special characters such as a newline character, a backslash is used followed by the character code. For example, \n’ for the newline character, ‘\r’ for the carriage return, ‘\b’ for backspace, '\t' for tab.


String Literals

  • String literals represent multiple characters and are enclosed by double quotes.
  • An example of a string literal is, “Hello World”.



Keywords

  • Keywords are predefined identifiers reserved by Java for a specific purpose.
  • You cannot use keywords as names for your variables, classes, methods ... etc.
  • Below is the list of the Java Keywords.

  • abstract
  • boolean
  • break
  • byte
  • bytevalue
  • case
  • catch
  • char
  • class
  • const
  • continue
  • default
  • do
  • double
  • else
  • extends
  • false
  • final
  • finally
  • for
  • goto
  • if
  • implements
  • import
  • instanceof
  • int
  • interface
  • long
  • native
  • null
  • package
  • private
  • protected
  • public
  • return
  • short
  • static
  • super
  • switch
  • synchronized
  • this
  • threadsafe
  • throw
  • transient
  • true
  • try
  • void
  • while

Identifiers

  • Identifiers
    -are tokens that represent names of variables, methods, classes, etc.
    -Examples of identifiers are: Hello, main, System, out.
  • Java identifiers are case-sensitive.
    -This means that the identifier Hello is not the same as hello.
  • Identifiers must begin with either a letter, an underscore “_”, or a dollar sign “$”. Letters may be lower or upper case. Subsequent characters may use numbers 0 to 9.
  • Identifiers cannot use Java keywords like class, public, void, etc. We will discuss more about Java keywords later.


Coding Guidelines:

  • For names of classes, capitalize the first letter of the class name. For example,
    ThisIsAnExampleOfClassName
  • For names of methods and variables, the first letter of the word should start with a small letter. For example,
    thisIsAnExampleOfMethodName
  • In case of multi-word identifiers, use capital letters to indicate the start of the word except the first word. For example,
    charArray, fileNumber, ClassName.
  • Avoid using underscores at the start of the identifier such as _read or _write.

Statements & Blocks

  • Statement
    -one or more lines of code terminated by a semicolon.
    -Example:System.out.println(“Hello world”);
  • Block
    -is one or more statements bounded by an opening and closing curly braces that groups the statements as one unit.
    -Block statements can be nested indefinitely.
    -Any amount of white space is allowed.
    -Example:

-public static void main( String[] args )

{

System.out.println("Hello");

System.out.println("world”);

}


Coding Guidelines:

  • In creating blocks, you can place the opening curly brace in line with the statement. For example:
    public static void main( String[] args ){
    or you can place the curly brace on the next line, like,
    public static void main( String[] args )‏
    {
  • You should indent the next statements after the start of a block. For example:
    public static void main( String[] args ){
    System.out.println("Hello");
    System.out.println("world");
    }


Java Comments

  • Comments
    -These are notes written to a code for documentation purposes.
    -Those texts are not part of the program and does not affect the flow of the program.
  • 3 Types of comments in Java
    -C++ Style Comments
    -C Style Comments
    -Special Javadoc Comments

C++-Style Comments

  • C++ Style comments starts with //
  • All the text after // are treated as comments
  • For example: // This is a C++ style or single line comments

C-Style Comments

  • C-style comments or also called multiline comments starts with a /* and ends with a */.
  • All text in between the two delimeters are treated as comments.
  • Unlike C++ style comments, it can span multiple lines.
  • For example: /* this is an exmaple of a C style or multiline comments */

Special Javadoc Comments

  • Special Javadoc comments are used for generating an HTML documentation for your Java programs.
  • You can create javadoc comments by starting the line with /** and ending it with */.
  • Like C-style comments, it can also span lines.
  • It can also contain certain tags to add more information to your comments.
  • For example: /** This is an example of special java doc comments used for \n generating an html documentation. It uses tags like: @author Vignesh @version 1.5*/



Coding Guidelines

  • The Java programs should always end with the .java extension.
  • Filenames should match the name of your public class. So for example, if the name of your public class is Hello, you should save it in a file called Hello.java.
  • You should write comments in your code explaining what a certain class does, or what a certain method do.

Analyzing the Basic Program

1 public class Hello
2 {
3 /**
4 * My first Java program
5 */

  • Indicates the name of the class which is Hello
  • In Java, all code should be placed inside a class declaration
  • The class uses an access specifier public, which indicates that our class in accessible to other classes from other packages (packages are a collection of classes). We will be covering packages and access specifiers later.

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */

  • The next line which contains a curly brace { indicates the start of a block.
  • In this code, we placed the curly brace at the next line after the class declaration, however, we can also place this next to the first line of our code. So, we could actually write our code as:
    public class Hello{

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */

  • The next three lines indicates a Java comment.
  • A comment
    -something used to document a part of a code.
    -It is not part of the program itself, but used for documentation purposes.
    -It is good programming practice to add comments to your code.

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){

  • Indicates the name of one method in Hello which is the main method.
  • The main method is the starting point of a Java program.
  • All programs except Applets written in Java start with the main method.
  • Make sure to follow the exact signature.

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){
7
//prints the string “Hello world” on screen

The 7th line is also a Java comment

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){
7 //prints the string “Hello world” on screen
8
System.out.println(“Hello world”);

The command System.out.println(), prints the text enclosed by quotation on the screen.
______________________________________________________________________

1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){
7 //prints the string “Hello world” on screen
8 System.out.println(“Hello world”);

9 }
10 }

The last two lines which contains the two curly braces is used to close the main method and class respectively.





Basic Elements For Programing

Sample Java Program:

1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){
7 //prints the string Hello world on screen
8 System.out.println(“Hello world”);
9 }
10 }

Phases of a Java Program






  • The above figure describes the process of compiling and executing a Java program



Java Features

  • Java Virtual Machine (JVM)‏
    -an imaginary machine that is implemented by emulating software on a real machine.
    -provides the hardware platform specifications to which you compile all Java technology code.
  • Bytecode
    -a special machine language that can be understood by the Java Virtual Machine (JVM)‏.
    -independent of any particular computer hardware, so any computer with a Java interpreter can execute the compiled Java program, no matter what type of computer the program was compiled on.
  • Garbage collection thread
    -responsible for freeing any memory that can be freed. This happens automatically during the lifetime of the Java program.
    -programmer is freed from the burden of having to deallocate that memory themselves.
  • Code security is attained in Java through the implementation of its Java Runtime Environment (JRE).
  • JRE
    -runs code compiled for a JVM and performs class loading (through the class loader), code verification (through the bytecode verifier) and finally code execution.
  • Class Loader
    -responsible for loading all classes needed for the Java program.
    -adds security by separating the namespaces for the classes of the local file system from those that are imported from network sources .
    -After loading all the classes, the memory layout of the executable is then determined. This adds protection against unauthorized access to restricted areas of the code since the memory layout is determined during runtime.
  • Bytecode verifier
    -tests the format of the code fragments and checks the code fragments for illegal code that can violate access rights to objects.

An Application and Runtime Environment

  • Java technology applications are typically general-purpose programs that run on any machine where the Java runtime environment (JRE) is installed.
  • There are two main deployment environments:


1. The JRE supplied by the Java 2 Software Development Kit (SDK) contains the complete set of class files for all the Java technology packages, which includes basic language classes, GUI component classes, and so on.


2. The other main deployment environment is on your web browser. Most commercial browsers supply a Java technology interpreter and runtime environment

A Development Environment

As a development environment, Java technology provides you with a large suite of tools:
  • A compiler (javac)‏
  • An interpreter (java)‏
  • A documentation generator (javadoc)‏
  • A class file packaging tooland so on...

What is Java Technology?

The Java technology is:
  • A programming language
  • A development environment
  • An application environment
  • A deployment environment


As a programming language, Java can create all kinds of applications that you could create using any conventional programming language.

Java Background

  • Java was created in 1991 by James Gosling et al. of Sun Microsystems.
    Initially called Oak, in honor of the tree outside Gosling's window, its name was changed to Java because there was already a language called Oak.
  • The original motivation for Java
    The need for platform independent language that could be embedded in various consumer electronic products.
  • One of the first projects developed using Java
    a personal hand-held remote control named Star 7.
    At about the same time, the World Wide Web and the Internet were gaining popularity. Gosling et. al. realized that Java could be used for Internet programming.

Introduction To Java

Objectives:


At the end of the post, the readers should be able to:

  • Describe the features of Java technology such as the Java virtual machine (JVM), garbage collection (GC) and code security
  • Describe the different phases of a Java program