English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

In C, 2 objects of same class(structure) can be subtracted(Please clear to me, why subtraction of 2 Objects needed?), but how I can do it in case of Java?

2006-12-27 23:51:26 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

No, you can't subtract structures in C. What you can subtract is addresses (pointers to) of the objects of the same type. This is useful particularly when you are dealing with arrays. For example, something like end_of_array - start_of_array + 1 will give you the size of the array, or ptr_to_object_in_array - start_of_array will tell you the index of the given object.

You cannot do this (and lots of other cool and useful things) in Java, because it has no pointers, and gives you no way to obtain an object's address

2006-12-28 01:36:04 · answer #1 · answered by n0body 4 · 0 0

I think your question is confusing to yourself also!! However let make me clear that there is no concept of object in C. C is a procedural middle level language which deals only with variables and memory locations of those. So in case of C you may need to subtract 2 structure variable or array just for managing a program by direct calculation of memory addresses of the variables. C is popular for its vast availability of memory manipulation of variables through pointers.
But in case of java you've to do everything by objects of classes. It is totally object oriented language and you can do anything by object orientation. For your answer I would like to state that you must have to create 2 objects of a class and you can subtract them as simple as subtracting 2 from 4!! But there is some conceptual things also needed which you have to learn at first before doing it. But I want to make you clear that there is a conception of pointer also exists in java but you don't have to bother for it. Because you can't do any memory manipulation directly in java, so it is totally different from C.

2006-12-28 03:40:24 · answer #2 · answered by Anonymous · 0 1

Think in terms of a case where subtraction would be a valid thing to do, for example with a complex number implementation. In C++ you can define operator overloading to allow you to use the "-" symbol to subtract one instance of your complex number class from another. Java does not have operator overloading, however. So you are forced to write methods like subtract(). The syntax would look something like:

Complex a = new Complex(5,4);
Complex b = new Complex(3,2);
Complex diff = a.subtract(b);

[In C#.NET you can define operator overloading too, so the last line would simply look like: diff=a-b;]

2006-12-28 01:53:12 · answer #3 · answered by Dr.Mr.Ed 5 · 0 1

in Java, u can subtract primitives (int, short, long) but in case of Wrapper Objects(Integer, Long...etc) you can change them ro primitives and then subtract then return them to Wrapper Objects again.

2006-12-28 00:05:12 · answer #4 · answered by a4adidas 1 · 3 1

fedest.com, questions and answers