Title: Logic of Java Operators && and ||
Tags: java;if-statement;operators
Question: Hello dear Programmers,
I have a String String input = "30.09.1993";
Then i want to save all numbers in this string in an array(Only numbers!). The "." are at index 2 and 5 so i want to skip these parts of my string in my loop with an if-statement.
I fixed my problem and everything works fine but I'm confused with the logic of the &amp;&amp; and || operators.
This is my working code:
String input = "30.09.1993";
int[] eachNumbers = new int[8];
int x = 0;
for(int i = 0; i <= 9; i++){
if(i != 2 &amp;&amp; i != 5){
eachNumbers[x] = Integer.parseInt(input.substring(i, i+1));
x++;
And this is the code which doesnt work:
String input = "30.09.1993";
int[] eachNumbers = new int[8];
int x = 0;
for(int i = 0; i <= 9; i++){
if(i != 2 || i != 5){
eachNumbers[x] = Integer.parseInt(input.substring(i, i+1));
x++;
The only difference between these two code snippets are the operators in the if-clause.
I thought that the results for these operators are:
&amp;&amp; operator:
false + false = false
true  + false = false
false + true  = false
true  + true  = true
|| operator:
false + false = false
true  + false = true
false + true  = true
true  + true  = true
So in my opinion the second code snippet should work and the first should throw a NumberFormatException. But thats not the case.
I'm sure there are some better solutions for what im doing but my question is only about the logic in my if-statement. Can someone explain me the logic behind this? I'm totally confused and thankful for every helping answer.
Greetings
Lukas Warsitz

Here is the accepted answer: In the second snippet, the if-clause will always be true, because i will always be not 2 or not 5 (because it cannot be 2 and 5 at the same time)
For what you want to do your first snippet is fine, it does exactly what you want: If your are not at the third element and neither at the 6th element, you want to parse, else you are at one of the points in the date.
Comment for this answer: After thinking a while about your answer i understood it, thank you very much for the fast answer!

Here is another answer: i can only have one value at a time.
So any of the following conditions are valid:
if(i != 2 &amp;&amp; i != 5) //means `i` is not any 2 AND i is not 5
if(i == 2 || i == 5) //means `i` is either 2 OR 5
Note: (i != 2 || i != 5) is always true because at any time
If i = 2, LHS i is not equal to 2 is FALSE and RHS i is not equal to 5 is TRUE
If i = 5, LHS i is not equal to 2 is TRUE and RHS i is not equal to 5 is FALSE
For all other value of i: Both LHS and RHS are true.
Comment for this answer: "|" does not stand for OR :)
Comment for this answer: well i don't see you are using bitwise OR as i thought it was a typo
Comment for this answer: @rookieB it does stands for OR, but [bitwise OR](http://stackoverflow.com/a/10142098/2982225), not for logical OR ;) thanks for noticing :)

Here is another answer: Key point is here :  In &amp;&amp; if first case is false, second case won't check.
true &amp;&amp; false // Evaluates false because the second is false
false &amp;&amp; true // Evaluates false because the first is false
true &amp;&amp; true // Evaluates true because both are true
false &amp;&amp; false // Evaluates false because the first is false
In || if first case is true , second case won't check.
true || false // Evaluates true because the first is true
false || true // Evaluates true because the second is true
true || true // Evaluates true because the first is true
false || false // Evaluates false because both are false
Comment for this answer: "false && false // Evaluates false because both are false" - is not true. because the first one is false , so the second one is untested

Here is another answer: I'm not giving answer for your question but suggesting better code.
public class splitToArray {
public static void main(String[] args) {
String input = "30.09.1993";
input = input.replace(".", " ");
String array[] = input.split(" ");
for(String value : array){
System.out.println(value);
}
Output:
Now use Integer.parseInt to get integer value. This will also work when you have String input = "30.9.1993"; [0 removed from 09]
Comment for this answer: Thanks for your advice but i need to create a digit sum from the birthdate, thats why i need every single number.

Here is another answer: Although not necessarily your issue which LionC notes.
the 4 and and or logic operators follow the following logic:
&amp; is as you describe &amp;&amp; above.
false + false = false
true  + false = false
false + true  = false
true  + true  = true
| is as you describe || above
false + false = false
true  + false = true
false + true  = true
true  + true  = true
&amp;&amp; and || are shortcut operators that don't test the second value if it would make no difference to the result.
&amp;&amp;
false + untested = false
true  + false = false
false + untested = false
true  + true  = true
false + false = false
true  + untested = true
false + true  = true
true  + untested  = true

Here is another answer: Pick a number, any number. It is either not equal 2, or not equal 5.
Therefore the expression (i != 2 || i != 5) can be statically (by the compiler) transformed into just true and the whole if control structure eliminated.
The obvious result is a NumberFormatException if any of the characters is not a digit.

Here is another answer: Your second snippet is not working because it should be
if(i != 2 &amp;&amp; i != 5)
and your first snippet is not throwing error because of same reason that you have used &amp;&amp; there.
Comment for this answer: You told me everything i knew before.. but thanks anyway

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 7, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:12/13/13', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<java><if-statement><operators>', 'Title': 'Logic of Java Operators && and ||', 'ViewCount': 289, 'id': 20564933, 'user_AboutMe': '<p>Im doing an apprenticeship as an "information science technician specialising in application development"</p>\n', 'user_AccountId': 3361883.0, 'user_DisplayName': 'Lukas Warsitz', 'user_Reputation': 1221.0, 'user_Views': 203.0, 'user_id': 2823515.0}}