Variables and Strings by Deepak Chandrasekaran
 Home | Kabir | Poetry | Blogs | BoloKids | Writers | Contribute | Search | Contact | Site Map | Advertise | RSS | Login | Register
 Channels

Analysis
Cartoons
Education
Environment
Going Inner
Opinion
Photo Essays

## Columns

A Bystander's Diary
My Word
PlainSpeak
Random Thoughts

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

Family Matters
Health
Parenting
Perspective
Recipes
Society
Teens
Women

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
Variables and Strings
by Deepak Chandrasekaran

In the last article we saw how to assigning values to variables and their respective ranges. Lets try to learn more on variables and strings. Use of variables is a key to efficient coding in any programming language. Variable declaration , if not done properly, can lead to many debugging errors.

Variable Assignment and Initialization

Once you have declared the type of a variable, you are free to initialize it and assign to it some value.
Assignment and initialization works just as they did in Perl. You simply use variable name = some value. For example, consider the following code:

int marks;
marks = 66;

Of course you can also declare variables and assign values to them at the same time using the following syntax:

int marks = 66;

Casting (Changing from one type to another)

What if you want to multiply 3 x 1.5,Or say, int x double? Will the result be an int or a double or something else?

Well, when in doubt, Java will convert to the less restrictive type to be safe. Thus, in the above example, the result will be a double since double is less restrictive.
The following chart shows how types will be caste if possible.

byte --> short --> int --> long --> float     --> double

However, what if you are going the other way? Suppose you have two doubles and you want to make an int out of the product

To do this type of caste, you simply perform an assignment using the type to be casted to in parentheses before the value to be casted. Consider the following example in which we caste from a double to an int:

double d = 345.456;
int    i = (int) d;

In this case, it will be assigned a value of "345".

Strings:

In Java, there is no variable type called string. However, although Java does not have a built-in String type, it does provide you will a predefined class called String which you can use instead.

I'll talk more about classes and using them in later articles. However, it would be nice to at least introduce the String class here so we can use it in the meantime.

To instantiate a String, you simply use the same syntax that you would if it were a type:

String s = "Hello Learner";

Strings have quite a few methods which allow you to manipulate them in every way that you could in Perl. To read about these methods, simply use the online documentation on java found on the SUN site.

In the later weeks, I will write in more about the intricacies that form an integral structure of the JAVA language. Currently I am doing a project with JAVA as the Front End and I learnt, the more you work in any programming language, the more you learn.

7-Feb-2002
More by :  Deepak Chandrasekaran

Views: 1546

Top | Java

 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