File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Strings"

 
Khalid Bou-Rabee
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are all "String" literals associated with a String object reference?
Thanks,
Khalid
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Khalid Bou-Rabee
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the lack of clarity in my previous post. I'm taciturn at times
Khalid
 
Ankur Gupta
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khalid,
"4" is correct because "99" becomes a String and thereby an Object !!
Ankur
 
Khalid Bou-Rabee
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AHA! Thank you
I can see again, it's a miracle!
-Khalid
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic