?
Solved

initialize  variables

Posted on 2004-11-17
10
Medium Priority
?
172 Views
Last Modified: 2010-03-31
Hi experts,
 
  when we initialize variables, why sometimes we need new, but sometimes don't.
e.g.

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

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

Expert Comment

by:vikraman_b
ID: 12611939
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.
0
 
LVL 3

Expert Comment

by:vikraman_b
ID: 12611974
Also u must know the concept of pass by value and pass by reference.
u can click the link..u will understand the concept.

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
0
 
LVL 3

Accepted Solution

by:
vikraman_b earned 1000 total points
ID: 12611993
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..
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 3

Expert Comment

by:vikraman_b
ID: 12612002
0
 
LVL 92

Expert Comment

by:objects
ID: 12612054
new is only needed to creeate new instances of a class
it is not needed for primitives.
0
 
LVL 2

Expert Comment

by:jprgn
ID: 12612181
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.

 
0
 

Expert Comment

by:introbert
ID: 12622387
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";

or

String str = new String("hello");

There will other times when you will not be able to create objects directly using "new".  InetAddress objects (in java.net) 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.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month3 days, 23 hours left to enroll

600 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