Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Quicker equality check?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Thanked 0 Times in 0 Posts

    Default Quicker equality check?

    I have an if statement and the entry condition requires the checking equality of a character against several other characters. Is it possible to put these several characters into a list to shorten the code? At the moment I have

    if (str.charAt(i) != '*' && str.charAt(i) != '/' && str.charAt(i) != '{' && str.charAt(i) != '}'...
    && str.charAt(i) != '\t' && str.charAt(i) != ' ' ){
    				lineHolder += str.charAt(i);

    Clearly this is an inefficient way of doing things..
    Any ideas?
    Last edited by Shaybay92; March 9th, 2012 at 09:32 PM. Reason: Realised the expression was wrong.. but fixed it now.

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Thanked 206 Times in 182 Posts

    Default Re: Quicker equality check?

    A couple of caveats: shorter code != quicker execution as your question presupposes. Secondly there is quicker and quicker: the hearts of people at the base of a mountain beat more quickly than those at the top (and their lives are correspondingly shortened). It's a general relativistic effect and it is simply *not* worth worrying about (unless your clocks are launched into space as part of a military grade GPS system in which case the clock speed in a gravitational field matters). The time savings you are chasing are, similarly, more theoretical than real.

    There is a String method that will test whether a character is present in a String and that could be used here. The resulting code would be more readable and that matters more than speed.

Similar Threads

  1. Check OS
    By andry2181 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 7th, 2012, 09:57 AM
  2. Can Somebody check this code?
    By manager00104 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: January 30th, 2012, 10:27 AM
  3. Problem with equality check with HashSet
    By jmegha in forum Member Introductions
    Replies: 2
    Last Post: June 15th, 2011, 07:28 AM
  4. how to check the value
    By javaking in forum Java Servlet
    Replies: 2
    Last Post: July 22nd, 2010, 06:56 AM
  5. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM