Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrapping primitve data types

Status
Not open for further replies.

Diancecht

Programmer
Jan 8, 2004
4,042
ES
Hi.

I was jumping around one of my strange ideas and I was wondering if anyone has any thoughts about this.

The idea is that I have an abstract data type called Data. That data can hold any kind of primitive type: int, char, boolean and so on. I have an estabilsh method for each data type, let's say setValueAsChar, setValueAsInt and so on ...

I was wondering if there was any way to include this in a single method. This is possible with class tupes, that can be encapsulated as objects, but didn't find a way for primitive.

The objective is not to write a bunch of methods with the same code for each functionality on the data type.

Cheers,

Dian
 
Can you use instanceOf in conjunction with the Wrapper classes? (sorry no way to test here at work)


Keith


The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents.
 
Hi Keith, thanks for your reply.

Yes, I can, but that works for Objects, not primitive data types.

You can write a method set(Object) and inside use the instanceOf thingie, but I'm lookig for a way to do that with primitive data types.

Cheers,

Dian
 
I wonder if there's some way to do it using the "+" operator at compile time?

For instance, if you do

Code:
int i;
long t;
boolean b;
System.err.println("Error" + i + t + b);

I assume that at compile time the compiler takes those primitives and converts them to Strings using String.valueOf() or something. If you did something similar, your object would them store the data as a String. Since you can't overload the return type, you'd still have to provide separate getters to retrieve the data in the desired primitive form anyway (ie: getDataAsInt(), etc.), so you could convert the data back at that point.

Of course, I don't know yet how to write a method that allows for the "+" sign ...
 
Hi idarke, thanks for you reply.

The problem with that approach is that String.valueOf() has a bunch of implementations, one for each primitive type, and that's exactly what I'm trying to avoid.

Cheers,

Dian
 
In java1.5, you may do such ugly things:
Code:
	void foo (Object o)
	{
		try
		{
			int i = (Integer) o;
			System.out.println ("int found!");
		}
		catch (ClassCastException expected)
		{
			// intentionally left blank!
		}
		try
		{
			boolean b = (Boolean) o;
			System.out.println ("boolean found!");
		}
		catch (ClassCastException expected)
		{
			// intentionally left blank!
		}
		try
		{
			char c = (Character) o;
			System.out.println ("char found!");
		}
		catch (ClassCastException expected)
		{
			// intentionally left blank!
		}
	}

	/** */
	public Primitiv ()
	{
		foo (true);
		foo (17);
		foo ('j');
	}

ugly, because it would be legal, to call foo (Object o) like this:

Code:
JTable jt = new JTable (myModel);
foo (jt);
which bypasses the Java-type-safety concept.

Why do you need such functionality?


seeking a job as java-programmer in Berlin:
 
Hi stefan, thanks for your reply.

So Tiger accepts casts to primitive data types and treats them as objects. That sounds interesting. Pity that I'm still bound to java 1.4 and woth compatibility to Java 1.3.

Well, I don't exactly need the functionality. I've managed to get this done by writing a method for each primitive type, but that means some repeated code and big interfaces that I don't like in my programs.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top