What is assertion really good for?

Yes, I read Programming With Assertions but still can't find a situation where I can use assertions. When I want to check I value, I use an if-statement or a special exception because I want to react for such a specific case in a specific manner. If an assertion fails, the JVM just throws an error. An error is something very dangerous but if an assertion is not true, the application should handle this and continue with controlled statements (e.g. error message but continue with next object).
What real life situations do you use assertions for?

What real life situations do you use assertions for?That's a tricky one. If by "real life" you mean "production environment" then the answer is "none".
An assertion should never ever happen in production code. It's a check for a condition that you think is impossible to meet. A quick example:
public class MyClass {
  private Foo foo;
  public MyClass(final Foo foo) {
    if (foo == null) {
      throw new IllegalArgumentException(foo);
    this.foo = foo;
  public void frobnicate() {
    assert foo != null;
    foo.frobnicate();
}That code looks harmless enough and (if I didn't make any incredibly stupid mistake and noone is fiddling with evil reflection tricks) the assert should never fail.
Now if someone added a default constructor to that class (for example to make it serializable) and forgot to make sure that each use correctly initialized foo then your assert would fail during development).
(All that is IMHO of course).

Similar Messages

  • What download speed is good for streaming HD movies?

    What download speed is good for streaming HD movies down to my Apple TV?

    kenjpark
    The recommended speeds are
    SD 2.5 Mbps
    720p 6 Mbps
    1080p 8 Mbps
    Jules

  • WHAT MS OFFICE IS GOOD FOR IPAD 2

    What MS OFFICE is good for ipad 2

    There is no iPad version of MS Office. You can use Apple's iDevice versions of Numbers, Pages, and Keynote which can read and export Office compatible documents. There are quite a few other third-party apps that do much the same such as Documents To Go.

  • What is Hashtable.clone() good for?

    I want to make a copy of a Hashtable. I read the javadoc of Hashtable.clone() and it says:
    "Creates a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned. This is a relatively expensive operation. "
    What does this mean? I want my copy to be de-coupled from the original, i.e. when i change anything in my copy, the original should be untouched. But the sentence "the keys and values are not cloned" seems to result in a coupled copy. So I have to copy all keys/values into a new Hashtable object myself. Leading to the question: what is Hashtable.clone() good for?

    I prefer this to ad hoc speed testing classes.
    import java.util.*;
    public class SpeedTester{
        public static final long OUTER_ITERATIONS = 100;
        public static final long INNER_ITERATIONS = 10;
        public static void main(String[] args)    {
            Data[] data = {new StringData()};
            Test[] tests = {new TreeMapTest(), new HashMapTest()};
            long[] times = new long[tests.length];
            for (int j = 0; j < OUTER_ITERATIONS; j++)
                for (int k = 0; k < data.length; k++)
                    data[k].create();
                for (int k = 0; k < tests.length; k++)
                    System.gc();
                    times[k] += test(tests[k]);
            for (int j = 0; j < tests.length; j++)
                System.out.println(tests[j].getClass().getName() + ": " + times[j] + " - "
                    + ((double) times[j]) / (OUTER_ITERATIONS * INNER_ITERATIONS)
                    + " millis per test");
        public static long test(Test test)
            long start = System.currentTimeMillis();
            for (int j = 0; j < INNER_ITERATIONS; j++) test.test();
            return System.currentTimeMillis() - start;
    interface Data{
        public void create();
    interface Test{
        public void test();
    class StringData implements Data
        static Random random = new Random();
        static String[] words;
        public static String test;
        public void create()
            words = new String[random.nextInt(4990) + 10];
            for(int i=0; i < words.length; ++i) words[i] = nextWord();
        public String nextWord()
            char[] chars = new char[random.nextInt(16)];
            for (int i=0; i<chars.length; ++i)
                chars[i] = (char) random.nextInt(0x10000);
            return new String(chars);
    class TreeMapTest implements Test
        public void test()
            Map m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
            for(int i=0; i < StringData.words.length; ++i)
                m.put(StringData.words,null);
    class HashMapTest implements Test
    public void test()
    Map m = new HashMap();
    for(int i=0; i < StringData.words.length; ++i)
    m.put(StringData.words[i].toUpperCase(),null);

  • Whats the check box good for?

    Greetings!
    I'm on a Monday roll. I got bit by a context menu so far so now lets try the check box.
    I've always been under the impression that the check box was an include/exclude thing its either on or off.
    Instance I was trying to burn a cd from a playlist and it went over. So I would un-check boxes for items in the list and try to re-burn the cd. Even after clearly it telling me I had less time and space itunes was still trying to burn the 'unchecked' items.
    So .... what are the check boxes good for?? I'm sure I'm going to feel really dumb when the answer is presented.
    Thanks for your time .. on a ridiculous question!
    Dave

    *I guess I haven't put enough thought into creating a playlist to contain every song I would like to play at a given time.*
    By checking/unchecking songs, you alreay are doing it, except instead of having them in a neat, tidy playlist or two, you have the whole library to go thru.
    Here's how I do it.
    It's very simple to get new music and my favorites onto the iPod, once set it up.
    I have a few playlist I sync.
    One is a regular playlist (iPod Music) with all the music I always want on my iPod.
    I also create a smart playlist.
    The criteria are.
    Match all rules;
    Playlist is not iPod music (this keeps from selecting songs I already have)
    My rating is not 1 star (keeps off stuff I rate one star)
    Have not heard in last two weeks (rotates older music)
    Limit selection to 2GB by Random (the size is set to the iPod total size minus iPod music playlist size).
    Then I select the iPod, click the Music tab.
    Tick Sync music and choose Selected playlists in the drop down.
    Tick the two playlists above.
    I add/delete from the iPod Music playlist to always sync this music.
    Once a week or so, depending on how much I have listened to the iPod, select the smart playlist.
    Select all the songs and press Delete. Since it's a smart playlist, it will automatically fill up again with new music based on the criteria.
    Then I sync the iPod.
    (I really gotta start a blog with this info in it since I type it a bunch...)

  • Direct Access Management Servers, what are the entry good for?

    In the advanced Direct Access setup wizard you have the ability to enter your management servers. I haven't been able to find an explanation of why, what is it good for? If I understand everything correct DA gives full access to the subnet so why is it of
    interest to list some servers as "management servers"?

    Hi,
    Management servers are servers that you are able to access from the da client Before logged on as a user.
    Your domain controllers are by default infrastructure servers, but in many cases you want to add for example SCCM, NAP and other servers to be accessable prior logon.
    If you are using the manage-out functionality in DirectAccess, and want to access a client prior anyone is logged on, the management server is also needed there.
    http://technet.microsoft.com/en-us/library/jj574200.aspx
    Microsoft Certified Trainer
    MCSE: Desktop, Server, Private Cloud, Messaging
    Blog: http://365lab.net

  • What are sOme really good apps for iPhone paid or free don't care

    I need some good apps for iPhone can someone help me thx

    No.  This is not the appropriate forum.  You can go to the iTunes app store and look for some that peak your interest. 

  • What's the best type of case that really good for maintaining iPhone' temperature?

    Feel free to answer, answering means helping and it means you are not only helping me , but the other. CHILLSS

    Sorry. I've never seen a phone case designed to maintain a specific temperature.

  • Which mac is really good for hdv working process and render??

    I'll work my hdv native workflow soon and I'm really very interesting on how to get the most of Mac product,s but I'm still asking me what is the minimun of features do I need on a MAc to process without any disturb or overload and render my video in Blu ray for distribution and broadcasting purposes.

    The MacBook Pro provides one key advantage not possible with the 12-inch PB -- the ability to create a second firewire bus using the ExpressCard/34 slot.
    In this manner, you can separate fw drives from camera/deck -- a big issue with FCP5 and certain cameras and hard drives.
    It is a key difference for anyone considering video editing.
    You'll get a beefier video card, and the ability to preview.

  • Ssoooo... what's this thing good for anyway?

    So I just bought my powerbook used. This is my first mac. I may have under researched this decision. i'm now finding all sorts of guff about the powerbook g4s being "too slow" and "outdated". did I just waste a bunch of money on and obsolete machine. or is it possible to upgrade the hardware enough to salvage it for a few years?

    I think it depends on what you paid whether you wasted money or not, and what your intended use is. Certainly the Powerbook series and G4 PowerPC processor has not been used for many years now, and support for programs, security updates, and so on is waning.
    Assuming the hardware is in good condition, the only logical upgrades for this machine is to max the RAM and install a larger hard drive (both depend on which Powerbook you have).
    While I would not be happy trying to edit video or do extensive Photoshop work (too slow), I still use my PB as a secondary machine for light duty tasks such as email, web surfing (except web video -too slow), photo viewing, iTunes playback, word processing, and so on.
    Computers get better. I don't think Pentium 3 machines satisfy many PC users anymore either.

  • What FTP server is good for iWeb

    Iweb just ate 7 years of images off all my old blogs and articles. I have limited  machine back ups.
    Tech support for 3.5 hrs was useless.
    What FTP servers are iWeb users looking at to transfer to?

    Any good hosting company will give you better service and tech support than MobileMe. Make sure you get a Linux account and that support is available 24/7.
    Most offer a 30 trial and this page also has a link to free hosting which I have tested out...
    http://www.iwebformusicians.com/iWeb/Website-Hosting.html

  • What Macintosh Machine is good for AAE CS5?

    Hello people
    First I want to thank everyone for their support in this forum
    I'd like to make a question,I can afford around 2.700$ for a Good Mac Computer
    I want it for HD Video Editting with After Effects CS5 and maybe for future use of Adobe Photoshop.
    Many people told me that 24GB RAM is good,but no one told me what other parts to buy,If anyone knows please help me
    PS:Correct me if I said anything wrong above.
    Thank you all!
    ~Dan

    Many people told me that 24GB RAM is good,but no one told me what other parts to buy,If anyone knows please help me
    Just for PS and AE you won't need much more. To some degree harddrive speed and capacity for placing caches and temporary data will matter, so you should get 2 or 3 drives, assuming we're talking Mac Pro and not iMac. Graphics won't matter much. Choice is limited anyway and neither AE nor PS are particulalrly dependent on OpenGL...
    Mylenium

  • What does 7.3 good for

    I don't know if I should upgraded or not. What's the difference?

    Don't yet. Wait for the bugs to be worked out. I always give iTunes updates a good month or two before actually upgrading. Though you could check the discussion boards here and see what people are having troubles with if you'd like to form your own opinion.

  • What does Videora do for me? What is this software good for?

    I am still having an issue with my export having no audio when i go to listen or watch it in my ipod? Someone please help????????????PLEASEEEEEEEEEEEEEEEEE
    My head continues to hurt because of this issue.
    Dee

    As far as I know, there are no known security risks with it (at least no widely reported cases). I've used it for about three years, and have had no security problems with it. The UNIX programmer folks on here will have more specific technical information, but from a user standpoint it's perfectly safe to use. In fact, if you add other user accounts to your own computer, the other users can't even attempt to access it. It's pretty neat...
    PowerBook G4 12 1.5GHz and a bag full of goodies!   Mac OS X (10.4.4)   80GB HD, 512 MB ram, 300GB Maxtor OneTouch II FireWire 400 External HD.

  • What is a really good and affordable HD Camcorder?

    I am looking for a Christmas present for my mother. She has always wanted a nice camcorder and she wants a HD one. She would love to either be able to record it right to a DVD or be able to put it on her computer and convert it to a DVD. So does anyone have a good suggestion? I don't know much about camcorders.

    Well, for camcorders, HD actually has two possible definitions:
    Hard Drive, or
    High Definition
    If you want High Definition, it will be difficult to burn directly to DVD from the camcorder.  You'll need to convert the video (a time consuming task) to be able to burn it to DVD.  However, the AVCHD format used by most high def camcorders can be burned to a Blu-Ray disk merely by renaming some of the files.  You can also burn it to a "mini-BluRay" which is basically a Blu-Ray file structure on DVD media and play it in a Blu-Ray player.
    If you want Hard Drive - I strongly recommend rethinking this.  Hard drive based camcorders are far less resistant to shock damage than flash memory based camcorders.
    I personally feel that Canon's Vixia HF series are your best bet for flash memory high definition units.  I have an HF100 (older model, 2 generations old) and love it.
    *disclaimer* I am not now, nor have I ever been, an employee of Best Buy, Geek Squad, nor of any of their affiliate, parent, or subsidiary companies.

Maybe you are looking for