analytics

Wednesday, October 14, 2009

Language Fundamentals




Class declaration and java source file

Only "one" top-level public class is allowed per java source file.
The name of the java source file and the name of the top-level public class MUST be same.
If there is no (not a single one) top-level public class present in a java source file, then the java source file's name can be anything.After compilation of such file, class files for all top-level classes are produced.


Declaration rules for package and import statements

The Java source file is structured to have three types of top-level declarations, the package declaration, import statements and class declarations. None of them has to be present in the source file. However, if they are present, theymust appear in the following order:

§ The package declaration must come first…

§ …followed by the import statements

§ …followed by the class definitions.



We know that a java source file can have multiple top-level classes. The source file in the listing has two top-level classes, Test andHelper. We also know that there can be only one public top-level class. In the listing, it is the Testclass. Since this source file has a top-level public class, the name of the source file and the name of the top-level public class must be the same. Therefore, the listing 8.1 must reside in file namedTest.java. In Java, a compilation of any source file creates a .class file for every class declared in it. Since Test.javahas two class declarations, two class files will be created (Test.class andHelper.class) after its compilation.

The package declaration inTest.java is applicable to both the classes Test and Helper. In other words, they both belong to the same package. There are also two import statements in the listing. The first import statement imports thejava.util.Date class. Therefore both the classes, Testand Helper can refer tojava.util.Date as simplyDate. The second import statement imports the entirejava.io package. Therefore, both of them can reference anyclass from java.iopackage by specifying only its name instead of the fully qualified name.

Now you know how a java source file is structured and the simple rules enforcing that structure. But, defining class definitions in a java source file does not make it executable after compilation. You cannot run the Test or Helperclasses as Java application unless they have a special main method defined.



Primitive data types and their ranges

Primitives are Java’s built-in data types, which mean they are directly understood by the compiler. It means you do not use any class to create (instantiate) them or modify them. Primitives are created directly by specifying a data value; you need not use the new keyword to create them. Primitive types can be broadly classified as the numeric data types and the boolean data type.


The numeric primitives can be either signed or unsigned. The signed primitives are further classified as integral primitives and floating-point primitives. The primitivesbyte, short, int andlong represent whole numbers and hence are called integrals whereas the primitives float anddouble represent fractional numbers and hence are called floating-point primitives. The primitive char is used to represent characters. It represents them in Unicode encoding as an unsigned numerical value. Therefore, it can also be used as an unsigned datatype. The primitive booleanis used to represent boolean values.


The char and String are two unrelated data types in Java. Thechar is a primitive datatype whereas the String is a class type. The string objectscan be created with the new keyword whereas char is not an object but a primitive type whose value can be specified by typing it in the code.


The variables of primitive datatypes hold either numeric or boolean values. The range of values a particular primitive variable holds depends on its type. Java specifies how many bits are required to represent the value of each primitive type. For instance, a primitive variable of type int is stored using 32 bits. Table 7.3 shows all the primitive types and the number of bits Java use to represent them.


Table 7.3 Primitive types and their bit representation


Primitive data types

Bit used for representation

boolean

1

byte

8

short

16

char

16

int

32

float

32

long

64

double

64



Arrays

An array object represents a fixed-sized ordered grouping of data elements of the same type. It can hold primitive values or object references as its data elements. These data elements are accessible by the index. An array is ahomogeneous collection, we means it can hold only similar kind-of objects. For instance, an array ofint values cannot hold strings objects. The array itself is always an object; it does not matter whether it is going to hold primitive elements or references to objects.

Arrays Declaration, Construction and initialization.

Arrays are somewhat special objects in java as array construction and declaration is a little bit different.
An array is a fixed-sized ordered collection of homogeneous data elements.

Following example shows array declaration at compile time and array construction at runtime.
 int[] ints;          // array declaration  ints = new ints[25]; // array construction at runtime. 

Now let us see how an array is declared, constructed and initialized at the same time.
 int[] ints = {1,2,3,4,5}; // array declaration, construction and initialization at the same time.

Array of primitive data types

An array of primitive data type created using the new keyword is initialized. Each array element is initialized to its default value.
For example,
   char[] arrayOfChars = new char[10]; 
An array of 10 chars will be created with each element initialized to '\u0000'. The default value for char is '\u0000'.

Array of object references

An array of object references created using the new keyword is also initialized. Each array element is initialized to its default value, i.e. null.
 String[] names = new String[10]; 
An array of 10 string references will be created with each element initialized to null.

Array index

Please note that array index starts at 0. So array of 10 elements has element at 0th index to 9th index.
 String[] names = new String[10]; 
Therefore, names[0] to names[9] will have value null. names[10] does not exist and hence it is undefined.

Please note that length is a property of array (and not a method).
 int[] ints = {1,2,3,4,5};
 System.out.println("Length of array ints is " + ints.length); 
As java.lang.Object is the superclass of an array, arrays understand all method calls of java.lang.Object class.


Argument passing during method calls.

main() method.

main() is the entry point of any java application.

Following are some valid main() method declarations.

public static void main(String[] args) {}
static public void main(String[] args) {}
public static void main(String args[]) {}

main() method takes String array as argument. The java runtime system can pass command line parameters to
the application through this array.

The first command line parameter is passed as the first argument and is referenced by args[0].
For example,
 java MyCalculator 12 50 add 
args[0] will be 12, args[1] will be 50 and so on.


The main() method

Every .class file has a definition of one Java class. Usually you use another Java class by instantiating it and calling its methods from your class. You can do in your Java code as long as you can access its .class file. However, you cannot instantiate a Java class from the command line or from outside the JVM. Then how will you start the execution of any Java code? As a starting point, you need the ability to call the Java code from the command line. Java provides you with a special method called as the main method. This method can be called directly from the command line. The JVM calls this method whenever you try to execute a Java .class file from command-line. For instance, you canexecute a Java class file Test.class as:

$ java Test

The java in the above command is a call to the Java interpreter; it first searches forTest.class file. Then it searches the main method in that class file and if present, executes it.

In this method, you can either create instances of other Java classes and call their methods or do whatever your application needs to do. Thus, it essentially provides you with an entry point from where you can start executing your Java application. That is the reason why if this particular method is present in your .class file, it becomes the Java Application instead of a mere Java class file.


No comments:

Post a Comment