Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

about the Object reference

Thread view: 
jtl.zheng - 28 Jul 2006 04:51 GMT
I have write a stack which have interfaces:

-------  public void push(Object o);
-------  public Object pop();

then I can push any type class object into the stack
and when I pop() one element I can use:

-------  Object o=pop();
-------  o.getClass();

to determine this element o belong to what class
(eg. if it's String it return "class java.lang.String")

my question is when I know this o is a String Object
how can I use its String interface
something like:

-------  Object o=pop();
-------  String s=o;      // can't compiled here
                              //  java.lang.Object, required:
java.lang.String
-------  System.out.println(s.charAt(n));    // use the String
interface

I know this o is point to a String object
how can I use this o's String interface (to get something like
o.charAt(n)) ?

Thank you very much in advance
Lionel - 28 Jul 2006 05:08 GMT
> I have write a stack which have interfaces:
>
[quoted text clipped - 24 lines]
> how can I use this o's String interface (to get something like
> o.charAt(n)) ?

String s = (String)o;

Lionel.
Martin Lansler - 28 Jul 2006 12:05 GMT
You can check if an object is of a certain type using 'instanceof'.

if (o instanceof String) {
   String s = (String) o;
}

however it would be better if you designed the Stack with generics.

/**
* A stack
* @param <T> the generic type
*/
public interface Stack<T> {
   /**
    * Push an object onto the stack
    * @param t the object
    */
   void push(T t);

   /**
    * @return pop an object from the stack
    */
   T pop();
}

A simple implementation used a linked list:

import java.util.LinkedList;

/**
* Implementation of Stack
*/
public class StackImpl<T> implements Stack<T> {
   private LinkedList<T> list = new LinkedList<T>();

   /**
    * {@inheritDoc}
    */
   public T pop() {
       return list.removeLast();
   }

   /**
    * {@inheritDoc}
    */
   public void push(T t) {
       list.addLast(t);
   }
}

Example usage:

public class StackTest {
   public static void main(String[] args) {
       Stack<String> stack = new StackImpl<String>();
       stack.push("hi");
       stack.push("joe");
       System.out.println(stack.pop());
       System.out.println(stack.pop());
   }
}

Cheers,
Martin.

>I have write a stack which have interfaces:
>
[quoted text clipped - 26 lines]
>
> Thank you very much in advance
jtl.zheng - 28 Jul 2006 14:49 GMT
----  String s = (String)o;

----  if (o instanceof String) {
----      String s = (String) o;
----  }

Thank you very much
It work out now

to Martin Lansler
I have not learn generics yet
but I will learn it soon
Thanks for your codes
: )


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.