0

I have been trying to simulate Stack using java classes. here go my class constructor:

        public Stack(Class<?> type){

        if(type==String.class){

                //...

            }

            switch(type){

                    case (String.class):
                            //...
                    break;
                    case (Integer.class):
                            //...
                    break;

                    case (Double.class):
                            //...
                    break;

                    case (Byte.class):
                            //...
                    break;

                    default:
                    break;

            }

            this.counter = -1;

        }

but confusingly for me, the if block works fine. But with the switch/case block it doesn't compile.

error says

incompatible types
                switch(type){
                       ^
  required: int
  found:    Class

And

error: constant expression required
case (String.class):

this repeats for all cases in the switch block.

Please kindly point me whether if anything missing here.

Dan Jay
  • 874
  • 10
  • 27
  • Wait... The compiler is clear enough: `switch` expects an `int` but you're passing a `Class` object – fge May 19 '14 at 05:44
  • 2
    *Please kindly point me whether if anything missing here*. Sure: [Java Tutorials. The switch Statement.](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) – Luiggi Mendoza May 19 '14 at 05:44
  • 1
    Apart from the wrong usage of `switch` statement here, may I ask why to have such odd design and not use generics? – Luiggi Mendoza May 19 '14 at 05:46
  • @Luiggi Mendoza : Thanks for the guidance. We just started the Data structures and algorithms subject. Thought its good to try different approaches ;) – Dan Jay May 19 '14 at 05:52
  • 2
    @shan it is good trying different approaches, but this approach leads to hard to maintain code and not-useful-to-be-prepared-for-real-world-apps. Are you really sure that's what you want? – Luiggi Mendoza May 19 '14 at 05:53
  • 1
    @Luiggi Mendoza : oops... didn't thought that far though. :( – Dan Jay May 19 '14 at 05:56
  • @Luiggi Mendoza : just needed to build a class design for Stack, that could use many data types. Is there anything bad I did? – Dan Jay May 19 '14 at 06:01
  • @shan You should really check out Java's [generics](http://docs.oracle.com/javase/tutorial/java/generics/). Using generics is *significantly* better than writing a case for every class you might come across. Generics would allow you to use your `Stack` class for *any* data type (besides primitives) – awksp May 19 '14 at 06:03
  • @Luiggi Mendoza : Thank you again. Its true I'm lack of that knowledge. I must read about generics more. :) – Dan Jay May 19 '14 at 06:05
  • Posted an answer with a kick-off example to implement your stack using a single linked list and the power of generics. It will be up to you to continue implementing the other methods for a stack. – Luiggi Mendoza May 19 '14 at 06:14

5 Answers5

3

Java switch/case will work only with primitives(byte, short, char, and int), Enums(from java 5) and String(from java 7) data types only. Check oracle tutorial here

Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

You can use like following

switch(type.getName()){

  case ("java.lang.String"):
         //...
         break;
  ...
}
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

Switch-case works with primitive, Enums and String(from java 7) data type. So in your case you can use class#getName which return string and if you are using java 7.

switch(type.getName()){

  case ("java.lang.String"):
         //...
         break;
  case ("java.lang.Integer"):
         //...
         break;
  ...
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

The main problem in the code is the wrong usage of the switch statement. It will only work for constant (literal or final marked variables) from primitive type byte, short, char or int. Since Java 5, it also allow constants variables of the respective wrapper classes of these primitives: Byte, Short, Character and Integer, and also support enums, and since Java 7 it supports constant String variables. This is covered in Java Tutorials. The switch Statement. So, to fix your current code, use if statements instead of switch:

//when comparing object references, use the equals method, not ==
if (String.class.equals(type)) {
    //...
} else if (Integer.class.equals(type)) {
    //...
} else ...

From your comment:

just needed to build a class design for Stack, that could use many data types. Is there anything bad I did?

The main problem in the design is that you would have to add basically any Java class and custom class in this long if list, which will make your code really ugly, awful and a nightmare even for you. To fix this, Java give us generics since Java 5. So, you could define your class like this:

class MyStack<E> {
    //implementing the stack using a generic single linked list
    class Item {
        E data;
        Item next;
    }
    private Item head;
    private Item tail;
    private int size = 0;
    public MyStack() {
    }
    public int getSize() {
        return this.size;
    }
    public void add(E e) {
        Item temp = new Item();
        temp.data = e;
        if (head == null) {
            head = temp;
        } else {
            tail.next = temp;
        }
        tail = temp;
        size++;
    }
    //add the rest of the behavior...
}

By the way, there already is a Stack class in Java Collections API, and you should not have any class with the same name of the Java JDK API. That's why in my example, the stack class is called MyStack.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

From oralce site I found this.

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

androidcodehunter
  • 21,567
  • 19
  • 47
  • 70
0

That means you can not use class type in switch. But if you need to do it for different class types just use default values.. like

...
case 1: // for string class
break;
case 2: // for integer class
break; 
... and so on
Rajesh
  • 546
  • 9
  • 29