10 Replies - 1568 Views - Last Post: 25 June 2013 - 08:05 AM Rate Topic: -----

#1 WINDY11965  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-June 13

what does ! mean in java?

Posted 22 June 2013 - 06:30 AM

Whats the difference in plain English in these two codes?

public class DQ1Condition1 {
    
    public static void main(String[] args) {
        char cChoice = 'Q';
        while ( cChoice != 'Q' || cChoice != 'q' ) {
            System.out.println("Condition 2");
            cChoice = 'q'; 
}
        System.out.println("Condition 1");
}
}


public class DQ2Condition2 {

  public static void main(String[] args) {
      char cChoice = 'Q';
      
      while ( !(cChoice == 'Q' || cChoice == 'q') ) {
          System.out.println("Condition 2");
          cChoice = 'q'; 
          System.out.println("Condition 1");
}
}
}


This post has been edited by macosxnerd101: 22 June 2013 - 11:46 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: what does ! mean in java?

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: what does ! mean in java?

Posted 22 June 2013 - 06:35 AM

It means "not".

while(!(cChoice =='Q'))

means

while cChoice is not equal to 'Q'
Was This Post Helpful? 1
  • +
  • -

#3 WINDY11965  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-June 13

Re: what does ! mean in java?

Posted 22 June 2013 - 06:47 AM

I get that it's not like !true = false, but what I do not get is when I run the first I get a never ending run of condition 2 but the second is a single display of condition 1 and I see the placement of the ! would make the difference but I do not know why. BTW thanks for the quick reply My second code was wrong too sorry

public static void main(String[] args) {
        char cChoice = 'Q';
        while ( !(cChoice == 'Q' || cChoice == 'q') ) {
            System.out.println("Condition 2");
            cChoice = 'q';
        }
        System.out.println("Condition 1");
}
}


This post has been edited by macosxnerd101: 22 June 2013 - 11:47 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: what does ! mean in java?

Posted 22 June 2013 - 06:57 AM

First iteration choice = Q, since you used || if the first conditional evaluates to true, the second will never get evaluated:

! (true || not evaluated) = false

That loop should never enter.
Was This Post Helpful? 1
  • +
  • -

#5 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: what does ! mean in java?

Posted 22 June 2013 - 07:06 AM

:code: :)

It sure is a difficult issue to get your head around. Here's my theory.

//first pass
while(!(true || false)) {
   cChoice = 'q'
}


//second pass
while(!(false || true)) {
   //mutation is the same.. just rinse repeat this.
}


So, try to understand that
while(!(cChoice=='Q') || !(cChoice=='q'))
will always return true, because one of them will always be false (and afterwards reversed to true).

Could be wrong, though. :)
Was This Post Helpful? 1
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: what does ! mean in java?

Posted 23 June 2013 - 12:52 AM

The OR operator is the most difficult to understand logically (even though it seems so simple), since people tend to use the words "or" and "and" interchangeably. If you ever get stump on what your logical expression containing OR operators really represents, apply demorgans law to get the expression in terms of and operators. (I like to think the "and" operator is the easiest logical operator to think about).

i.e.
while(!(cChoice == 'Q' || cChoice == 'q'))



apply demorgan's law

while((cChoice != 'Q' && cChoice != 'q'))



And boom, the condition should make perfect sense, "while cChoice is not equal to 'Q" AND cChoice is not equals to 'q'.
Was This Post Helpful? 3
  • +
  • -

#7 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: what does ! mean in java?

Posted 23 June 2013 - 06:34 PM

like people have mentioned its use is "NOT" used alot at the end of a do while loops and what not
Was This Post Helpful? 0
  • +
  • -

#8 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: what does ! mean in java?

Posted 23 June 2013 - 07:01 PM

Taking a closer look at this question, I come to realise that I did make an assumption based on trust. You said this was causing an infinite loop, however I just tested it and KYA is right.

while(!(cChoice == 'Q' || cChoice == 'q'))

This loop will, indeed, never run.

public class t {
	public static void main(String[] args) {
		int i = 1;
		while(!(i == 1 || i == 2)){
			System.out.println("loop " + i);
			i++;
		}
	}
}


Here's the code, similar in structure to your own, that I used to test it.
Was This Post Helpful? 1
  • +
  • -

#9 codecrusader  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-June 13

Re: what does ! mean in java?

Posted 25 June 2013 - 07:50 AM

It means "not"
Was This Post Helpful? 0
  • +
  • -

#10 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 454
  • View blog
  • Posts: 3,150
  • Joined: 12-January 10

Re: what does ! mean in java?

Posted 25 June 2013 - 07:56 AM

it means "not"

{ int a != 2}

means a is not equal to 2
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7731
  • View blog
  • Posts: 13,058
  • Joined: 19-March 11

Re: what does ! mean in java?

Posted 25 June 2013 - 08:05 AM

View PostKYA, on 22 June 2013 - 08:57 AM, said:

First iteration choice = Q, since you used || if the first conditional evaluates to true, the second will never get evaluated:

! (true || not evaluated) = false

That loop should never enter.



Just to expand on this, java uses short-circuit evaluation for the logical comparison operators && and ||. This means that such an expression is only evaluated until its result is known, and then it stops evaluating and returns that value.

An && expression is false if either of its components is false, therefore if the left-hand side evaluates to false, the right-hand side will never evaluate.

This is why you see things like
if (y != 0 && x/y > 42){
  // do something here
}


The y!=0 is a "guard clause" - if it is false, that is, if y==0, the division will never be evaluated and the "do something" will not happen.

This should really never matter to you, since this sort of thing typically makes for unreadable code, but it's something you should know about.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1