Results 1 to 4 of 4

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

  1. #1
    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,

  2. #2
    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.

  3. #3
    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,393

    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.

Similar Threads

  1. Which java enum should I give to the variable "followers"?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 20-02-2010, 05:24 PM
  2. Defining a Remote Object in Java
    By Dharamsi in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 07:10 PM
  3. what is the difference between c and java?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 04:11 PM
  4. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  5. Replies: 3
    Last Post: 25-04-2009, 11:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •