Martin

Senior Member
Dec 31, 2000
56,913
Any of you peeps want to help a honkey out with his Java? I've to explain what and why a code segment will output.



My answer was marked as incomplete;

/**Following on from both segments, the third segment creates the memory reference 'Hello World'.
* It then assigns the 'mystring2' string the same memory reference.
/* As a result, it will display 'M3 The two strings are equal */

Any help would be much appreciated.
Incomplete? The nerve.

Your terminology is a bit confused, I don't know what you mean by "segment" or which one is supposed to be "the third".

Line 2 creates a string.
Line 3 creates another string.

mystring1 and mystring2 are different objects, so the predicate in the if evaluates to false.
 

Buy on AliExpress.com

IrishZebra

Western Imperialist
Jun 18, 2006
23,327
Incomplete? The nerve.

Your terminology is a bit confused, I don't know what you mean by "segment" or which one is supposed to be "the third".

Line 2 creates a string.
Line 3 creates another string.

mystring1 and mystring2 are different objects, so the predicate in the if evaluates to false.
The segments are basically just blocks of code 'snipped' out from a programme and there were 3 to analyse

1:String mystring1 = "Hello World"
String mystring2 = new String("Hello World");
if (mystring1 == mystring2)
The first segment will return 'M1 The 2 Strings are not equal'.
*This is because when a string is defined as string = "string1", it creates a permanent 'old generation' memory reference for the value 'string1',
*This means that "string1" always returns to the same memory reference as it is static data.
*When a string is defined as string = ("Hello World"), a new memory reference is create each time, opposite to the string = "string1".
*The strings are not equal because they reference different memory areas and the requirement of '==' is that the same references are equated.
* */
2:String mystring1 = "Hello World";
String mystring2 = new String("Hello World");
if (mystring1.equals(mystring2))...

**The Second segment will return 'M2 The strings are equal because the (string1.equals(string2)),
* compares the content of the string rather than the memory references as in the first segment,
*/
They are all marked as correct with the right output chosen,
'Code is all good but part 3 (The one I posted) of 2.6 needs a bit more explanation as to why this is the case.'



So:I'm intending to resubmit this as my answer

/**Following on from both segments, the third segment creates the memory reference 'Hello World' for mystring1.
* It then assigns the 'mystring2' string the same memory reference.
* This means that mystring1 and mystring2 are made to reference the same object by the java compiler as they are the same string literal (the text in the string is the same)
* As a simplification function of java the two strings are essentially the exact same thing which sastisfies the '==' operator
* As a result, it will display 'M3 The two strings are equal */
 

Users Who Are Viewing This Thread (Users: 6, Guests: 165)