1

So I'm trying to run this simple program here:

import java.util.*;

class StackDemo
{
    public static void main(String[] args) {
        Stack s = new Stack();
        s.push(5);
        s.push("dog");
        System.out.print(s);
    }
}

StackDemo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Process completed.

It displays the expected result, which is "[5, dog]" but I don't understand that message on the Build Output window. What could possibly be wrong here?

Mureinik
  • 252,575
  • 45
  • 248
  • 283
user3026693
  • 133
  • 2
  • 11
  • So you want to put integer and String types in the Stack? Is there any relation between `5` and `"dog"`? – Rohit Jain Jan 16 '14 at 09:05
  • Consider to use Deque instead of Stack. From Javadoc: "A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class." – Puce Jan 16 '14 at 09:07
  • 1
    Did you try compiling with `-Xlint:unchecked` to get more details? – Jon Skeet Jan 16 '14 at 09:08
  • 3
    It's because you are not using generics. To get rid of the warning you would need to declare your stack like `Stack s = new Stack<>();`. But mixing animals and arithmetics is a weird idea. – assylias Jan 16 '14 at 09:08
  • @Puce: No, that's irrelevant. This is about the OP using a raw type instead of generics. – Jon Skeet Jan 16 '14 at 09:08
  • 3
    @JonSkeet It's a general recommendation (according to the Javadoc) and just a comment not an answer. – Puce Jan 16 '14 at 09:10
  • 1
    @Puce: But I don't think it's actually helpful at this point. If you're at the stage of not understanding generics, the benefits of Deque vs Stack aren't really the primary concern, IMO. – Jon Skeet Jan 16 '14 at 09:10
  • This [link](http://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning) will help – Keerthivasan Jan 16 '14 at 09:12
  • 1
    @JonSkeet My point is that especially if you're new it's best to work and learn with the newer classes instead of legacy classes, which you're most likely not going to use later anyway. – Puce Jan 16 '14 at 09:12
  • 2
    @Puce: I think it would *at least* have been worth explicitly stating that your comment had absolutely nothing to do with the problem the user is facing. – Jon Skeet Jan 16 '14 at 09:13
  • @RohitJain There isn't any relation between 5 and "dog" obviously, but I was actually trying to build a palindrome program and I was getting that error, so I tried to build a simpler program, thus shown above, and I got the same error, too. – user3026693 Jan 16 '14 at 09:14
  • @assylias thanks! it worked. :) and yes i know this is just a sample program so I just typed in random stuff :) My instructor never told me about this, just straight on asked me to create a program using stacks so my apologies. Thanks for the help – user3026693 Jan 16 '14 at 09:18
  • @user3026693 You should use the most specific type. So if your stack contains Strings only, you should use a `Stack` – assylias Jan 16 '14 at 10:00
  • @assylias Yeah, I figured it out quickly, I'll keep this in mind. Thanks again – user3026693 Jan 16 '14 at 10:05

2 Answers2

5

Stack is a generic class, which you can use, if you wish, to store objects of a specific type (e.g., Stack<String> would be used to store strings). Using bare classes, without a type specifier is usually considered a bad practice, as you're losing the type safety of the collection.

If there is indeed a usecase where you'd like to store both 5 and "dog" in your stack, you should define your stack with the greatest common denominator between the two - Object:

Stack<Object> s = new Stack<>();
RainMaker
  • 41,717
  • 11
  • 78
  • 97
Mureinik
  • 252,575
  • 45
  • 248
  • 283
2

Stack is a legacy class but the warning is because you haven't use generics and the compiler cannot check whether you have added the right types or not.

Given you have mixed the types, in a real program I would hope this is bug.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075