How string literal is working

  • Thread starter Proton Projects - Moin
  • Start date
P

Proton Projects - Moin

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
 
M

Michael Rauscher

Proton said:
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

It's impossible.

Bye
Michael
 
P

Proton Projects - Moin

Proton said:
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

It's impossible.

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
 
E

Esmond Pitt

Proton said:
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.
 
P

Proton Projects - Moin

Because of the answer to (1), this question is moot.


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
 
C

Chris Dollin

Proton said:
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....

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

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.
 
M

Michael Rauscher

Proton said:
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")

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
 
T

Thomas Schodt

Proton said:
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 encounter this code..it automatically converts to char
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.
 
M

Mark Rafn

Proton Projects - Moin said:
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[].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top