It's not a secret anymore!
The moose likes Beginning Java and the fly likes differentiate  plz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "differentiate  plz" Watch "differentiate  plz" New topic

differentiate plz

Asif khan

Joined: Oct 04, 2001
Posts: 1
What is the difference between :
String ss = new String("tiger");
String ss = "tiger"

What is the purpose of "intern()" method in String class
Umair Uddin

Joined: Sep 12, 2001
Posts: 14
both think have done the same job ... they both make object of String class.. but the first one is locating the new space for object in memory ..and second dont..
take a look at it..for second
String s="tiger";
String a="tiger";
the second line is not creating a new object of tiger.. the object refrence (a) points the same object "tiger" that is pointed by (s)..
and in case
String s="tiger";
String a=new ("tiger");
the second line is creating a new object in memory for tiger object..
i think you will pick it...
Umair Uddin Qa dd'rouy
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Strings are allowed to bend the rules. Two reasons for this are:
1) object creation is relatively expensive, and String objects pretty widely used. We save a *lot* of time by allowing String objects to hold values without dynamically allocating memory for them
2) Some String objects (like properties and input parameters) will hang around for the life of the program, so it makes sense to declare space for them at compile-time.
intern() makes it possible to store a String's contents for reuse in a subsequent assignment.
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Sohrab Khan Niazi

Joined: Oct 05, 2001
Posts: 3
there are different types of data types in java. and in array we also used different types datatypes like int char and etc.. ; but when we want to use character type dat we can use only specific data but when we use string we can give many characters. there are two things when we store data in the memory and there are two methods to initialize our string. one is string and other is string buffer. there is some differecnce b/w these two methods. when we used " new " keyowrd it will give a specific memory location it will create an object and give a specific index no. we can't inter change the values b/w strings.but when we use stringbuffer then it will not create an object we can give an other index no. to it.
it is the main difference between string and string buffer.
for urs i am declearation these both as
string a= new string{"a;jf asfkja askjfa"};
string b= "jsdfkl;aj";
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
Originally posted by Michael Ernest:

intern() makes it possible to store a String's contents for reuse in a subsequent assignment.

To add to Michael's explanation, the reason that Strings can be reused is that Strings are constant; their values cannot be changed after they are created.
Marium Hasan

Joined: Oct 03, 2001
Posts: 12

The String created through new Operator like this:
String ss = new String{"tiger");
are created on Heap Memory area ,while
String ss = "tiger";
created are on Constant Pool memory area.
As any operation is applied on the later one, like:
ss += "Animal";
The STring going to transfer to Heap area.
" the basic difference b/w is the Memory area"
intern(): this method when applied to first of the STring
it will tranfer itself from Heap to Constant POOL memory.
I hope this has helped U. Any difficulty ask again i'll try to help.

wood burning stoves
subject: differentiate plz
It's not a secret anymore!