Hi all,
I have doubt regarding the String literal...
I come to know from java documentation
String str = "abc"; is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
Whether , String str = "abc" is compiler specific code...so whenever
the compile encouter this code..it automatically converts to char
data[] = {'a', 'b', 'c'};
String str = new String(data);
Can i have my own class to have this function...say i have class
called Hello,
So that Hello h = "Hi"
can it be able to convert it
char data[] = {'H', '1'};
Hello h = new Hello(data);
How can do this programmatically
Regards
Moin
Michael Rauscher - 22 Feb 2007 07:32 GMT
> So that Hello h = "Hi"
>
[quoted text clipped - 4 lines]
>
> How can do this programmatically
It's impossible.
Bye
Michael
Proton Projects - Moin - 22 Feb 2007 08:01 GMT
> > So that Hello h = "Hi"
>
[quoted text clipped - 9 lines]
> Bye
> Michael
Hi,
Thanks for your reply.
My Questions are:
1. Whether the string literals conversion is taken care by the
Compiler ?
2. If not, How we can do it programatically?
3. How the String class able to achieve this?
Thanks
Moin
Esmond Pitt - 22 Feb 2007 09:39 GMT
> 1. Whether the string literals conversion is taken care by the
> Compiler ?
Yes.
> 2. If not, How we can do it programatically?
Because of the answer to (1), this question is moot.
> 3. How the String class able to achieve this?
The compiler takes care of it.
Proton Projects - Moin - 22 Feb 2007 10:13 GMT
On Feb 22, 2:39 pm, Esmond Pitt <esmond.p...@nospam.bigpond.com>
wrote:
> > 1. Whether the string literals conversion is taken care by the
> > Compiler ?
[quoted text clipped - 8 lines]
>
> The compiler takes care of it.
Hi,
Thanks for your reply....May i know what's the base purpose of this
functionality ....Why they have made two way of initialisation of a
String....
String s = "Hello"
String s1= new String("Hello")
Please dont explain me the difference between the two
initialisation....I want to know Why they have made like this....Any
reasons please
Regards
Moin
Chris Dollin - 22 Feb 2007 10:30 GMT
> On Feb 22, 2:39 pm, Esmond Pitt <esmond.p...@nospam.bigpond.com>
> wrote:
[quoted text clipped - 14 lines]
> Thanks for your reply....May i know what's the base purpose of this
> functionality
So that equal string literals save space, and so that literals
are included in the interned string table.
> ....Why they have made two way of initialisation of a
> String....
[quoted text clipped - 5 lines]
> initialisation....I want to know Why they have made like this....Any
> reasons please
The string literals are there for the programmers comfort and
convenience (because otherwise you'd have mad stuff like
System.out.println( new String( new char[]{'h', 'e', 'l', 'l'} ) );
). `String s = "Hello";` is just a use of a string literal as
an initialisation expression. The string /constructor/ String(String)
is there because it makes a /copy/ of the argument string and
exactly enough of its underlying character array: consider
String small = someVeryLargeString.substring( 10, 20 );
which will hold on to the entire someVeryLargeString, even though
it represents only a few characters. But then
String trulySmall = new String( small );
is a string which has an underlying array only long enough
to hold the slice. If that allows someVeryLargeString to be
GCd, that can be important.
The initialisation:
String s = new String( "Hello" );
is a more-or-less pointless combination of those: it makes
a copy of the interned literal string. I can't think of a
common reason why one would bother.

Signature
Chris "electric hedgehog" Dollin
"Based on their behaviour so far -- I have no idea" /Sahara/
Michael Rauscher - 22 Feb 2007 10:36 GMT
Proton Projects - Moin schrieb:
> On Feb 22, 2:39 pm, Esmond Pitt <esmond.p...@nospam.bigpond.com>
> wrote:
[quoted text clipped - 17 lines]
> String s = "Hello"
> String s1= new String("Hello")
In fact, these two statements are different. The first let s reference
an interned String. The second creates a new String object.
So the following is true:
s1 != "Hello" && s == "Hello" && s1.equals(s)
Please read Roedy Green's explanation of interned Strings[1].
Bye
Michael
[1] http://mindprod.com/jgloss/interned.html
Thomas Schodt - 22 Feb 2007 13:50 GMT
> I come to know from java documentation
> String str = "abc"; is equivalent to:
[quoted text clipped - 6 lines]
> data[] = {'a', 'b', 'c'};
> String str = new String(data);
<quote>
Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
</quote>
All this says is; in both cases the String reference 'str'
ends up referencing a Java String with a value of "abc".
String literals are stored in the class file.
When the class is loaded the String literals are loaded as well.
Indeed because of the cumbersome way Java loads arrays of literals
you often see things like;
class Small {
static char[] data = "abcdefghijklmnopqrstuvwxyz".toCharArray();
}
instead of
class Cumbersome {
static char[] data = {
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'};
}
Try compiling these and use 'javap -c' to investigate the class files.
Mark Rafn - 22 Feb 2007 16:23 GMT
>I come to know from java documentation
>String str = "abc"; is equivalent to:
>char data[] = {'a', 'b', 'c'};
>String str = new String(data);
It's similar, but not actually equivalent. "abc" is a string literal that is
a different object than a new string from the char[] literal you create with
the contstructor call. Prove it with:
char[] chars1 = { 'a', 'b', 'c' };
String s1 = new String(chars1);
char[] chars2 = { 'a', 'b', 'c' };
String s2 = new String(chars2);
System.out.println("fromarray equals: " + s1.equals(s2));
System.out.println("fromarray same: " + (s1 == s2));
String s1 = "abc";
String s2 = "abc";
System.out.println("literal equals: " + s1.equals(s2));
System.out.println("literal same: " + (s1 == s2));
>Whether , String str = "abc" is compiler specific code...so whenever
>the compile encouter this code..it automatically converts to char
>data[] = {'a', 'b', 'c'};
>String str = new String(data);
No. "abc" is a literal. 7 is a literal. {'a', 'b', 'c'} is a literal.
They're created as objects directly, _NOT_ by assigning them to a variable.
>So that Hello h = "Hi"
Only if Hello extends String. But it's final, so you can't. "Hi" is a
String object. You can't assign it to a variable that isn't compatible with a
String.
>can it be able to convert it
>char data[] = {'H', '1'};
>Hello h = new Hello(data);
THIS you can do. Just have Hello with a constructor that takes char[].
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>