Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Confusion with the switch & if-else in jdk7

  1. #1
    dinesh.guntha is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default Confusion with the switch & if-else in jdk7

    Hi All,
    I have the confusion with the switch string & if-esle both uses the equals() for comparison which should i use?
    If use the switch string any performance will be increased or any other usage will be their?
    I try to understand in lot of site about the switch with string but i did not understand why should i use switch instand of if-else?
    Can any one help on this topic.


    Thanks in advance.

  2. #2
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Confusion with the switch & if-else in jdk7

    I'd say use an if statement.
    If I have helped you, + rep

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Confusion with the switch & if-else in jdk7

    If use the switch string any performance will be increased or any other usage will be their?
    Other usage?

    First and foremost, don't worry your head about micro-optimization. The JRE is much, much smarter than you are, so write code in a way that makes it easy to read and maintain. For a large number of conditions, a switch is usually easier to read; you also have the benefit of the fall-through, which saves you from writing a whole lot of || (OR) in your if conditions.

    For a few conditions, a few if/else blocks may be a better and more maintainable option.

    I'd say use an if statement.
    Wary of using anything new, are we?

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Confusion with the switch & if-else in jdk7

    Quote Originally Posted by DarrylBurke View Post
    Wary of using anything new, are we?
    No, I use to use switch statements a fair bit when I was doing mediocre web design, I just prefer if statements over them, and as for constantly using "||" in if statements; that doesn't phase me at all.
    Last edited by Vampiricx3; 02-06-2012 at 12:54 AM.
    If I have helped you, + rep

Similar Threads

  1. jdk7 closed source?
    By sofralini in forum New To Java
    Replies: 1
    Last Post: 11-28-2011, 09:41 PM
  2. Object Confusion
    By Huskies in forum New To Java
    Replies: 13
    Last Post: 07-29-2011, 09:54 AM
  3. Please help clear up confusion
    By jim01 in forum New To Java
    Replies: 8
    Last Post: 04-17-2011, 08:55 PM
  4. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  5. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •