Translate

Friday 15 June 2012

Variable declaration in Java

Following is the general form of a variable declaration:

type var-name

Here, type specifies the type of variable being declared, and var-name is the name of the variable. If you want to declare more than one variable of the specified type, you may use comma-separated list of variable names. Java defines several data types, including integer, character, and floating-point. 
Consider the following program.

class Example
{
    public static void main(String args[])
    {
         int num;          // This declares a variable called num
         num = 100;    // This assigns num the value 100
         System.out.println("This is num: " + num);
    }
}

When you run this program, you will see the following output:

This is num: 100

The first new line in the program is shown here:
int num;     //This declares a variable called num

This line declares an integer variable called num. Java requires that variables be declared before they are used. The keyword int specifies an integer type.
In the program, the line
num = 100;    //this assigns num the value 100
assigns to num the value 100. In java, the assignment operator is a single equal sign.

The next line of code outputs the value of num preceded by the string "This is num: "
System.out.println("This is num: " + num);
In this statement, the plus sign causes the value of num to be appended to the string that precedes it, and then  the resulting string is output. This approach can be generalized. Using the + operator you can string together as many items as you want within a single println() statement.



Tuesday 12 June 2012

Java's main() method

public static void main(String args[])
This line begins the main() method. All java applications begins execution by calling main(). The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. In this case, main() must be declared as public, since it must be called by the code outside of its class when the program is started. The keyword static allows main() to be called without having to instantiate a particular instance of the class. This is necessary since main() is called by Java interpreter before any objects are made. The keyword void simply tells the compiler that main() does not return a value. As stated, main() is the method called when a Java application begins. Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main() method. But Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. But the Java interpreter would report an error because it would be unable to find the main() method.
Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. In main(), there is only one parameter. String args[] declares a parameter named args, which is an array of instances of the class String. In this case, args receives any command-line arguments present when the program is executed.    

Sunday 10 June 2012

Types of Comments in Java

Consider the following sample program:

/*
This is a simple Java program.
Call this file "Example.java".
*/

class Example
{
    //Your program begins with a call to main().
    public static void main(String args[])
   {
      System.out.println("This is a simple Java program.");
   }
}

The program begins with the following lines:
/*
This is a simple Java program.
Call this file "Example.java".
*/

This is a comment. The contents of the comment are ignored by the compiler. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code. In real applications, comments generally explain how some part of the program works or what a specific feature does.
Java supports 3 types of the comments. The one shown at the top of the program is called multiline comment. This type of comment must begin with /* and end with */. Anything between these two comment symbols is ignored by the compiler. As the name suggests, a multiple comment may be several lines long.

The second type of the comment supported by java is the single line comment, shown here:

    //Your program begins with a call to main().

A single line comment begins with a // and ends at the end of the line. As a general rule, programmers use multiline comments for longer remarks and single line comments for brief, line by line descriptions.

The third type of comment is called a documentation comment. This type of comment is used to produce an HTML file that documents your program. The documentation comment begins with a /** and ends with a */. Here is an example of a documentation comment for a class:

/**
* This class draws a bar chart.
* @author author_name
* @version 3.2
*/





Saturday 9 June 2012

A simple Java program

/*
This is a simple Java program.
Call this file "Example.java".
*/

class Example
{
    //Your program begins with a call to main().
    public static void main(String args[])
   {
      System.out.println("This is a simple Java program.");
   }
}

Entering the program
For this example, the name of the source file should be Example.java. Let's see why.
In Java, a source file is officially called a compilation unit. It is a text file that contains one or more class definitions. The Java compiler requires that a source file use the .java filename extension. 

Compiling the Program
To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here:

C:\>javac Example.java

The javac compiler creates a file called Example.class that contains the bytecode version of the program. As we know, the Java bytecode is the intermediate representation of your program that contains instructions the Java interpreter will execute. Thus, the output of javac is not code that can be directly executed.

To actually run the program, you must use Java interpreter, called java. To do so, pass the class name Example as a command line argument, as shown here:

C:\>java Example

When the program is run, the following output is displayed:
This is a simple Java program

When Java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension. When you execute the Java interpreter as just shown, you are actually specifying the name of the class that you want the interpreter to execute. It will automatically search for a file by that name that has the .class extension. If it finds the file, it will execute the code contained in the specified class.

Friday 8 June 2012

Java's Magic: The Bytecode

The key that allows Java to solve both the security and the portability problems is that the output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode is a set of instructions designed to be executed by the Java run-time system known as Java Virtual Machine (JVM). That is, in its standard form, the JVM is an interpreter for bytecode. 
Translating a Java program into bytecode helps makes it easier to run a program in a wide variety of environments. The reason is straightforward: only the JVM needs to be implemented for each platform. Once the run-time package exists for a given system, any Java program can run on it. Although the details of JVM will differ from platform to platform, all interpret the same Java bytecode. 
The fact that a Java program is interpreted also helps to make it secure. Because the execution of every Java program is under the control of the JVM, the JVM can contain the program and prevent it from generating side effects outside of the system. 
Although Java was designed for interpretation, there is technically nothing about Java that prevents on-the-fly compilation of bytecode into native code. Along these lines, Sun supplies its Just In Time (JIT) compiler for bytecode, which is included in the Java 2 release. When JIT compiler is part of the JVM, it compiles bytecode into the executable code in real time, on a piece-by-piece, demand basis. It is not possible to compile an entire Java program into executable code all at once, because Java performs various run time checks that can be done only at run time. Instead, the JIT compiles code as it is needed, during execution.

