aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes refrance variable clearance required.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "refrance variable clearance required.." Watch "refrance variable clearance required.." New topic
Author

refrance variable clearance required..

deepesh mathur
Ranch Hand

Joined: Aug 13, 2007
Posts: 39
hi ranchers,
by this statement(of java)
class_name object_name=new class_name();

i understand that a refrence variable object_name of type class_name is being pointed towards the newly created instance of class_name.
so i want to ask:
1.am i correct???
2.what is role of type of refrence variable here???
3.how the casting of refrence variable is done and why???
i've studied this topic from k&b and still have doubts what should i do???
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

1. Yes - this line of code declares a new variable 'object_name' of type 'class_name' and initialises it by making it refer to a new instance of class 'class_name'.

2. All variables have a type, and here 'class_name' is the type of the variable named 'object_name'.

3. There is no casting involved in this line of code.

Maybe you are thinking that this is more complicated than it really is...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Deepesh,

May I ask if you're coming from a different programming language background? If so, which programming language are you more familiar with?

In my experience, people learning Java as their first programming language don't usually struggle with the concepts you mentioned, whereas it can be more difficult for people used to a different language (especially weakly-typed scripting languages). So we can probably help you more if we better understand where you're coming from.

Best regards!
[ November 03, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
deepesh mathur
Ranch Hand

Joined: Aug 13, 2007
Posts: 39
ya you are right,
infact i consider bert bates along with kathy to be the most learned java writers..
and when they repeated same concept thrice(uptill i've read) i found myself in chaos and put up such a question so sorry alongwith thanks..

regards
DEEPESH
deepesh mathur
Ranch Hand

Joined: Aug 13, 2007
Posts: 39
Originally posted by Kelvin Lim:
Hi Deepesh,

May I ask if you're coming from a different programming language background? If so, which programming language are you more familiar with?

In my experience, people learning Java as their first programming language don't usually struggle with the concepts you mentioned, whereas it can be more difficult for people used to a different language (especially weakly-typed scripting languages). So we can probably help you more if we better understand where you're coming from.

Best regards!

[ November 03, 2007: Message edited by: Kelvin Lim ]


hello sir,
yes i've learned c,c++,python and more languages
and you can take granted that i've a good command on c++(and perl script i addmit)..
kindly tell me a way out..


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

C++ has almost the same concepts as Java. So if you understand C++ well, then what exactly is it about the Java way that you don't understand?
deepesh mathur
Ranch Hand

Joined: Aug 13, 2007
Posts: 39
Originally posted by Jesper Young:
C++ has almost the same concepts as Java. So if you understand C++ well, then what exactly is it about the Java way that you don't understand?




sir,
i agree with the fact that java and c++ are same with concepts but to contradict there are some issues(genrally related to syntexes) such as:
1. no pointers
2. exception catching/handeling procedure
3. concept of refrence variables
4. concept of floats
and many others due to which i feel wired...
but no matter i'll take care of it..
and by the way thanks for your concern... :roll:

Regards
DEEPESH
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Sorry, but I am in agreement with Jesper here. This example is a variable declaration and initialization. C++ and Java is very similar in this regard.

Furthermore, having "no pointers" actually makes Java syntax simpler, as there are less options. As for references, C++ does have them -- they even have more complex versions of them called smart pointers or handles. I don't know what does exceptions or floats have to do with this example though.

Henry
[ November 04, 2007: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

How are C++ and Java different when it comes to floating point? I know that unlike Java, C++ does not mandate IEEE 754 in its language standard (and nor are the types defined to a specific size), but most hosted implementations do.

Otherwise, the semantics of both languages when it comes to 'float' and 'double' are identical, isn't it?


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Deepesh,

So far it looks like we're all struggling to know how to help you - sorry about that.

Perhaps you could explain to us what it is about reference variables that you think is different between C++ and Java. There is enough C++ experience in this forum to help from that direction.

In other words, "What's true in C++ that's not true in Java?"

- Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
deepesh mathur
Ranch Hand

Joined: Aug 13, 2007
Posts: 39
sir,
i agree with the fact that java and c++ are same with concepts but to contradict there are some issues(genrally related to syntexes) such as:
1. no pointers..


when we say array we mean a pointer specifying on the heap of memory(isn't it??)because in arrays we know the starting location stored in pointer we have a faster acess then linklists that makes it better hence how come we say that java dosen't have pointers???(if you require source for above statement kindly let me know that or post your e-mail)


3. concept of refrence variables

in my openion we cannot pass a call-by-value parameter in c++ i.e the array object don't have a copy of refrence type literal(or does it???)


4. concept of floats

you'll think i am really wired but please have a look at this code..

class Float {
float f=2.23,d;
f=d;
}
this won't compile in java
but it will compile in c/c++



as not enough
class Float {
float f=2.23,d=10.33,e;
e=d%f;
}
this won't compile in java
but it does in c/c++





and many others

//syntex of upper
int i=4;
i=i++ + i++;
//printing i will give
10(in c/c++)
9(in java)
to be honest i am an engineering major(persuing) for which i have to look at each and every aspect of the thing(genrally a language)
due to which i feel wired...
but no matter i'll take care of it
and thank you all once again..
Regards
DEEPESH
deepesh mathur
Ranch Hand

Joined: Aug 13, 2007
Posts: 39
nd by the way i want to ask another question
is java fully object oriented?
(i've heard someone saying that java is not fully object oriented(almost 99% object oriented) can you tell me why???)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

when we say array we mean a pointer specifying on the heap of memory(isn't it??)because in arrays we know the starting location stored in pointer we have a faster acess then linklists that makes it better hence how come we say that java dosen't have pointers???(if you require source for above statement kindly let me know that or post your e-mail)


Java and C++ arrays basically behaves the same (single dimension). The only thing missing in Java is pointer arithmethic, which means that Java can't use a pointer to traverse the array, it must always dereference to the elements using indexes.

Having pointer arithmetic doesn't make arrays faster -- In Java, you just have to do index arithmetic instead of pointer arithmetic.

in my openion we cannot pass a call-by-value parameter in c++ i.e the array object don't have a copy of refrence type literal(or does it???)


C++ has call-by-value. C++ has call-by-pointer. C++ even has a thing where it looks like call-by-value, but it is really call-by-pointer, so a programmer may not notice that the function call can change the value -- unless he/she looks at the include file....

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by deepesh mathur:
nd by the way i want to ask another question
is java fully object oriented?
(i've heard someone saying that java is not fully object oriented(almost 99% object oriented) can you tell me why???)


Java and C++ supports primative types. There are those who believe that this support makes the language not fully object oriented.

Personally, I don't care about this distinction, because quite frankly, what is the disadvantage of a language that support integers, characters, or floating point numbers?

Henry
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Henry Wong:


C++ has call-by-value. C++ has call-by-pointer. C++ even has a thing where it looks like call-by-value, but it is really call-by-pointer, so a programmer may not notice that the function call can change the value -- unless he/she looks at the include file....

Henry


I fail to see how Java alleviates this. I pass an object reference as an argument to some method. Now how do I know if this method called a method on my object?

Even worse, let's say a method on an object A returns me a reference to object B. Later I call a method on object B. Now is object A modified?
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by deepesh mathur:

class Float {
float f=2.23,d;
f=d;
}
this won't compile in java
but it will compile in c/c++


Thanks for pointing this difference out (I'll assume you meant 'd' to be defined as double).



as not enough
class Float {
float f=2.23,d=10.33,e;
e=d%f;
}
this won't compile in java
but it does in c/c++


I hope you meant the other way, because I know it will NOT compile in C++. To evaluate the "remainder" of a division between two floating point values in C++, one has to use either fmod or modf.


//syntex of upper
int i=4;
i=i++ + i++;
//printing i will give
10(in c/c++)
9(in java)


That is incorrect. In C++ it produces "undefined behaviour". Java tightened the grammar so that such expressions always produce predictable results.

- Anand
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

I fail to see how Java alleviates this. I pass an object reference as an argument to some method. Now how do I know if this method called a method on my object?


My issue has nothing to do with what calls what, etc. Or whether something is pass by reference or not. My issue has to do with the contract... In C++, when I do this...



I expect that the value of "a" can change. It is pass by pointer. When I do this...



I used to expect that the value of "a" won't change, as it is pass by value.

This isn't true anymore with the newer C++ compilers -- as it is possible to declare the function like this.



In Java, you don't have a choice, the parameter passing is defined. In C++, you get to choose, which can be nice... and I am not going into a debate of whether it is a good thing or not, because, I don't care if it is pass by value or pass by pointer -- as long as it is easy for me to tell the difference. Forcing me to look at the include file for function calls is just not acceptable.

Henry
[ November 05, 2007: Message edited by: Henry Wong ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Deepesh,

I don't know if we're making any progress here or not? In retrospect I think we should have split this thread apart into several different threads early in its life

What I'd like to recommend is that if you don't feel that your questions have been answered start a new separate thread for each question.

Thanks,

Bert
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Henry Wong:

My issue has nothing to do with what calls what, etc. Or whether something is pass by reference or not.


If that is what you got out of my post, I would say you've read it very superficially.


In Java, you don't have a choice, the parameter passing is defined.


To what end?



My issue has to do with the contract...


And I was trying to point out that Java doesn't help any more than C++ takes away.

With C++ you could at least get an answer from looking at the interface (e.g., function's declaration in a header file). With Java, short of looking at the implementation, you cannot get answers for the scenarios I pointed out in my previous post.

In any case, like Bert pointed out, this issue is tangential to OP's question, and I am certainly not about to start a Java versus C++ language war (in a moderated Java forum).

best wishes,
- Anand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: refrance variable clearance required..