This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""Strings"" Watch ""Strings"" New topic
Author

"Strings"

Khalid Bou-Rabee
Ranch Hand

Joined: Jul 12, 2000
Posts: 54
Are all "String" literals associated with a String object reference?
Thanks,
Khalid


\\ //<BR>~\// irucidal~
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Khalid,
I am not sure if I read your question right.
Anonymous strings can exist without any references. For example, consider the statement
System.out.println("Hello"+"World");
Here, two strings are created internally in the string pool, but they will have no way to refer to them again after the statement.
Is this what you were asking? If not, please ask clearly.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Khalid Bou-Rabee
Ranch Hand

Joined: Jul 12, 2000
Posts: 54
In Marcus Green's applet exam he has a question:
---Number:47---
Which of the following is[sic] will sucessfully[sic] create an instance of the Vector class and add an element;[sic]
1) Vector v = new Vector(99);
v[1]=99;
2) Vector v= new Vector();
v.addElement(99);
3) Vector v=new Vector();
v.add(99);
4) Vector v = new Vector(100);
v.addElement("99");
The answer given is 4
The addElement function takes Object as a parameter. So I'm assuming that all string literals "" are automatically 4(implicitly)paired with a String reference object. Otherwise shouldn't two and three be valid answers as well?
Thanks again,
Khalid
[This message has been edited by Khalid Bou-Rabee (edited July 17, 2000).]
Khalid Bou-Rabee
Ranch Hand

Joined: Jul 12, 2000
Posts: 54
Sorry for the lack of clarity in my previous post. I'm taciturn at times
Khalid
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
2 and 3 can't be right answers because :
To a vector u can only add Objects - in "2" an int is being added which is not allowed.
There is no add(int i) method for Vector class. You can add only using addElement() - in "3" the method being used is "add(int i)" you could use add(int i, Object o) or add(Object o).
Ankur
[This message has been edited by Ankur (edited July 17, 2000).]
Khalid Bou-Rabee
Ranch Hand

Joined: Jul 12, 2000
Posts: 54
Khalid {{ so why is 4 correct? }}

recall:
---Java 2 Platform Specs:Vector Class---
<pre>
addElement
public void addElement(Object obj)
Adds the specified component to the end of this vector, increasing its size by
one. The capacity of this vector is increased if its size becomes greater than its
capacity.
This method is identical in functionality to the add(Object) method (which is
part of the List interface).
Parameters:
obj - the component to be added.
See Also:
add(Object), List
</pre>
[This message has been edited by Khalid Bou-Rabee (edited July 17, 2000).]
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
Khalid,
"4" is correct because "99" becomes a String and thereby an Object !!
Ankur
Khalid Bou-Rabee
Ranch Hand

Joined: Jul 12, 2000
Posts: 54
AHA! Thank you
I can see again, it's a miracle!
-Khalid
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: "Strings"
 
Similar Threads
date format with MS Access database
How to convert byte array into string array?
Tech Word Game
WA #1.....word association
ResultSet to Array