The Creation of Java

Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first working version. This language was initially called "Oak" but was renamed "Java" in 1995. The primary motivation was the need for platform-independent (that is architecture neutral) language that could be used to create software to be embedded in various consumer electronic devices, such as microwave ovens and remote controls. As you can probably guess, many different types of CPUs are used as controllers. The trouble with C and C++ is that they are designed to be compiled for a specific target. Although it is possible to compile a C++ program for just about any type of CPU, to do so requires a full C++ compiler targeted for that CPU. The problem is that compilers are expensive and time-consuming to create. In an attempt to find such a solution, Gosling and others began work on a portable, platform-independent language that could be used to produce code that would run on a variety of CPUs under differing environments. This effort ultimately led to the creation of Java. Java derives much of its characters from C and C++. This is by intent. 

Thursday 7 June 2012

Signed Binary Numbers: Two's Complement Representation

If 1 is added to 1's complement of a binary number, the resulting number is known as the two's complement of the binary number. For example, 2's complement of 0101 is 1011. Since 0101 represents (+5)10, therefore, 1011 represents (-5)10 in 2's complement representation. In this representation also, if the MSB is 0 the number is positive, whereas if the MSB is 1 the number is negative.

Exercise
Find the 2's complement of the numbers
  1. 01001110
Solution:

    0 1 0 0 1 1 1 0................Number   

    1 0 1 1 0 0 0 1................1's complement          

                         1................Add 1  
    1 0 1 1 0 0 1 0

From the above example, we observe the following:
  1. If the LSB of the number is 1, its 2's complement is obtained by changing each 0 to 1 and 1 to 0 except the least-significant bit.
  2. If the LSB of the number is 0, its 2's complement is obtained by scanning the number from LSB to MSB bit by bit and retaining the bits as they are up to and including the occurrence of the first 1 and complement all other bits. 

Signed Binary Numbers: One's Complement Representation

In a binary number, if each 1 is replaced by 0 and each 0 by 1, the resulting number is known as the one's complement of the first number. In fact, both the numbers are complement of each other. If one of these numbers is positive, then the other number will be negative with the same magnitude. For example, (0101)2 represents (+5)10, whereas (1010)2 represents (-5)10 in this representation. This method is widely used for representing signed numbers. In this representation also, MSB is 0 for positive numbers and 1 for negative numbers. 

Exercise 1
Find the one's complement of the following binary numbers.

  1. 0100111001
  2. 11011010
Solution:

1) 1011000110
2) 00100101

Exercise 2
Represent the following numbers in one's complement form.
  1. +7 and -7
  2. +8 and -8
  3. +15 and -15
Solution:

1) (+7)10 = (0111)2 and (-7)10 = (1000)2
2) (+8)10 = (01000)2 and (-8)10 = (10111)2
3) (+15)10 = (01111)2 and (-15)10 = (10000)2


Wednesday 6 June 2012

Signed Binary Numbers: Sign-Magnitude Representation

In the decimal number system a plus (+) sign is used to denote a positive number and a minus (-) sign is used to denote a negative number. The plus sign is usually dropped, and the absence of any sign means that the number has positive value. This representation of numbers is known as signed number. Digital circuits can understand only two symbols, 0 and 1; therefore we must use the same symbols to indicate the sign of the number also. Normally, an additional bit is used as the sign bit and it is placed as the most significant bit. A 0 is used to represent a positive number and a 1 to represent a negative number. For example, an 8-bit signed number 01000100 represents a positive number and its value (magnitude) is (1000100)2 = (68)10. The left most 0 (MSB) indicates that the number is positive. On the other hand, in the signed binary form, 11000100 represents a negative number with magnitude (1000100)2 = (68)10. The 1 in the left most position (MSB) indicates that the number is negative and the other seven bits give its magnitude. This kind of representation for signed numbers is known as sign-magnitude representation. The user must take care to see the representation used while dealing with binary numbers.

Exercise
Find the decimal equivalent of the following binary numbers assuming sign-magnitude representation of the binary numbers.

  1. 101100
  2. 001000
  3. 0111
  4. 1111
Solution:

1) Sign bit is 1, which means the number is negative.
     Magnitude = 01100 = (12)10
      (101100)2 = (-12)10

2) Sign bit is 0, which means the number is positive.
    Magnitude = 01000 = 8
    (001000)2 = (+8)10

3) (0111)2 = (+7)10

4) (1111)2 = (-7)10

Binary Addition

The rules of binary addition are given in following table








In the first three rows above, there is no carry, that is, carry = 0, whereas in the fourth row a carry is produced (since the largest digit possible is 1), that is, carry = 1, and similar to decimal addition it is added to next higher binary position.

