• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)why the following code gives error
byte b1=1;
byte b2=127;
short s=(short) b1+b2;

2)what is the output pls explain
StringBuffer sb=new StringBuffer("Hello");
if(sb==sb.append("world"))
System.out.println("equals");
else
system.out.println("XXXXXX");
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)
byte b1=1;
byte b2=127;
short s=(short) b1+b2;
Promotion is created by operator +, although b1 is modifiered by (short), b2 is stili promotioned to int, and the result type of adding is int. At last , you get complie error.
2)what is the output pls explain
StringBuffer sb=new StringBuffer("Hello");
if(sb==sb.append("world"))
System.out.println("equals");
else
system.out.println("XXXXXX");[/B]
The result is "equals".But I donot know why. I always think the stringBuffer is a magic.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didnt get the first question correctly im not convinced that this will give u a compile time error i think the result of a arithmetic expression is type casted not b1.
for the second question StringBuffer is mutable unlike String and when u do the modifiaction u r changing the original Object and the object referenced by the variable is still the same
try using equals and this will give u false. u got to first understnad the immutability of the String and that StrinBuffers are mutable.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this weird thing!!!:
class Try {
public static void main(String[] args) {

StringBuffer sb=new StringBuffer("Hello");
StringBuffer sb1= sb.append("world");
if(sb==sb1)
System.out.println(sb1);
else
System.out.println("XXXXXX");

}
}
The output is "helloworld", because sb==sb1 returns true. This is what I suspect:
when we get a new string with originalString.append() method and compare it with the original string(using == or equals), the compiler magically truncates the new string back to the originally string and returns true for the comparison. This is very illogical considering the following strict comparison rules between two stringBuffer objects:
class Try {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
System.out.println(sb1==sb2);
}
}
You bet! The output is false. Can someone shed more light?
 
amrit singh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !!!guys
well
in second question i want to know
about the expression in if(....)
how it is evaluated
pls
thanxxxxxx in advance
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it isn't really illogical.In the code

StringBuffer sb=new StringBuffer("Hello");
StringBuffer sb1=sb.append("World");
sb.append("World") first appends the String "World" to sb and then it returns a reference to sb so that we have sb1 and sb referencing the same thing. The == operator checks to see if the references are the same and 'returns' true if they are. The = operator does not make a new object and copy the value of the fields from the lhs object to the fields of rhs object, it only assigns a reference. You would use clone() to make a new object that is an exact copy of another object with a different reference.
In the code:
StringBuffer sb= new StringBuffer("Hello");
StringBuffer sb1= new StringBuffer("Hello");
you are creating two new objects with seperate references so that when == is applied to them it will return false because they don't occupy the space in memory. The equals() method has the same default behaviour but, being a method, it can be overridden to check if the 'values' are the same ( or to do anything else you want it to.) Personally, coming from a C (with some C++) background, I tend to think of Java references as just pointers to objects that can't be dereferenced. Maybe someone could enlighten me on this point? How, exactly, does a reference differ from a pointer other than that you can't do pointer arithmetic and such with them?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rodney,
You are right Java object variables maintain references to the objects and Java does not allow any pointer arithmatic. However same code presented here will give different result for Strings, reason being strign are immutable in Java and hence String.append() will create a new object and pass on reference of that new object instead StringBuffer modifies existing object and passes on reference of that object.
 
George Toronto
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"sb.append("World") first appends the String "World" to sb and then it returns a reference to sb so that we have sb1 and sb referencing the same thing. "
yeah, i got it. i should understand the fundamental concept more.
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rodney. This really makes things much clearer to me. The equals method has the same default behavior as "==", but the overridden version in the String class really made me believe equals always compares the contents of two object. Actually that is wrong. StringBuffer objects uses the default equals method and therefore has very different behaviour from the String objects.
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Amrit:
The if condition is evaluated as follows:
sb.append("world") returns sb as "Helloworld"(think it as a StringBuffer object, not a String.) Because the right side of == still refers to sb even though the content has been changed, the == operator returns true. The same happens with equals(), because, as Rodney points out, the equals method has default behavior like ==. What make things messy is the String class use the overidden verison of equals() to compare the contents of the String objects. Therefore these codes will have different results for String.
Best wishes
Tom
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me for talking a little more about this topic because == and equals have been haunting me for a long time.
Java in a nutshell(2nd edition) writes on page 465 about java.lang.Object:
"equals() tests whether two objects have the same value( ie. not whether two varialbes refer to the same object, but whether two distinct objects have byte-for-byte equivalence.)."
I believe the author got it wrong just like me. The above definition is good for the overridden String equals() method. But for the original equals method in the Object class, no one explains better than the API documents:
"The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true)."
Therefore the default behavior of equals is the same as ==.
Please point out if I was wrong.
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, things become more interesting as we enter the world of String. According to API, there is no such method as String.append(), but instead you get String.concat(). Because of the immutability of String, this method returns different objects when concatenateing with an empty String.
code:
String s1="good";
String s2="";
System.out.println(s1==s1.concat(s2));
//return true because referring to the same object
System.out.println(s1==s2.concat(s1));
//return false because referring to the different object
System.out.println(s1.equals(s2.concat(s1)));
//return true because comparing two indentical object.
 
George Toronto
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom Tang:
[B]Actually, things become more interesting as we enter the world of String. According to API, there is no such method as String.append(), but instead you get String.concat(). Because of the immutability of String, this method returns different objects when concatenateing with an empty String.


if the empty string is added the end of a String by STring.concat(""), a new object is not created. plz check the API documents about String.concat().
regds
george
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic