Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



What is the difference between defining and declaring a variable in java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 25-02-2009
Member
 
Join Date: Feb 2009
Posts: 4
What is the difference between defining and declaring a variable in java?
  

Hello!

What is the difference between defining and declaring a variable in java?

Regards,

Reply With Quote
  #2  
Old 25-02-2009
Member
 
Join Date: May 2008
Posts: 115
Re: What is the difference between defining and declaring a variable in java?

The basic types of variable in C are namely char, int, double and float. First the variables have to be declared. This is done as follows:

First any of the data type defined above must be mentioned and this is followed by the name of the variable. For instance if wants to declare a variable v1 of type char it is done as follows

char v1;

While declaring a variable what happens is the data type is described to the compiler and no space or memory allocation takes place. In other words just the data types are mentioned without any memory storage for the data types. While defining the variable the declaring the data type of the variable along with space allocation for the variable takes place.
Reply With Quote
  #3  
Old 25-02-2009
Member
 
Join Date: May 2008
Posts: 72
Re: What is the difference between defining and declaring a variable in java?

Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined.
In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization.
e.g String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.
Reply With Quote
  #4  
Old 25-02-2009
Member
 
Join Date: May 2008
Posts: 2,377
Re: What is the difference between defining and declaring a variable in java?

The following is a variable declaration:

int x;

The following is a variable definition:

int x = 10;

The following are method declarations:

abstract void foo();
native void foo();

method definitions is when what the method does is defined:

void foo() {
System.out.println("Hello");
}

In interfaces you can define variables (but they are really all constants). In interfaces you can only declare methods, but not define them.

In abstract classes, some methods may be only declared and others declared and defined.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is the difference between defining and declaring a variable in java?"
Thread Thread Starter Forum Replies Last Post
Which java enum should I give to the variable "followers"? KADRI Software Development 4 20-02-2010 05:24 PM
Defining a Remote Object in Java Dharamsi Software Development 5 18-02-2010 07:10 PM
what is the difference between c and java? Linoo Software Development 5 28-11-2009 04:11 PM
Runtime Error 91 : Object Variable or with block variable not set Ryan21 Software Development 2 28-08-2009 07:51 PM
What is Global Variable static? Declaring static variable instead of Global variable Reckon Software Development 3 25-04-2009 11:34 AM


All times are GMT +5.5. The time now is 04:52 AM.