Showing posts with label JAVA Basics Part 1. Show all posts
Showing posts with label JAVA Basics Part 1. Show all posts

Monday, July 2, 2012

JAVA Basics 4: Java Command Line Arguments

This class demonstrates how command line arguments are passed in Java. Arguments are passed as a String array to the main method of a class. The first element (element 0) is the first argument passed not the name of the class.

Source Code
An example that prints in the command line arguments passed into the class when executed.
public class ReadArgs
{
       public static final void main(String args[])
       {
                for (int i=0;i<args.length;++i)
                {
                         System.out.println( args[i] );
                }
        }


Sample Run
With the following command line, the output shown is produced.

java ReadArgs zero one two three


Output:

The following command line arguments were passed:
arg[0]: zero
arg[1]: one
arg[2]: two
arg[3]: three

JAVA Basics 3:Java Data and Variables

There are 8 primitive data types. he 8 primitive data types are numeric types. The names of the eight primitive data types are:
byte short int long float double char boolean
There are both integer and floating point primitive types. Integer types have no fractional part; floating point types have a fractional part. On paper, integers have no decimal point, and floating point types do. But in main memory, there are no decimal points: even floating point values are represented with bit patterns. There is a fundamental difference between the method used to represent integers and the method used to represent floating point numbers. 
Integer Primitive Data Types
Type Size Range
byte 8 bits -128 to +127
short 16 bits -32,768 to +32,767
int 32 bits (about)-2 billion to +2 billion
long 64 bits (about)-10E18 to +10E18
Floating Point Primitive Data Types
Type Size Range
float 32 bits -3.4E+38 to +3.4E+38
double 64 bits -1.7E+308 to 1.7E+308
Examples
int yr = 2006;
double rats = 8912 ;
         For each primitive type, there is a corresponding wrapper class. A wrapper class can be used to convert a primitive data value into an object, and some type of objects into primitive data. The table shows primitive types and their wrapper classes:
primitive type Wrapper type
byte Byte
short Short
int Int
long Long
float Float
double Double
char Character
boolean Boolean
 Variables only exist within the structure in which they are defined. For example, if a variable is created within a method, it cannot be accessed outside the method. In addition, a different method can create a variable of the same name which will not conflict with the other variable. A java variable can be thought of as a little box made up of one or more bytes that can hold a value of a particular data type:
Syntax: variabletype variablename = data;
Source Code ( demonstrating declaration of a variable )
class example
{
  public static void main ( String[] args )
  {
    long x = 123;    //a declaration of a variable named x with a datatype of long

    System.out.println("The variable x has: " + x );
  }
}
Source Code
public class MaxDemo {
     public static void main(String args[]) {
     //integers
          byte largestByte = Byte.MAX_VALUE;
          short largestShort = Short.MAX_VALUE;
          int largestInteger = Integer.MAX_VALUE;
          long largestLong = Long.MAX_VALUE;

         //real numbers
         float largestFloat = Float.MAX_VALUE;
         double largestDouble = Double.MAX_VALUE;

         //other primitive types
        char aChar = 'S';
        boolean aBoolean = true;

        //Display them all.
        System.out.println("largest byte value is " + largestByte + ".");
        System.out.println("largest short value is " + largestShort + ".");
        System.out.println("largest integer value is " + largestInteger + ".");
        System.out.println("largest long value is " + largestLong + ".");
        System.out.println("largest float value is " + largestFloat + ".");
        System.out.println("largest double value is " + largestDouble + ".");
   }
}


Sample Run
The largest byte value is 127.
The largest short value is 32767.
The largest integer value is 2147483647.
The largest long value is 9223372036854775807.
The largest float value is 3.4028235E38.
The largest double value is 1.7976931348623157E308.

Java Basics 2: Java Comments

The Java programming language supports three kinds of comments:

/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.
// text

The compiler ignores everything from // to the end of the line.
Example
Java denotes comments in three ways:

1. Double slashes in front of a single line comment:

int i=5; // Set the integer to 5

2. Matching slash-asterisk (/*) and asterisk-slash (*/) to bracket multi-line comments:

/*
Set the integer to 5
*/
int i=5;

3. Matching slash-double asterisk (/**) & asterisk-slash(*/) for Javadoc automatic hypertext documentation, as in

/**
This applet tests graphics.
*/
public class testApplet extends applet{...

or

/**
* Asterisks inside the comment are ignored by javadoc so they
* can be used to make nice line markers.
**/

The SDK tool javadoc uses the latter /** ..*/ comment style when it produces hypertext pages to describe a class.

Java Basics 1: Hello World

Our first application will be extremely simple - the obligatory "Hello World". The following is the Hello World Application as written in Java. Type it into a text file or copy it out of your web browser, and save it as a file named Hello World.java. This program demonstrates the text output function of the Java programming language by displaying the message "Hello world!". Java compilers expect the file name to match the class name.

A java program is defined by a public class that takes the form:
 public class program-name {
            
                optional variable declarations and methods
                
                public static void main(String[] args) {
                   statements
                }
                
                optional variable declarations and methods
          
            }

Source Code
In your favorite editor, create a file called HelloWorld.java with the following contents:
/** Comment
 * Displays "Hello World!" to the standard output.
 */

class HelloWorld {

  public static void main (String args[]) {

    System.out.println("Hello World!");   //Displays the enclosed String on the Screen Console

  }

}


To compile Java code, we need to use the 'javac' tool. From a command line, the command to compile this program is:

javac HelloWorld.java

For this to work, the javac must be in your shell's path or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code.
You're ready to run your first Java application. To run the program, you just run it with the java command:

java HelloWorld

Sample Run

Hello world!
The source file above should be saved as myfirstjavaprog.java, using any standard text editor capable of saving as ASCII (eg - Notepad, Vi). As an alternative, you can download the source for this tutorial.

HelloWorld.java


Note: It is important to note that you use the full name with extension when compiling (javac HelloWorld.java) but only the class name when running (java HelloWorld).