Someone once said, "me in the programming language does not bother?" What makes me more is that an experienced programmer who had to deal with several languages \u200b\u200b(eg C, Java) should be no problem to deal with programming in another language (eg C #). There is a lot of truth, but is without a comprehensive knowledge of the language code for this developer will definitely reliable?
Consider a simple example written in Java:
Integer i1 = 1000;
Integer i2 = 1000;
if (i1 == i2) {
System.out.println ("same object");}
else
{System.out.println ("different objects");}
if (i1.equals (i2)) {
System.out.println ("values \u200b\u200bof the objects are the same");
} else {
System . out.println ("value of the objects are not the same");}
Is there anything in this example, which could lead us to think - rather not. It seems clear that the first condition will be displayed on the console "Different objects" , while the second condition "equal facilities" . The first condition will be fulfilled only if we have to deal with variables that indicate precisely the same object. The second condition is already comparing the values \u200b\u200bof these objects so the case is quite obvious. 's modify this example:
Integer i1 = 10;
Integer i2 = 10;
if (i1 == i2) {
System.out.println ("same object");
} else {
System. out.println ("different objects");}
if (i1.equals (i2)) {
System.out.println ("values \u200b\u200bof the objects are the same");
}
else {System.out.println ("values \u200b\u200bof the objects are not the same");}
What is it? The first condition is met the result is displayed on the console "same object" . Why? The answer is simple, although not obvious in the Java language in order to save memory class instances and Short Integer with values \u200b\u200bfrom -128 127 to point to the same object (memory area). Otherwise the situation is if we use the explicit operator new : Integer i1 = new Integer (10);
Integer i2 = new Integer (10);
if (i1 == i2) {
System.out.println ("same object");}
else {System.out.println ("different objects");}
if (i1.equals ( i2)) {
System.out.println ("values \u200b\u200bof the objects are the same");}
else {System.out.println ("values \u200b\u200bof the objects are not the same");}
Then you will see "different objects" . conclusion from this is one that without a thorough knowledge of the language constructs that might seem obvious can surprise us in the least expected time. However, there is an advice: if you are czegoś pewien sprawdź to, a jeżeli jesteś czegoś bardzo pewien sprawdź to dwa razy...
0 comments:
Post a Comment