initialize  variables

Posted on 2004-11-17
Last Modified: 2010-03-31
Hi experts,
  when we initialize variables, why sometimes we need new, but sometimes don't.

  float num = 1 ;
  Cat meow = new Cat() ;

 why I need new in the second case, but don't need one in the first ???
Question by:meow00
    LVL 3

    Expert Comment

    In the first case If it going to be wrapper class then u need to declare like
    Float f = new Float()
    In the second case u r creating an object(Class declaration)
    clearting a seperate memory allocation to that meow object.
    LVL 3

    Expert Comment

    Also u must know the concept of pass by value and pass by reference.
    u can click the link..u will understand the concept.
    LVL 3

    Accepted Solution

    Also the main concept is
    In the first case it is a primitive type..(like int,float,byte,char and double).for these cases these there is no method implementation(it is not a class),for that java made wrapper classes like for int Integer class (wrapper class).
    In case of class u can safely send the data thro marshalling etc.
    thro class object u can manipulate..
    any doubts ask me..
    LVL 3

    Expert Comment

    LVL 92

    Expert Comment

    new is only needed to creeate new instances of a class
    it is not needed for primitives.
    LVL 2

    Expert Comment

    1st Case:

     a. primitive datatype.
     b. when the primitive datatype is declared memory is allocated in the heap.

    2nd Case:

     a. creating reference type and assigning object to that reference.
     b. when the keyword(new) is used, this means that you are allocating memory from heap to that object.

     c. if you just type:

        Cat meow; ( this means that you are just creating reference type not allocating memory or assigning object to it.

       and if you type:

       Cat meow = new Cat(); ( this means that you are creating both reference type , allocating memory and assigning new object to that reference.

    In case of primitive datatypes memory is allocated by just declaration so there's no need of new for it

    if you still got any confusion ask me.


    Expert Comment

    As has already been mentioned, the reserved word new is only required when creating a new object and primitives (int, double, long, byte, etc.) do not require the word new.  Also, even though String is a type of object, Java does not require you to declare "new String()" when creating a new String object--it will allow you to do

    String str = "hello";


    String str = new String("hello");

    There will other times when you will not be able to create objects directly using "new".  InetAddress objects (in are an example of this.  (Note: the InetAddress class is basically used for IP addresses).  You will get an error if you type the following statement:

    InetAddress ip = new InetAddress();

    That is because certain classes do not allow you to create objects of their type directly (i.e. they have a private constructor).  In this case, you would have to use one of the classes static methods to create such an object (or a method from another class).  For example:

    InetAddress ip = InetAddress.getLocalHost();

    This is done typically to control the creation of such objects.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now