[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

string primitive

Posted on 2014-02-21
19
Medium Priority
?
300 Views
Last Modified: 2014-03-25
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
Comment
Question by:gudii9
  • 11
  • 3
  • 3
  • +1
19 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 39878372
It's an Object.

What makes you think otherwise?
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 39878377
Of course it's "made" of primitives - chars.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39878515
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 28

Expert Comment

by:dpearson
ID: 39878770
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39879023
"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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39879066
The clue is that the primitives are all fixed length. A String can be a variable length - (up to several billion chars).
0
 
LVL 7

Author Comment

by:gudii9
ID: 39881021
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
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 400 total points
ID: 39881243
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39881300
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
 
LVL 7

Author Comment

by:gudii9
ID: 39881486
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39881934
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
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 400 total points
ID: 39883378
>>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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39883520
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39883573
. . . 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
 
LVL 32

Expert Comment

by:awking00
ID: 39883628
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39883651
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
 
LVL 32

Expert Comment

by:awking00
ID: 39883713
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 39884116
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
 
LVL 17

Accepted Solution

by:
krakatoa earned 1200 total points
ID: 39884309
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 23 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question