Embla Tingeling

Ranch Hand
+ Follow
since Oct 22, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Embla Tingeling

Kevin McMahon wrote:
I've been asked recently "Is it better to store objects within another object, or store an id which allows us to look up the other object?"
My initial instinct is that presuming the lifecycle of the nested object is dependent on the containing object, nest the object itself. But I realised I couldn't actually explain in clear words why.



This is the difference between ordinary composition and another form of composition called aggregation. The difference is the duration of the relationship. Ordinary composition is forever whereas aggregation is temporary. Compare for example the different relationship a Car has with its Parts and with its Passengers. Both are has-a relationships but if a Part is missing the Car is broken while Passengers come and go without compromising the Car. Parts are forever (it's the very fabric of a Car) whereas Passengers are temporary. The former is ordinary composition, the latter is aggregation.

But note that these relationships are modelling relationships. It doesn't really matter how they're implemented (direct with an object reference or indirect via some other identity representing the object). That wouldn't change the nature of Part's and Passenger's relationships with Car. Whether you use a "bald" reference or some id is more a matter of convenience - which fits the design or implementation better.
9 years ago

Amirr Rafique wrote:Thanks for your reply Embla. I have one confusion in digesting this concept, As per your post what i understood is

1111 1111 1111 1111 1111 1111 1111 1111 => -1

then what will be value of following bit pattern

1000 0000 0000 0000 0000 0000 0000 0001

please explain



As has been mentioned integers (long, int, short and byte) in Java are stored internally in a so called two's complement format. Positive numbers are as expect but the negative are kind of the other way around, like

000 : 0
001 : 1
010 : 2
011 : 3
100 : -4
101 : -3
110 : -2
111 : -1

Analogously if you have,

1000 0000 0000 0000 0000 0000 0000 0000

that will be the most negative 32 bit number there is. If you instead have a 1 at the end like

1000 0000 0000 0000 0000 0000 0000 0001

it will be 1 less small. So that's the second most negative 32-bit int there is.
9 years ago

Vidmantas Maskoliunas wrote:Another interesting approach is to use java.util.TreeSet<Integer> API. But that may be an overhead.



There's no reason to use a TreeSet when order doesn't matter and there's a HashSet available.
9 years ago

Campbell Ritchie wrote:The nested loop technique runs in quadratic time, so it is not very efficient, but is almost certainly what your teacher wants you to find.



If performance is a concern the inner loop linear searches can later be replaced with hashed searches which will give linear complexity to the algorithm.

Sorting is only a good option if the arrays are intially sorted, not if you first need to sort them.
9 years ago

Aruna Balasuriya wrote:Why ???



Please post the error message with your idea of what it means.
9 years ago

Anirban Chowdhury wrote: Would really appreciate if somebody explains me the reason for this,this one certainly looks like a very very wicked SCJP question..



The expression you see actually is this,

if ((a==0 && (b==0)) ? (b==0) : ((c==1)&&(d==5)))

So ((a==0 && (b==0)) first is evaluates to false and then ((c==1)&&(d==5)) is selected and becomes the result of the whole expression which then become true.

[Well, I didn't notice the other reply for some reason but I let my post stay]
9 years ago

Pradeep Adibatla wrote:
what should be corrected in the above?





You need to assign objects to the variables you declare, otherwise they will be null.

For this to work properly you most likely will have to override the equals and hashCode methods of ClassA.
9 years ago

Satya Siripuram wrote:is there any API to access private members of a class?



Yes, the Reflection API, but it's not recommended because you seriously compromise the type safety of your program.
9 years ago

Amirr Rafique wrote:I want to invert the integer value 0 in such a way that all its bits are set to 1. Please help me how to do it?



You use the bitwise not operator ~. To invert all 0's in 0 you do ~0.

If you print ~0 you'll get -1. That's because all 1's is the internal coding for -1. This means -1 is an alternate way of having an int with only 1's.
9 years ago

Christopher Laurenzano wrote:I want to give the CD objects the name CDRecord1(CDRecord1.Artist, CDRecord1.albumTitle, CDRecord2.name, CDRecord2.albumTitle, etc) but I'm not sure how or if I can use that naming method with an Array or an Array List in some kind of a loop to advance to the next record. Can anyone tell me if this is possible or do I have to wait until I read more?



You can use an ArrayList. It will grow as you add CD objects.

Note that in Java objects are always represented by references (pointers to objects). This means that for each CD object you store, only a reference (4 bytes) will be entered into the ArrayList. This also means the CD objects will hold the bulk of the information, not the ArrayList itself. The ArrayList will consume far less memory than the sum total of CD objects.

The overhead for an object is 16 bytes. One String object will consume 16 bytes of overhead plus 2 bytes for each character plus an integer of 4 bytes holding the String length. Say the artist name is 10 bytes on average and the album title too. This gives 16 + 2*10 + 4 = 40 bytes for the artist and 40 bytes for the title String object. Then you have the CD object itself. It will hold two String object references of 4 bytes. This gives 16 + 2*4 = 24 bytes.

So all in all one CD object will occupy 24 + 40 + 40 = 104 bytes. This should be compared with the 4 byte reference which is stored in the ArrayList for one CD object. This means only about 4% of the total memory requirements falls on the actual ArrayList and 96% will be on the CD objects themselves.
9 years ago

Vishal Hegde wrote:is it necessary because islands of obect is reallly confusing me ??? or let the JVM handle the gc thing???



You should be able to consider different situations and tell whether the GC can claim an object or not.

Mel Ram wrote:I think im suppose to use the linear search method.
I just don't know how to use the linear search method. The example in my homework is not very good in terms of how to use it.



Linear search means you scan the arrays.

You don't need to sort the arrays. The trick is to use nested loops (loops inside other loops).
9 years ago

Pradyumna Khadanga wrote:One of the interviewer asked me "How to write a custom Imutable class ?"
For this what is the exact answer ?



The exact answer is: Don't make it mutable.
9 years ago

santhosh.R gowda wrote:
Is it possible to add different values or objects to the same key of hashmap other than collection or array



A somewhat intrusive solution would be to add a "next" field to the objects. Objects associated with the same key could then chain themselves together in a linked list.
9 years ago