Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Instantiation

  Asked By: Hamish    Date: Nov 17    Category: Java    Views: 496
  

I have come across a problem, which I would have
lerned a long time ago. I'm just stuck. You have many
times been discussing instantiation without calling
the new() keyword. This is not possible in Java except
for the string class in a way, which of course is
strange.

What I wonder is, since Java is in a way C++, is
String myString; meaning the same as in Java WITH new
in its' predecessor (C++), is that's why these
questions come up so often? And while I'm at it, we
get either instantiated functions or class (static)
operators like this, myLabel.setText(""); but in C++
we sometimes have -> doing the same thing, why not in
Java (while I'm at it, what is ->, since I'm only into
Java, I don't know).

I hope these wonderings are not too much off topic.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Kay Rodriguez     Answered On: Nov 17

In C++, When u create a object pointer u have to use ->, this is a syntax. if we
want to use any pointer variable not just objects, we have to use -> for
accessing var.

 
Answer #2    Answered By: Paul Brooks     Answered On: Nov 17

Though java  and C++ are quite similar, they have a lot of
differences too. The basci idea is in java we don't have pointers.
i.e. we can't have '->' as in C++.

Look, In java alot of things have been standarised. For example.
1. In java, if we say

Object obj;
// this means obs is REFERANCE to an object of type Object.

i.e. all identifiers in java are refernces. except for Primitive
types (like int, boolean, etc.)
but in C++, u have two mechanism.
1. ur identifier may be representing the object itself e.g
Object obj;
2. ur identifier may be refering to the object. e.g
Object *obj;

In a real life we use object reference for implementation (in C++)
since otherwise method calling might be expensive.

As far as String class  is concerned, It is higly used class, so a
sortcut has been provided by instantiation without calling the new()
keyword.

U can look into a java book by Bruce Eckle [Thinking in Java]. After
reading this book, u will now what's diff. between java and C++? why
those differences? etc.

 
Answer #3    Answered By: Sheryl Morgan     Answered On: Nov 17

Thanks a lot. I hope this helped a lot of people,
because I have asked a lot of programmers who really
didn't know this. (I hope everybody stays with Java
though, now I know I will.)

 
Didn't find what you were looking for? Find more on Instantiation Or get search suggestion and latest updates.




Tagged: