Solved

string primitive

Posted on 2014-02-21
19
282 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 16

Expert Comment

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

What makes you think otherwise?
0
 
LVL 16

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
 
LVL 26

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 16

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 16

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 26

Assisted Solution

by:dpearson
dpearson earned 100 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 16

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 16

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 31

Assisted Solution

by:awking00
awking00 earned 100 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 16

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 16

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 31

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 16

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 31

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 16

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 16

Accepted Solution

by:
krakatoa earned 300 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now