Java

INTRODUCTION TO JAVA

OBJECTIVE

To understand Java Environment and Java Data Types.


JAVA

Java is a set of several computer software products and specifications from Sun Microsystems (which has since merged with Oracle Corporation), that together provide a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones on the low end, to enterprise servers and supercomputers on the high end. While less common, Java applets are sometimes used to provide improved and secure functions while browsing the World Wide Web on desktop computers. Java is object oriented programming language. OOP is a programming methodology that helps organize complex programs through the use of inheritance, encapsulation, and Polymorphism. Current version of java is Java SE 7 Update 25, Code named Dolphin and released on July 28, 2011.

A First Simple Program


Let’s start by compiling and running the short sample program shown here. As you will see, this involves a little more work than you might imagine.

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

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

The first thing that you must learn about Java is that the name you give to a source file is very important. For this example, the name of the source file should be same as you give to class. Here, file name of this source code is Example.java.

Compiling the Program

javac Example.java
 
Running the Program
java Example

Output

 




Explanation

·         Open notepad -> Type Code ->  Save your source file with extension .java to the folder “C:\jdk1.6\bin”

  • multiline comment.  This type of comment must begin with /* and end with */.
  • single-line comment  // Your program begins with a call to main().

  • class Example {
This line uses the keyword class to declare that a new class is being defined. Example
is an identifier that is the name of the class.

  • public static void main(String args[]) {
This line begins the main( ) method. As the comment preceding it suggests, this is the
line at which the program will begin executing. All Java applications begin execution
by calling main( ).

·         The keyword void simply tells the compiler that main( ) does not return a   value.

  • Keep in mind that Java is case-sensitive

Format Specifiers

The general syntax of a format specifier is
%[flags][width][.precision][argsize]typechar

 
JAVA Data Types

Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float, double, and boolean. These can be put in four groups:

v  Integers This group includes byte, short, int, and long, which are for whole valued signed numbers.

v  Floating-point numbers This group includes float and double, which represent numbers with fractional precision.

v  Characters This group includes char, which represents symbols in a character set, like letters and numbers.

v  Boolean This group includes boolean, which is a special type for representing true/false values.

Integers


long
 
long is a signed 64-bit type and is useful for those occasions where an int type is not
large enough to hold the desired value. Here is a program that computes the number of miles that light will travel in a specified number of days
 
// Compute distance light travels using long variables.
public class Light
{
    public static void main(String args[])
   {
         int lightspeed;
         long days;
         long seconds;
         long distance;
// approximate speed of light in miles per second
         lightspeed = 186000;
        days = 1000; // specify number of days here
        seconds = days * 24 * 60 * 60; // convert to seconds
        distance = lightspeed * seconds; // compute distance

       System.out.print("In " + days);
       System.out.print(" days light will travel about ");
       System.out.println(distance + " miles.");
   }
}
 
Output


 
 
 
Floating-Point Types
                



 
 
 
double
Here is a short program that uses double variables to compute the area of a circle:
// Compute the area of a circle.
public class Area
{
     public static void main(String args[])
    {
         double pi, r, a;
         r = 10.8; // radius of circle
      pi = 3.1416; // pi, approximately
      a = pi * r * r; // compute area
      System.out.println("Area of circle is " + a);
    }
}
 
Characters
In Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars.


Booleans

Java has a simple type, called boolean, for logical values. It can have only one of two
possible values, true or false.
Here is a program that demonstrates the boolean type:
 
// Demonstrate boolean values.
public class BoolTest
{
     public static void main(String args[])
    {
        boolean b;
        b = false;
        System.out.println("b is " + b);
        b = true;
        System.out.println("b is " + b);
// a boolean value can control the if statement
        if(b)
            System.out.println("This is executed.");
        b = false;
        if(b)
            System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
       System.out.println("10 > 9 is " + (10 > 9));
    }
}

Output











No comments:

Post a Comment