RE: String/StringBuffer (was Re: An alternative to JSP)

2001-01-26 Thread Christopher Kirk


 Paul,
 
 Actually, my investigations in the past have shown that (at least in
 Sun's JDK 1.2) this is implemented as:
 
 new StringBuffer 
 ("My").append("dog").append("has").append("fleas").toString();
 
 It is also possible to write a statement like:
 
   "My" + "dog" + '.'
   
 The ability to concatenate a char points at an underlying StringBuffer
 implementation, which supports append(String) and 
 append(char) methods.
 
 
 Last paragraph in the java.lang.String javadoc says:
 
 The Java language provides special support for the string 
 concatentation operator
 ( + ), and for conversion of other objects to strings. String 
 concatenation is
 implemented through the StringBuffer class and its append 
 method. String
 conversions are implemented through the method toString, 
 defined by Object and
 inherited by all classes in Java. For additional information 
 on string concatenation
 and conversion, see Gosling, Joy, and Steele, The Java 
 Language Specification. 
 
 

On a performance note, doesn't it strike anybody the methods
on StringBuffer are declared 'synchronized' is a bad thing?

Object creation and synchronization are 2 of the most costly operations in
Java.


- Chris. 

Brainbench MVP Java2.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]




RE: String/StringBuffer (was Re: An alternative to JSP)

2001-01-26 Thread Michael . Smith

 Last paragraph in the java.lang.String javadoc says:
 
 The Java language provides special support for the string 
 concatentation operator
 ( + ), and for conversion of other objects to strings. String 
 concatenation is
 implemented through the StringBuffer class and its append 
 method. String
 conversions are implemented through the method toString, 
 defined by Object and
 inherited by all classes in Java. For additional information 
 on string concatenation
 and conversion, see Gosling, Joy, and Steele, The Java 
 Language Specification. 

And the Java Language Specification (Section 3.10.5: String Literals) says
this:

"Strings computed by constant expressions (15.27) are computed at compile
time and then treated as if they were literals"

http://java.sun.com/docs/books/jls/html/3.doc.html#101083


  Just thought that I would point out that: 
  "My " + "dog " + "has " + "fleas." will be compiled as one String:
  "My dog has fleas." and incurs no runtime penalties.  In the case
 
 Paul,
 
 Actually, my investigations in the past have shown that (at least in
 Sun's JDK 1.2) this is implemented as:
 
 new StringBuffer 
 ("My").append("dog").append("has").append("fleas").toString();

If this is actually the case, Sun's JDK is not in compliance with the spec.
However, in my tests, this is not the case. 

From this class: 

public class StringTest {
  static String blah = "My " + "dog " + "has " + "fleas.";
}

The following is the result from "javap -c StringTest" after compiling:

Compiled from StringTest.java
public class StringTest extends java.lang.Object {
static java.lang.String blah;
static {};
public StringTest();
}

Method static {}
   0 ldc #1 String "My dog has fleas."
   2 putstatic #5 Field java.lang.String blah
   5 return

Method StringTest()
   0 aload_0
   1 invokespecial #4 Method java.lang.Object()
   4 return


regards,
michael

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]




RE: String/StringBuffer (was Re: An alternative to JSP)

2001-01-26 Thread Arieh Markel


 Mailing-List: contact [EMAIL PROTECTED]; run by ezmlm
 list-help: mailto:[EMAIL PROTECTED]
 list-unsubscribe: mailto:[EMAIL PROTECTED]
 list-post: mailto:[EMAIL PROTECTED]
 Delivered-To: mailing list [EMAIL PROTECTED]
 From: [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Subject: RE: String/StringBuffer (was Re: An alternative to JSP)
 X-Spam-Rating: h31.sny.collab.net 1.6.2 0/1000/N
 X-MIME-Autoconverted: from quoted-printable to 8bit by amon.Central.Sun.COM id 
KAA15483
 
  Last paragraph in the java.lang.String javadoc says:
  
  The Java language provides special support for the string 
  concatentation operator
  ( + ), and for conversion of other objects to strings. String 
  concatenation is
  implemented through the StringBuffer class and its append 
  method. String
  conversions are implemented through the method toString, 
  defined by Object and
  inherited by all classes in Java. For additional information 
  on string concatenation
  and conversion, see Gosling, Joy, and Steele, The Java 
  Language Specification. 
 
 And the Java Language Specification (Section 3.10.5: String Literals) says
 this:
 
 "Strings computed by constant expressions (15.27) are computed at compile
 time and then treated as if they were literals"
 
 http://java.sun.com/docs/books/jls/html/3.doc.html#101083
 
 
 Just thought that I would point out that: 
   "My " + "dog " + "has " + "fleas." will be compiled as one String:
   "My dog has fleas." and incurs no runtime penalties.  In the case
  
  Paul,
  
  Actually, my investigations in the past have shown that (at least in
  Sun's JDK 1.2) this is implemented as:
  
  new StringBuffer 
  ("My").append("dog").append("has").append("fleas").toString();
 
 If this is actually the case, Sun's JDK is not in compliance with the spec.
 However, in my tests, this is not the case. 
 
 From this class: 
 
 public class StringTest {
   static String blah = "My " + "dog " + "has " + "fleas.";
 }
 
 The following is the result from "javap -c StringTest" after compiling:
 
 Compiled from StringTest.java
 public class StringTest extends java.lang.Object {
 static java.lang.String blah;
 static {};
 public StringTest();
 }
 
 Method static {}
0 ldc #1 String "My dog has fleas."
2 putstatic #5 Field java.lang.String blah
5 return
 
 Method StringTest()
0 aload_0
1 invokespecial #4 Method java.lang.Object()
4 return
 
 

Michael,

thanks. I stand corrected.

Arieh

 regards,
 michael
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, email: [EMAIL PROTECTED]

--
 Arieh Markel   Sun Microsystems Inc.
 Network Storage500 Eldorado Blvd. MS UBRM11-194
 e-mail: [EMAIL PROTECTED]   Broomfield, CO 80021
 Let's go Panthers  Phone: (303) 272-8547 x78547
 (e-mail me with subject SEND PUBLIC KEY to get public key)


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]