1

I have this code

  import java.io.*; import java.util.*;
import javax.xml.parsers.*; import org.xml.sax.InputSource;
class TeamPlayer {
    private int pulse;
    TeamPlayer() { pulse= -10; }
    TeamPlayer(int v0) {pulse= v0 +5;}
    public int m(int v) { return 31%3 ;}
    public int get_pulse() { return 1* pulse;}
}
class GoalKeeper extends TeamPlayer {
    GoalKeeper() { stress -=8; }
    public static int stress=3;
    public int m(int v) { return (v & 3) + 15; }
}

but I can't understand what "&" means. Is it different from "&&"?

brso05
  • 13,142
  • 2
  • 21
  • 40
user5510402
  • 67
  • 1
  • 1
  • 7
  • 6
    Google "difference between `&` and `&&` java". Please make an effort to search before posting here. Here is one example: http://stackoverflow.com/questions/7199666/difference-between-and-in-java – brso05 Feb 04 '16 at 13:23
  • http://stackoverflow.com/questions/5564410/difference-between-and – brso05 Feb 04 '16 at 13:26
  • http://stackoverflow.com/questions/9458602/whats-the-difference-between-and-and-and – brso05 Feb 04 '16 at 13:26

3 Answers3

11

It the bitwise "and" operator. Link: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Edit more specific explanation: It is used to combine two numbers in which a position in the binary representation is 1, if it is 1 in both numbers so:

0101 & 0000 = 0000
0101 & 1111 = 0101
0101 & 0100 = 0100
...and so on...
Robert Bräutigam
  • 7,514
  • 1
  • 20
  • 38
2

It is an operator which performs binary operations.
eg:

  1001   //9
& 1110   //14
---------
  1000   //8

It returns 1 if both the operands are 1.


...Is it different from "&&"

Yes it is.

&& is a logical operator, which checks two (or more) operators and returns true if all are true.

They have similar working, except that && won't check the second argument if the first argument is enough to tell that the result is false.

dryairship
  • 6,022
  • 4
  • 28
  • 54
0

Consider I have the expression,

   int a=0;
   if (a!=0 && a!=1)
   {     
         //some expression(s)
   }

When it checks the first condition and sees that it is false it does not check the other condition and automatically returns false as AND returns true only if all the conditions are true. But in case of '&', it also checks the second condition although the first one is false.

Shinjinee Maiti
  • 143
  • 1
  • 2
  • 10