Home | Hindi | Kabir | Poetry | Workshop | BoloKids | Writers | Contribute | Search | Contact | Share This Page!                      Shop Online

  News
Channels
In Focus

Analysis  
Bolography  
Cartoons
Environment   
Opinion 

Columns
 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
Jagoji
Literary Shelf 
Love Letters  
Memoirs
Musings
Ramblings
Stories
Travelogues

Computing
  General Articles
 
CC++ 
  Flash 
  Internet Security 
 
Java 
 
Linux     
  Networking  

 

Computing | Java 
Variables and Strings

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.

  Deepak Chandrasekaran
February 7, 2002

Articles on Java     
By Deepak Chandrasekaran
JAVA: A Beginner's Guide    
Oops... I did it Again!   
Objects and Classes    
First Java Application    
Your First Java Applet   
Statements and Expressions  
Variable Type & Declaration 
Variables and Strings  
Arrays and Overloading in JAVA   

By Neeraj Mathur  
Java – Man Not Again!  
Common Misconceptions About Java 

By Ruchi Gupta
Java Virtual Machine  

Computing 
CC++ | Flash | Internet Security   
Linux | Networking  

General Articles  

Top
 


 

Recommend This Page!

Analysis | Architecture | Astrology | Ayurveda | Book Reviews | Buddhism | Cartoons | Cinema | Computing | Culture | Dances
Environment | Fables | Family Matters | Festivals | Hinduism | Health | History | Home Remedies | Humor | Individuality | Jagoji
Literary Shelf | Memoirs | Musings | Opinion | Parenting | Perspective | Photo Essays | Places | Ramblings
Random Thoughts | Recipes | Sikhism | Society | Spirituality | Stories | Teens | Travelogues | Vastu | Vithika | Women

Home | Hindi | Bolography | BoloKids | Kabir | Poetry | Quotes | Workshop | Writers | Contribute | Search | Contact


Boloji.com is owned and managed by Boloji Media Inc

Privacy Policy | Disclaimer
No part of this Internet site may be reproduced without prior written permission of the copyright holder.