Brain Spout

Wizard No More
How do you simply state a class in java? im not sure how to explain what i mean so ill post the code and elaborate: (keep in mind that that ArrayList is properly imported and everything else to make the program work is good)

ArrayList d = new ArrayList();
d.add(new String("susanna"));

Object u = d.get(0);
String oohyeh = new String("ASSASASASAS");

if (u.getClass() == oohyeh.getClass())
System.out.println ("IN IF");
String ue = (String) u;
System.out.println (ue);

okay, i tried to instead of what i did say u.getClass() == String, whcih didn't work. I had no idea what to try from there so i tried == (class String). which ofcourse didnt work. im wondering if there is anyway to just have string there without having to declare the variable type i want to be compared to. im writing a program where different types of variables can be input in the same way, but slightly different things are done to each kind.


Brain Spout

Wizard No More
i thought someone here knew java. or is the question that hard and obscure. or is it impossible?

if anyone knows anything about this i got another question as well, related to this and about inheretance. it is:
i have a class rectangle, and then a class square that extends rectangle. if i make a square object and then set up the .getClass() on the square object and say == (Psudo Code - rectangle class) will it be true?


Banned - What an Asshat!
There are several people that know how to do it, I'm just not one of them, give it a little bit and then come back, i'm sure Jung, or someone else would help you with it.

Brain Spout

Wizard No More
im assuming that no one knows java, or no one knows the answer to this problem. either way with some experimentation i was able to find the answer to my problem. using the 'class' class:
there are several ways that one can have a class object to compare to another class object. the specific one i was looking for is forName(String name). a static method that returns the a class object. there are also a lot of other good methods that will help you if you are desinging your software without using a lot of premade code.


Might want to ask BRiT, he's a Java developer. I'm admittedly horrible at java (mostly because I think it's a worthless language), and as such I avoid it. :D

Edit: I see you've answered your own question though.


Hey there, just saw this thread tonight, but it seems you found your own solution.

For something this simplistic use the following "instanceof" operator.

if (u instanceOf String) {
System.out.println("IN IF");
final String ue = (String)u;

Ideally, you could use the Class.isAssignableFrom method. This works for multiple levels of inheritence of interfaces and classes.

given "class A", "class B extends A", "class C extends B"

A objA = new A();
B objB = new B();
C objC = new C();

a.getClass().isAssignableFrom(c.getClass()); // TRUE
b.getClass().isAssignableFrom(c.getClass()); // TRUE
c.getClass().isAssignableFrom(c.getClass()); // TRUE
a.getClass().isAssignableFrom(b.getClass()); // FALSE
b.getClass().isAssignableFrom(c.getClass()); // FALSE

Oh, and btw, do not do the following: String a = new String("This is the string"); At compilation time, this creates 2 string objects as literals are converted into string objects by the compiler. The prefered and optimal means is simply: String a = "This is the String";

// The following creates 4 unique string objects in memory and is wasteful
String s1 = new String("A String");
String s2 = new String("A String");
String s3 = new String("A String");
String s4 = new String("A String");

// The following creates 1 unique string object in memory and has 4 pointers to it
String s1 = "A String";
String s2 = "A String";
String s3 = "A String";
String s4 = "A String";

Brain Spout

Wizard No More
thankyou for the reply very insightful. when testing out how things work i like to make all my variables have seperate locations in memory because my program may run into a situation where two strings (or variables) that contain the same data have two different references. when testing i want to make sure i have taken that into account.