|
|
|
| Author Details |
|---|
| All Tutorials by whizlabs |
1 2 [Next] [Last Page]
|
|---|
Java Arrays
Arrays are dynamically created objects in Java code. An array can hold a number of variables of the same type. The variables can be primitives or object references; an array can even contain other arrays.
Arrays are dynamically created objects in Java code. An array can hold a number of variables of the same type. The variables can be primitives or object references; an array can even contain other arrays.
Declaring array variables
When we declare an array variable, the code creates a variable that can hold the reference to an array object. It does not create the array object or allocate space for array elements. It is illegal to specify the size of an array during declaration. The square brackets may appear as part of the type at the beginning of the declaration or as part of the array identifier:
| | Hits:100 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|---|
Declaring classes, variables, and methods
Now let's look at ways we can modify classes, methods, and variables. There are two kinds of modifiers -- access modifiers and non-access modifiers. The access modifiers allow us to restrict access or provide more access to our code.
Declaring classes, variables, and methods page 2 of 5
Now let's look at ways we can modify classes, methods, and variables. There are two kinds of modifiers -- access modifiers and non-access modifiers. The access modifiers allow us to restrict access or provide more access to our code.
Class modifiers
The access modifiers available are public, private, and protected. However, a top-level class can have only public and default access levels. If no access modifier is specified, the class will have default access. Only classes within the same package can see a class with default access. When a class is declared as public, all the classes from other packages can access it.
Let's see the effect of some non-access modifiers on classes. The final keyword (see Java keywords and identifiers for more on keywords) does not allow the class to be extended. An abstract class cannot be instantiated, but can be extended by subclasses:
| | Hits:200 Rate: 5.0(out of 5) Vote:1 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|---|
Constructors
A constructor is used when creating an object from a class. The constructor name must match the name of the class and must not have a return type. They can be overloaded, but they are not inherited by subclasses.
A constructor is used when creating an object from a class. The constructor name must match the name of the class and must not have a return type. They can be overloaded, but they are not inherited by subclasses.
Invoking constructors
A constructor can be invoked only from other constructors. To invoke a constructor in the same class, invoke the this() function with matching arguments. To invoke a constructor in the superclass, invoke the super() function with matching arguments. When a subclass object is created, all the superclass constructors are invoked in the order starting from the top of the hierarchy.
| | Hits:89 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|---|
Flow control statements
The flow control statements allow you to conditionally execute statements, to repeatedly execute a block of statements, or to just change the sequential flow of control.
The flow control statements allow you to conditionally execute statements, to repeatedly execute a block of statements, or to just change the sequential flow of control.
if/else statement
The if/else statement is used for decision-making -- that is, it decides which course of action needs to be taken. The syntax is:
| | Hits:80 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|---|
Assertions
An assertion is a statement containing a boolean expression that is assumed to be true when the statement is executed. The system reports an AssertionError if the expression evaluates to false.
| | Hits:86 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|---|
Exception handling
Exceptions are Java objects; exception classes are derived from java.lang.Throwable.
| | Hits:285 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java |
|
|
|---|
Behavior of the garbage collector
A Java programmer does not have to worry about memory management, because it is automatically taken care of by the garbage collector. The Java virtual machine (JVM) decides when to run the garbage collector. The garbage collector is a low priority thread that runs periodically, releasing memory used by objects that are not needed anymore.
Behavior of the garbage collector page 1 of 5
A Java programmer does not have to worry about memory management, because it is automatically taken care of by the garbage collector. The Java virtual machine (JVM) decides when to run the garbage collector. The garbage collector is a low priority thread that runs periodically, releasing memory used by objects that are not needed anymore.
The garbage collection (GC) algorithm varies from one JVM to another. There are different algorithms being used, like reference counting or the mark and sweep algorithm. See Resources for more information on garbage collection.
Running the
| | Hits:78 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|---|
Eligibility for garbage collection
An object is eligible for garbage collection when no live thread can access it. An object can become eligible for garbage collection in different ways.
Eligibility for garbage collection page 2 of 5
An object is eligible for garbage collection when no live thread can access it.
An object can become eligible for garbage collection in different ways:
If the reference variable that refers to the object is set to null, the object becomes eligible for garbage collection, provided that no other reference is referring to it.
If the reference variable that refers to the object is made to refer to some other object, the object becomes eligible for garbage collection, provided that no other reference is referring to it.
Objects created locally in a method are eligible for garbage collection when the method returns, unless they are exported out of the method (that is, returned or thrown as an exception).
Objects that refer to each other can still be eligible for garbage collection if no live thread can access either of them.
Consider the following example:
| | Hits:73 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|---|
Finalization
Java technology allows you to use the finalize() method to do the necessary cleanup before the garbage collector removes the object from memory.
Finalization page 3 of 5
Java technology allows you to use the finalize() method to do the necessary cleanup before the garbage collector removes the object from memory. This method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. It is defined in the Object class, thus it is inherited by all classes. A subclass overrides the finalize() method to dispose of system resources or to perform other cleanup:
| | Hits:93 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|---|
Package and class declarations
A package represents a group of classes.
A package represents a group of classes. A package statement should be the first valid statement in the source file. If there is no package statement, the classes in the source file belong to the default unnamed package, or else they belong to the named package. Only one package statement is allowed in a source file.
In a source file, there can be only one public class, and the name of the file should match that of the class. An import statement allows you to use a class directly instead of referring to it using its fully qualified name. Import statements must come after any package statement and before the class declarations, as shown below:
| | Hits:84 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-20 Rate It | Error | Review | | | Category: Home > Java > General Java |
|
|
|
|
|
|
|
|