Example 
1) Add 1011 and 1100

Solution


             1      0      1      1
  (+)      1      1      0      0
     1      0      1      1      1
Carry






Following figure shows Binary addition along with its equivalent decimal addition.


From the above example, we observe the following:
  1. if the number of 1's to be added in a column is even then sum bit is 0, and if number of 1's to be added in a column is odd then the sum bit is 1.
  2. Every pair of 1's in a column produces a carry (1) to be added to the next higher bit column. 

Binary to Decimal and Decimal to Binary Conversion


Binary to Decimal conversion
Any binary number can be converted into its equivalent decimal number using the weights assigned to each bit position.

Decimal to Binary conversion
Any decimal number can be converted into its equivalent binary number. For integers, the conversions is obtained by continuous division by 2 and keeping track of the remainders, while for fractional parts, the conversion is affected by continuous multiplication by 2 and keeping track of the integers generated. The conversion process is illustrated by the following examples.


Binary Number System



The number system with base (or radix) two is known as the binary number system. Only two symbols are used to represent numbers in this system and these are 0 and 1. These are known as bits. This system has the minimum base (0 is not possible and 1 is not useful). It is a positioned system, that is every position is assigned a specific weight.
The following table illustrates counting in binary number system. The corresponding decimal numbers are given in the right hand column. Similar to decimal number system, the left-most bit is known as the most significant bit (MSB) and the right-most bit is known as the least significant bit (LSB). Any number of 0s can be added to the left of the number without changing the value of the number. In the binary number system, a group of four bits is known as nibble, and a group of eight bits is known as a byte

4-bit binary numbers and their corresponding decimal numbers

      Binary number                                      Decimal number    

 0         0        0        0                                       0         0
 0         0        0        1                                       0         1
 0         0        1        0                                       0         2
 0         0        1        1                                       0         3
 0         1        0        0                                       0         4  
 0         1        0        1                                       0         5
 0         1        1        0                                       0         6
 0         1        1        1                                       0         7 
 1         0        0        0                                       0         8
 1         0        0        1                                       0         9
 1         0        1        0                                       1         0
 1         0        1        1                                       1         1
 1         1        0        0                                       1         2
 1         1        0        1                                       1         3
 1         1        1        0                                       1         4 
 1         1        1        1                                       1         5

Sunday 3 June 2012

Boolean Algebra

The digital signals are discrete in nature and can only assume one of the two values 0 or 1. A number system based on these two digits is known as binary number system. In the middle of 19th century, an English mathematician George Boole developed rules for manipulations of binary variables, known as Boolean Algebra. This is the basis of all digital systems like computers, calculators, etc.

Binary variables can be represented by a letter symbol such as A, B, X, Y, ... The variable can have only one of the two possible values at any time, viz. 0 or 1. The Boolean algebraic theorems are shown in following table. 



Theorem No. Theorem
1) A + 0 = A
2) A . 1 = A
3) A + 1 = 1
4) A . 0 = 0
5) A + A = A
6) A . A = A
7) A + ~A = 1
8) A . ~A = 0
9) A . (B + C) = AB + AC
10) A + BC = (A + B) (A + C)
11) A + AB = A
12) A(A + B) = A
13) A + ~AB = (A + B)
14) A(~A + B) = AB
15) AB + A~B = A
16) (A + B). (A + ~B) = A
17) AB + ~AC = (A + C) (~A + B)
18) (A + B)(~A + C) = AC + ~AB
19) AB + ~AC + BC = AB + ~AC
20) (A + B)(~A + C)(B + C) = (A + B)(~A + C)
21) ~(A . B . C...) = ~A + ~B + ~C + ...
22) ~(A + B + C...) = ~A . ~B . ~C...

Theorems 1 to 8 involve a single variable only. Each of these theorems can be proved by considering every possible value of the variable. For example, in theorem 1,
if A = 0 then 0 + 0 = 0 = A
and if A = 1 then 1 + 0 = 1 = A
and hence the theorem is proved.

Theorems 9 to 20 involve more than one variable and can be proved by making a truth table. For example, Theorem 10 can be proved by making the truth table given below.

Truth table to prove Theorem 10

   A     B     C     BC     A + BC     A + B      A + C      (A+B) (A+C)   
0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0
0 1 0 0 0 1 0 0
0 1 1 1 1 1 1 1
1 0 0 0 1 1 1 1
1 0 1 0 1 1 1 1
1 1 0 0 1 1 1 1
1 1 1 1 1 1 1 1

For each combination, the value of A + BC is same as that of (A+B) (A+C), which proves the theorem.
Theorem 21 and 22 are known as De Morgan's theorems.

Exclusive-OR Operation

The Exclusive-OR (EX-OR) operation is widely used in digital circuits. It is not a basic operation and can be performed using the basic gates- AND, OR and NOT or universal gates NAND or NOR. Because of its importance, the standard symbol shown below is used for this operation.


Logical equation of EX-OR gate is written as

Y = A EX-OR B

From the truth table we observe that when both inputs are same (0 or 1) the output is 0, whereas when the inputs are not same (one of them is 0 and the other one is 1) the output is 1.