Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes: Variables

Variables are places in memory to store values. There are different kinds of variables, and every language offers slightly different characteristics.

Name.

Data Type specifies the kinds of data a variable an store. Java has two general kinds of data types.

  • 8 basic or primitive types (byte, short, int, long, float, double, char, boolean).
  • An unlimited number of object types (String, Color, JButton, ...). Java object variables hold a reference (pointer) to the the object, not the object, which is always stored on the heap.

Scope of a variable is who can see it. The scope of a variable is related program structure: eg, block, method, class, package, child class.

Lifetime is the interval between the creation and destruction of a variable. The following is basically how things work in Java. Local variables and parameters are created when a method is entered and destroyed when the method returns. Instance variables are created by new and destroyed when there are no more references to them. Class (static) variables are created when the class is loaded and destroyed when the program terminates.

Initial Value. What value does a variable have when it is created? There are several possibilites.

  1. No initial value. Java local variables have no initial value, however Java compilers perform a simple flow analysis to ensure that every local variable is assigned a value before it is used. These error messages are usually correct, but the analysis is simple-minded, so sometimes you will have to assign an initial value even tho you know that it isn't necessary.
  2. User specified initial value. Java allows an assignment of intitial values in the declaration of a variable.
  3. Instance and static variables are given default initial values: zero for numbers, null for objects, and false for booleans.

Declarations are required. Java, like many languages, requires you to declare variables -- tell the compiler the data type, etc. Declarations are good because they help the programmer build more reliable and efficient programs.

  • Declarations allow the compiler to find places where variables are misused, eg, parameters of the wrong type. What is especially good is that these errors are detected at compile time. Bugs that make it past the compiler are harder to find, and may not be discovered until the program has been released to customers. This fits the fail early, fail often philosophy.
  • A declaration is also the perfect place to write comments describing the variable and how it is used.
  • Because declarations give the compiler more information, it can generate better code.

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.