In the interview often encountered such a problem: 1, what is immutable object. What are the benefits of immutable objects. In what scenarios to use it, or more specifically, why should the Java String class be set to immutable types?1, immutable objects, as the name implies is created after the object can not be changed, the typical example has a string type in Java
Original address: # Why String was immutable in Java?As we all know, the string class is immutable in Java. Immutable classes are simply instances of classes that cannot be modified. When an instance is created, all information cannot be modified when it is initialized. Immutable classes have many benefits, and this article briefly describes why the string
a const member function. The definition of move () is as follows:
Inline void screen: Move (int r, int C){If (checkrange (R, c )){Int ROW = (r-1) * _ width;_ Cursor = row + C-1; // modify _ cursor}}
We noticed that move () modifies the data member _ cursor. Therefore, it cannot be declared as const without modification. However, it may seem strange that _ cursor cannot be modified for a const object in the screen class. Because _ curso is only
C ++ Primer study note _ 24 _ class and data abstraction (10) -- static and Singleton modes, auto_ptr and Singleton modes, const member functions, const objects, mutable modifiers, constmutable
C ++ Primer study note _ 24 _ class and data abstraction (10) -- static and Singleton modes, auto_ptr and Singleton modes, const member functions, const objects,
1. A simple implementation of a mutable parameter listWhen a method is called, the number of arguments or the type of the method is unknown, which is called a mutable argument list. In previous Java code, you could use an object array to implement such a function. Because all classes are directly or indirectly inherited from the object
C + + Primer Learning Notes _24_ class and Data Abstraction (--static) with singleton mode, auto_ptr and singleton mode, const member function, const object, mutable modifierObjective"Example" writes out the five basic principles of object-oriented?Solution: Single Duty principle, open closure principle, dependency inversion principle, interface isolation principle and Richter replacement principleRichter s
What is a mutable parameter
Variable-length parameters are provided in Java 5, allowing for the passing of indefinite lengths of parameters when invoking a method. The variable-length parameter is a syntactic sugar in Java and is essentially an array-based implementation
void foo(String... args);void foo(String[] args);
Method signature([lj
functions of a class, can only be read, not assignable, and can only invoke functions of the same const type. Of course, if you need to modify a member variable in a const-type function, C + + provides a way to use the keyword "mutable", which indicates that the member variable is ready to become and can break through the const type function.
Class A
{
Public
A
The list in Python is quite the same as the array in C, List: Lists initialized using [], tuple: tuple initialized using ();A list of lists1 #!/usr/bin/python23 #list初始化: []4 classmate = [' s ', ' su ', ' Sum ', ' sume ', ' Sumey '];56 print classmate;7 print classmate[0]; #第一个元素: S8 print classmate[-5]; # negative, i.e. from right to left: s9 Print classmate[(len (classmate)-1)]; #classmate [4]; Positive number, that is, from left to right: Sumey#append AppendClassmate.append (' Hi ');Print cla
own inner classes, such as:Back in the message class, we can see that there are also get and set under the info and what fields, well, this is the getter and setter functions, the general Getter function using the default is good, the setter function is actually at the time of assignment to do some processing, To prevent the setter function from infinitely recursive calls, you can use the $ symbol to access member variables in a
Java1.5 adds new features: variable parameters: For cases where the parameter number is indeterminate and the type is determined, Java handles the mutable parameter as an array. Note: The mutable parameter must be in the last item. When the number of mutable parameters is one more, there must be one that is not the las
Java1.5 adds new features: variable parameters: For cases where the parameter number is indeterminate and the type is determined, Java handles the mutable parameter as an array. Note: The mutable parameter must be in the last item. When the number of mutable parameters is one more, there must be one that is not the las
java1.5 adds new features: variable parameters: For cases where the parameter number is indeterminate and the type is determined, Java handles the mutable parameter as an array. Note: The mutable parameter must be in the last item. When the number of mutable parameters is one more, there must be one that is not the las
Java SE5 Adds a mutable parameter list attribute parameters can be defined like this, (Object...args). Variable parameters are defined with "...", and args is an array of mutable parameters. As an example:Package sample; Class a{} public class Newvarargs { static void
() {return
salary
}}
Double test = Harry.getsalary ();
Test = m;
SYSTEM.OUT.PRINTLN (test);
System.out.println (Harry.getsalary ());Printed results: 50.0
75000.0
Workaround:If you need to return a reference to a Mutable object, you should first clone it. Object clone refers to a copy of an object that is stored in another location. Here is the modified code:
Class Employee
{
private String name
What is an immutable object?As we all know, in java, the String class is Immutable. So what exactly is an immutable object? It can be said that if an object cannot change its state after it has been created, then the object is Immutable. The inability to change the state means that the member variables within the object cannot be changed, that the value of the underlying data type cannot be changed, that a
Java allows methods to use variable parameters after 1.5, the advantage of a mutable parameter is that it allows 0 or more parameters to be passed. For example, there is a code to download the following:Java code 650) this.width=650; "class=" star "src=" Http://yizhenn.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border:0px; "/>
Publicclass T
" +args.length); } Public Static voidMain (string[] args) {f (A); f (); G (1); g (); System.out.println ("int[]:" +New int[0].getclass ()); }}Output:Here, the class description, which has a "[" beginning, indicates that the class is an array, and then the data type of the array is immediately followed by the parentheses, as in the following I indicates that this is a primitive data typ
We might want to do something about the parameters in one method in some particular requirement environment, and the parameters in these methods are not specified, so what do we do? In fact, Java has considered this situation for us, that is, the use of variable parametersFeatures of variable parameters:1: Can only appear at the end of the parameter list2: ... Between the variable type and the variable name, there are no spaces before and after it can
Java1.5 adds new features: variable parameters: For cases where the parameter number is indeterminate and the type is determined, Java handles the mutable parameter as an array.Characteristics:1. Can only appear at the end of the parameter list;2. Ellipsis (...) Between the variable type and the variable name, there are no spaces before and after it can be3. When invoking a method of a
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.