Protective Copy Inefficiency in Java

I am a longtime C / C ++ programmer who is learning Java. I read about the problem of breaking encapsulation using an access method that returns a link to a private field. The standard Java solution is similar to protective copying - calling the copy constructor or clone () to create a copy of the field and return a link to the copy. I do not understand why no one is concerned about the inefficiency of creating a protective copy. In C ++, Accessor will simply return a pointer to const, protecting the private member without copying. Why is Java not referencing const?

+4
source share
2 answers

Why is Java not referencing const?

, , , , . ( " Java-", ) , . const...

, C ++ const , , "" . (. const-correctness.) , Java, ( ) , . .

, Java , , , . , Java ... , "" - , .

+1

. , , Java const. ( , - Java, const , , , - . [ ++ , Java, .])

, , , .

(String, Date) , .

@DavidWallace, , , - , . , .

: java.util.Map, , . - const, , :

public interface Lookup<K,V> {
    public V get(K);
}

. , , ... ... .

MapLookup, , 5 .

, , , , . ( , , .)

0

All Articles