Tuesday, June 21, 2011

Program to find Prime Numbers till 100

What is a Prime Number ?
If it is bigger than one and has no divisors other than 1 and itself.


==========================primenumbers.java=======================
public class primenumbers {
   
    /**
    * Calculating Prime Numbers
    *
    */
    public static void main(String[] args) {

    int num1, num2, modulo = 0;
    int maxPrimeNumber = 100;

    for (num1 = 1; num1 < maxPrimeNumber; num1++) {

    label:for (num2 = 2; num2 < num1; num2++) {

    modulo = num1 % num2;

    if (modulo == 0)
    break label;
    }

    if (num1 == num2)
    System.out.print(num1 + " : ");
    }

    }

    }
====================================================


Output:
2 : 3 : 5 : 7 : 11 : 13 : 17 : 19 : 23 : 29 : 31 : 37 : 41 : 43 : 47 : 53 : 59 : 61 : 67 : 71 : 73 : 79 : 83 : 89 : 97 :


Monday, June 20, 2011

Java Cast assigning Double to Init.

Whats is as Cast ?
A cast is an explicit conversion.

In the below program we convert values from double to int.

===conversiontypes.java======
public class converisontypes {
    public static void main (String[] args) {
        double  x,y;
        x=20;
        y=10;
        int z;
       
       
        z = (int) (x/y);
       
        System.out.println("The Value of Z is " +z);
       
       
       
       
    }

}
========================
result: 

The Value of Z is 2


Thursday, June 16, 2011

Change of Variables inside different Block

Here in below program Block1 initializes   z values as 20 and Block2 initializes z values as 30.

public class Scopevariable {
public static void main(String[] args) {
    int x,y;
    x=10;
    y=20;
    if(x==10){
        int z=20;
        System.out.println("The Value of z inside Block 1 is " +z );
    }
    if(x==10){
        int z=30;
        System.out.println("The value of z inside Block 2 is " +z );
    }
}
   
}

Tuesday, June 14, 2011

Integers and values they support.


Java defines four integer types: byte, short, int, and long, which are shown here:

Type Width in Bits Range

byte 8 –128 to 127
short 16 –32,768 to 32,767
int 32 –2,147,483,648 to 2,147,483,647
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

As the table shows, all of the integer types are signed positive and negative values.
Java does not support unsigned (positive-only) integers.
Many other computer languages support both signed and unsigned integers.
However, Java’s designers felt that unsigned integers were unnecessary.


for loop with iteration example

public class appleprice {
    public static void main(String[] args) {
        int apple,cost,price;
        cost=10;
       
        /* using { after for statement will use block and execute iterations if you remove block it wont show iterations */

        for (apple=1; apple <10; apple++) {
            price = cost * apple;
            System.out.println("price of  " + apple + "   apple is  " +price);
        }
           
           
    }

}

Sunday, June 12, 2011

A Sample Java Program and description :

class Example{
public static void main(String...args){
S.o.p("HOW");
}
}

[S.o.p = System.out.println]



class Example {
This line uses the keyword class to declare that a new class is being defined.
The class is Java’s basic unit of encapsulation.

public static void main (String args[]) {
The method is public because it be accessible to the JVM to begin execution of the program.

It is Static because it be available for execution without an object instance. you may know that you need an object instance to invoke any method. So you cannot begin execution of a class without its object if the main method was not static.

It returns only a void because, once the main method execution is over, the program terminates. So there can be no data that can be returned by the Main method

The last parameter is String args[]. This is used to signify that the user may opt to enter parameters to the java program at command line. We can use both String[] args or String args[]. The Java compiler would accept both forms.








Javas Magic "The Bytecode"

                                  Java Program (eg class files)
                                                       |
                                                       |
JVM (Java Virtual Machine) part for JRE (Java Runtime Environment)
                                                       |
                                                       |
 JVM converts the Java Program to Bytecodes and talks to the machine    

Bytecodes are the machine language of the Java virtual machine. When a JVM loads a class file, it gets one stream of bytecodes for each method in the class


*Java Bytecode is Highly optimised for speed of execution.


$javac testfile.java

This will create a testfile.class which will have java bytecode version of the program.

To actually run the program 

You run,
$java testfile (class file name).






 Also read for more info:

http://www.javaworld.com/javaworld/jw-09-1996/jw-09-bytecodes.html




JAVA FAQ's

1) How Java Relates to C and C++

Java is directly related to both C and C++. Java inherits its syntax from C. Its object model is adapted from C++.


Second, Java’s designers did not “reinvent the wheel.” Instead, they further refined an already highly successful programming paradigm. The modern age of programming began with C. It moved to C++, and now to Java.

This makes it easy for a C/C++ programmer to learn Java and, conversely, for a Java programmer to learn C/C++.


2) What are  Java Applets
An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible Web browser. Furthermore, an applet is downloaded on demand.

3) Java Security :

Java confines a Java program to the Java execution environment and not allowing it access to other parts of the computer.

Frankly, the ability to download applets with confidence that no harm will be done to the client computer is the single most important aspect of Java.

4)   To address the issues of portability and security, why was it necessary to create a new computer language such as Java; couldn’t a language like C++ be adapted? In other words, couldn’t a C++ compiler that outputs bytecode be created?

While it would be possible for a C++ compiler to generate bytecode rather than
executable code, C++ has features that discourage its use for the creation of applets—the most important feature being C++’s support for pointers. A pointer is the address of some object stored in memory. Using a pointer, it would be possible to access resources outside the program itself, resulting in a security breach. Java does not support pointers,thus eliminating this problem.