New answers tagged

-1

values is an array of String. I'm not sure what observation led you to conclude otherwise. Variadic method parameters in Java collect the arguments into an array, as you can see in the Java 5 varargs documentation. To find out the type of an object, use values.getClass().getName() (yields "[Ljava.lang.String;" indicating an array of String). You can use ...


0

This is a varargs argument, and values is treated as a String array. Java is hiding the array creation, note., but you can call that method with an array rather than just a list of arguments.


-2

values is a String. When you called toString on it, the returned value, Ljava.lang.String tells you it's type, based on the JLS Section 4.3.2. In this case, it's a String. When I tried a MCVE of code (as best as I could, given I don't know what value you're passing in), I get different results. public static void test(String... strings) { Object o = ...


3

There is a simple general strategy that helps you make this decision. Consider how the exception would be handled if it was to be thrown. So imagine your music player, user clicks stop and then stop again. Would you want to display an error message in that scenario? I haven't yet seen a player that does. Do you want the application behaviour to be any ...


2

Here is how android does it: MediaPlayer In a nutshell, stop when start has not been called is not a problem, the system stays in stopped state, but if called on a player that does not even know what it is playing, an exception is thrown, because there is no good reason for calling stop there.


1

What should be the general rule when a method is not supposed to be called in some situation, but it's execution does no harm to the program in general? I see what could be a minor contradiction in your statement that may make the answer clear to you. Why is it that the method is not supposed to be called and yet it's execution does no harm? Why is ...


11

The purpose of exceptions isn't to signal that something bad has happened; it's to signal that Something bad has happened that I don't know how to fix here that the caller, or something up the callstack from it should know how to deal with so I'm bailing out quickly, halting execution of the current code path in order to prevent damage or data corruption, ...


3

The rule is you do what the contract of your method requires. I can see multiple way to define meaningful contracts for such a stop method. It could be perfectly valid for the stop method to do absolutely nothing if the player isn't playing. In that case, you define your API by its goals. You want to transition to the stopped state, and the stop method does ...


0

I prefer designing the API in a way that makes it harder or impossible for the consumer to make mistakes. For example, instead of having MediaPlayer.play() and MediaPlayer.stop(), you could provide MediaPlayer.playToggle() which toggles between "stopped" and "playing". This way, the method is always safe to call - there's no risk of going into an illegal ...


17

There are two distinct kinds of actions one may wish to perform: Simultaneously test that something is in one state, and change it to another. Set something to a particular state, without regard for the previous state. Some contexts require one action, and some require the other. If a media player which reaches the end of content will remain in a ...


5

Look at it this way: If the client calls Stop() when the player isn't playing, then Stop() is automatically successful because the player is currently in the stopped state.


11

There is no general rule. In this specific case, the intention of the user of your API is to stop the player from playing the media. If the player is not playing the media, the MediaPlayer.stop() may do nothing and the goal of the method caller will still be achieved - the media is not playing. Throwing an exception would require the user of the API to ...


35

There is no rule. It's entirely up to how you want to make your API "feel." Personally, in a music player, I think a transition from the state Stopped to Stopped by means of the method Stop() is a perfectly valid state transition. It's not very meaningful, but it is valid. With this in mind, throwing an exception would seem pedantic and unfair. It would ...


1

What exactly do MediaPlayer.play() and MediaPlayer.stop() do?-- are they event listeners for user input or are they actually methods which start some sort of media stream on the system? If all they are are listeners for user input, then it's perfectly reasonable for them to do nothing (although it would be a good idea to at least log them somewhere). ...



Top 50 recent answers are included