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 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 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 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
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
No comments:
Post a Comment