Arrays and Overloading in Java by Deepak Chandrasekaran SignUp
Boloji.com
Boloji
Home Kabir Poetry Blogs BoloKids Writers Contribute Search Contact Site Map Advertise RSS Login Register
Boloji
Channels

In Focus

Analysis
Cartoons
Education
Environment
Going Inner
Opinion
Photo Essays

Columns

A Bystander's Diary
Business
My Word
PlainSpeak
Random Thoughts

Our Heritage

Architecture
Astrology
Ayurveda
Buddhism
Cinema
Culture
Dances
Festivals
Hinduism
History
People
Places
Sikhism
Spirituality
Vastu
Vithika

Society & Lifestyle

Family Matters
Health
Parenting
Perspective
Recipes
Society
Teens
Women

Creative Writings

Book Reviews
Ghalib's Corner
Humor
Individuality
Literary Shelf
Love Letters
Memoirs
Musings
Quotes
Ramblings
Stories
Travelogues
Workshop

Computing

CC++
Computing Articles
Flash
Internet Security
Java
Linux
Networking
Java Share This Page
Arrays and Overloading in Java
by Deepak Chandrasekaran Bookmark and Share
 

In the last article, we dealt with strings, variable initializations and castings. This week lets have a look at the array feature of java and also on a special feature called Operator Overloading. 

Arrays in Java

Java has a built in array class which you can use to hold multiple values provided those values are of the same data type.
In Java, arrays are much more restrictive. For example, you may not change the size of an array once you have created it. To add elements to an array dynamically you actually have to create a new, larger array and copy the old array into the new one using array Copy() in the java.lang.System class.
For a dynamically resizable data structure, look to the Vector class in the java.utilpackage.
However, for cases in which you do not need to dynamically resize your data structure, arrays work great. Java provides a zero-based array, which is defined much as variables are. You first declare what type of data will be stored in an array, give the array a name, and then define how large it is. Consider the following example: 

int[] intArray = new int[40];

This array would hold 40 ints numbered from 0-39.
 
Filling and extracting values from an array is the same process as it was for Perl.
Consider the following example:
 
int[] intArray = new int[40];
for (int i = 0; i < 40; i++)
{
intArray[i] = i;
}
for (int i = 0; i < 40; i++)
{
System.out.println("Value: " + intArray[i]);
}
 
Java also allows you to define an array at time of initialization such as in the following example:
 
int[] intArray = {0,1,2,3,4};
Operator Overloading

Another cool feature of Java is that methods in Java can easily be overloaded such that the same method name can be used for several different implementations of the same action. For example printNumber(), printLetter(), printImage() are the various methods that can be used for several different implementations of the same action.
The only requirement for overloading is that each version of the method takes a different set of parameters as arguments (sometimes, we say that it has a "different signature") such as in the following example: 

int add(int a, int b)
{
return (a+b);
}

float add(float a, float b)
{
return (a+b);
}
 
Finally, it is worth noting that since methods belong to specific classes, it is fine for you to have the same method name with the same arguments in different classes. Thus, the Button class might have a setLabel(String s) method and a Label might have a setLabel(String s) method and neither will conflict since each method is specific to a specific class.   

7-Feb-2002
More by :  Deepak Chandrasekaran
 
Views: 1810
Article Comment very nice
Reena Baliyan
06/20/2011
Share This Page
Post a Comment
Bookmark and Share
Name*
Email ID*  (will not be published)
Comment
Verification Code*
N5L55
Please fill the above code for verification.

    

 
 
Top | Java



Solitude and other poems by Rajender Krishan
 


    A Bystander's Diary     Analysis     Architecture     Astrology     Ayurveda     Book Reviews
    Buddhism     Business     Cartoons     CC++     Cinema     Computing Articles
    Culture     Dances     Education     Environment     Family Matters     Festivals
    Flash     Ghalib's Corner     Going Inner     Health     Hinduism     History
    Humor     Individuality     Internet Security     Java     Linux     Literary Shelf
    Love Letters     Memoirs     Musings     My Word     Networking     Opinion
    Parenting     People     Perspective     Photo Essays     Places     PlainSpeak
    Quotes     Ramblings     Random Thoughts     Recipes     Sikhism     Society
    Spirituality     Stories     Teens     Travelogues     Vastu     Vithika
    Women     Workshop
RSS Feed RSS Feed Home | Privacy Policy | Disclaimer | Site Map
No part of this Internet site may be reproduced without prior written permission of the copyright holder.
Developed and Programmed by ekant solutions