Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

string primitive

Hi,

Is String is primitive or object. I am confused to see it as primitive.
Please advise

 Any links resources ideas highly appreciated. Thanks in advance
0
gudii9
Asked:
gudii9
  • 11
  • 3
  • 3
  • +1
3 Solutions
 
krakatoaCommented:
It's an Object.

What makes you think otherwise?
0
 
krakatoaCommented:
Of course it's "made" of primitives - chars.
0
 
gudii9Author Commented:
Table in this link lists String also under  heading of
Primitive Data Types

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

what is difference between String and String Literal
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
dpearsonCommented:
A String is a variable:

e.g. "String x"

You can store a value in a String.
x = y ;
x = "hello" ;
x = "goodbye" ;
x = x + x ;

A String Literal is a value, e.g. "Hello World".

It's the same relationship between Integer and 10.

Integer a ; // This is a variable
println(10) ; // 10 is an integer constant
Integer a = 10 ; // Assigning the integer constant 10 to a variable called a

Doug
0
 
krakatoaCommented:
"The eight primitive data types supported by the Java programming language are:"

- that's what it says further up that page. But you are right, the table does include the String, (object), and it goes on to explain it thusly:


"The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such. You'll learn more about the String class in Simple Data Objects"

I've read that page in the past many times, and I'd say,personally, that I have big doubts as to how helpful that explanation is about String. Because unless you unpick a String by calling .charSequence() on it, then you will always be working with an Object.
0
 
krakatoaCommented:
The clue is that the primitives are all fixed length. A String can be a variable length - (up to several billion chars).
0
 
gudii9Author Commented:
Integer a = 10 ; // Assigning the integer constant 10 to a variable called a


Integer is wrapper object of int right.

can we assign primitive 10(int) value to it? Please advise



"The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such.

what special support makes it think as primitive.


Because unless you unpick a String by calling .charSequence() on it, then you will always be working with an Object.

Can you please elaborate on this. I am not clear on above sentence
0
 
dpearsonCommented:
Integer is wrapper object of int right.

Yes that's right.

can we assign primitive 10(int) value to it? Please advise

Yes we can.  When you write:

Integer x = 10 ;

this is effectively converted to:

Integer x = new Integer(10) ;

Your other questions are for the comment krakatoa posted, so I'll leave him to respond to those.

Doug
0
 
krakatoaCommented:
String (although an Object) can be allocated to a value just by declaring : String s = "Howdy"; in a similar way to declaring say an int - int i = 10;

(whereas usually for an Object you have to use "new Object ()")

And, with a String, you can concatenate using the "+" operator to make a new one. You can't do that with regular Objects.

Because a String is immutable, the only way to get at its components - chars - is to obtain a char or Charsequence if you need that. The original String object still persists.
0
 
gudii9Author Commented:
Because a String is immutable, the only way to get at its components - chars - is to obtain a char or Charsequence if you need that. The original String object still persists.

How immutability related to getting to its components chars. I am not clear on this point. Please advise
0
 
krakatoaCommented:
How immutability related to getting to its components chars. I am not clear on this point. Please advise

They are not related as such - it just means that a String, once defined, stays how it was when it was made. If you want to get individual characters from it, (for whatever reason) then you can obtain discrete chars, that's about it.
0
 
awking00Commented:
>>Table in this link lists String also under  heading of
Primitive Data Types<<
Actually, it lists String(or any object) as a data type, but doesn't include String in the definitions of primitive data types.
As for immutability, try this little test.
String s = "Java";
s.concat(" is fun");
sysout_println(s);

The console output is
Java

and not Java is fun.
0
 
krakatoaCommented:
doesn't include String in the definitions of primitive data types.

. . . is because String is *not* a primitive, just quasi supported like one, as the docs state.

@awking00

Presumably your comment was directed toward mine/ me; and if so I did not mention "concat".

I said that String s "+" String t would equal String st.

concat works differently, and you have to apply it thusly :

"Java".concat(" is fun.");

The console output is
Java is fun.
0
 
krakatoaCommented:
. . . meaning of course that it can be done this way if you wish:

s.toString().concat(new String(" is fun."))

Open in new window

0
 
awking00Commented:
krakatoa,
My response wasn't really directed to you, but trying to provide an example of how strings are immutable.
The idea of "re-assigning" a new value to a string (i.e. making a new String as in your example) can also be used to circumvent the immutablity feature of Strings -
String s = "Java"
s = s.concat(" is fun");
0
 
krakatoaCommented:
can also be used to circumvent the immutablity feature of Strings -

OK thanks for the clarification of comment;

The concat, sorry, makes a NEW String.

(IOW s (in the form of "Java", doesn't exist any more, only in the form of "Java is fun." does.)
0
 
awking00Commented:
That's what I was trying to say that once it's "re-assigned", it becomes a new string and your statement that the String "Java" no longer exists further clarifies that the immutability can not really be circumvented. Thanks.
0
 
krakatoaCommented:
You say this most recently:

further clarifies that the immutability can not really be circumvented.

but earlier on you said this:

can also be used to circumvent the immutablity feature of Strings -

I'm not sure what you mean or meant, but Strings are

a) Objects
b) immutable
c) and have some resemblance to primitives in certain, narrow terms.
0
 
krakatoaCommented:
class St{

public static void main(String[] args){

	String s = "Java";

	s.concat(" is fun.");

	//the next line simply prints "Java"
	System.out.println(s);

	s = s.concat(" is fun.");

	//the next line prints "Java is fun."
	System.out.println(s);

	//the next line prints "Java is fun. is fun."
	System.out.println(s.concat(new String(new char[]{' ','i','s',' ','f','u','n','.'})));

	//the next line prints "Java is fun." s is NOT equal to "Java is fun. is fun.", but IS still equal to "Java is fun."
	// because s has not been re-allocated to a new String, as it was first time 'round.
	System.out.println(s);

	CharSequence c =s.subSequence(0,s.length());

	// this line prints out String s as a sequence of chars
	for (int a=0;a<c.length();a++){System.out.println(c.charAt(a));}


	// this line prints a concatenation of "Java is fun." and " is fun."
	System.out.println(s.concat(new String(new char[]{' ','i','s',' ','f','u','n','.'})));

	// this line prints "Java is fun." again.
	System.out.println(s = new String(s));

	// this prints "Java is fun. is fun."
	System.out.println(s.concat(" is fun."));

	//BUT

	s.concat(" is fun.");

	System.out.println(s);

	//has just printed only "Java is fun."

       //WHEREAS THIS

	s = s + " is fun.";

	System.out.println(s);

	//prints "Java is fun. is fun."


}


}

Open in new window


Have fun! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 11
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now