Difference between != and !(boolean) in java
Whats difference between != and !(boolean) in java?
int a = 1;
int b = 2;
boolean c = a != b; //true
vs
int a = 1;
int b = 2;
boolean c = !(a == b); //true
1 answer
-
answered 2020-11-24 00:57
Thudani Hettimulla
int a = 1; int b = 2; boolean c = a != b; //true
In this one you are assigning whether a is not equal to b to the variable c. which is true as a is not equal to b
int a = 1; int b = 2; boolean c = !(a == b); //true
Here you are assigning the inverse of whether a equal to b. Where a is not equal to b and inverse of it is true.
See also questions close to this topic
-
I can't Create a Directory inside Download Directory (or) Create a directory/folder for my app
I want to create a separate folder for my application in Mobile's Internal storage. In that folder, I want to Store My downloading Video File using Download Manager.
My problem is No folder is Created and My Video file is also Downloaded.
MainActivity.kt:
btnDownload.setOnClickListener { val url = Uri.parse(etUrl.text.toString()) val file = File(Environment.DIRECTORY_DOWNLOADS +"/AndroidDownloader") if (!file.exists()) { file.mkdirs() } val request = DownloadManager.Request(url) .setTitle("down") .setMimeType("video/MP4") .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE or DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION) .setAllowedOverMetered(true) .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/AndroidDownloader", "video.mp4") val dm: DownloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager dm.enqueue(request) }
I also Included Below Code in the Manifest:
<manifest .. android:requestLegacyExternalStorage="true"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> </manifest>
I can't download video file too
btnDownload.setOnClickListener { val request = DownloadManager.Request(Uri.parse(etUrl.text.toString())) .setTitle("down") .setMimeType("video/MP4") .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE or DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION) .setAllowedOverMetered(true) .setDestinationInExternalPublicDir(DIRECTORY_DOWNLOADS , "video.mp4") val dm: DownloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager dm.enqueue(request) }
I also gave permission to app for accessing Storage, Manually.
I'm sorry if My question isn't clear to you.
Thanks in Advance :)
-
How do I make an auto-retry system with labels?
I would like to create an auto-retry system in my application. Here, I have my 3 nested for loops and in the last one, I have my try-catch, preceded by a label :
for (String engine : engines) for (String website : websites) for (String keyword : keywords) { scrape: try { // my long code } catch (Throwable t) { } }
And in that catch, I would have my auto-retry system :
catch (Throwable t) { if (retry <= retries) { retry++; break scrape; // Go to the label } }
The problem is that
break scrape
goes to the label but by skipping an iteration in my loop. Noting that I have 3 nested loops (might not be that important though), how would I proceed to go to that label without skipping a whole iteration? -
Kotlin/Java processBuilder efficiency vs python subprocesses
I'm having trouble with JVM processes, I'm trying to create a program which needs to call a different language program with changing parameters each times, a lot of times.
For example, lets say I need to call node.js program via my main program (KotlinJvm) 1000 times in 10 seconds.
Now, I'm using ProcessBuilder class in order to create a new process so I can get the information back to my main, but its not fast enough. its even slow :/
I researched a bit and found out about python subprocess library, tried to implement the same idea there. In python 3.9 my implementation worked great! and fast
1. So I'm asking, what is the difference between python subprocess and Jvm Process
2. Is there a way to create a Jvm subprocess like python
As I read, subprocesses can be created in Jvm too, by calling
.start()
from the same ProcessBuilder, but its still slow.Just to make sure, in case of calling just one time it wouldn't have been a problem. The problem is that I need to call this file 1000 times in 10-20 seconds
Adding some code here for examples
Kotlin example - I tested a bit, and
waitFor()
function takes a long time, and that's my problemPython example
Thank for help :)
Edited: If this is the same, is there any way to optimize Jvm processes execution? any environments changes?
-
the variable is set to false inside my method, once the method runs it should change to True. But it does not
def checkPassword (password): upper = False lower = False number = False special = False for i in range (len(password)): if ((ord(password[i]) >= 65) and (ord(password[i]) <= 90)): upper = True elif ((ord(password[i]) >= 97) and (ord(password[i]) <= 122)): lower = True elif ((ord(password[i]) >= 0) and (ord(password[i]) <= 9)): number = True elif ((ord(password[i]) >= 0) and (ord(password[i]) <= 9)) : special = True if ((upper == True) and (lower == True) and (number == True) and (special == True)): print ("Your password is strong") else: print ("Your password is not strong. Make sure to make it a mix between upper and lower case letters, number, and special characters.") def main(): password = input("Enter a password: ") checkPassword (password)
Input:
running99*FAST
Output:
Your password is not strong. Make sure to make it a mix between upper and lower case letters, number, and special characters.
Problem: This password should come back as a strong password but it does not. I honestly don't know what I did wrong.
P.S. I am using the ASCII table to identify whether the letters are lowercase, uppercase, or numbers, and special characters.
-
Why dosen't my boolean change its value to true after meeting the conditions?
I'm trying to make a simple customer registration system and a certain textfield has a label beside it that changes whenever it validates a good birthday format(mm/dd/yyyy) I did a switch statement to invalidate if they got the format wrong, but the boolean value I have set remains to be false even though the conditions are met for it to be true
int month = 0, day, year; Boolean vmonth = false, vday = false, vyear = false; for(int x = 1; x<=txt_customerbday.getText().length(); x++){ if(txt_customerbday.getText().length() != 10){ lbl_bdaycheck.setForeground(Color.red); lbl_bdaycheck.setText("Bad Format"); } else if(10 == txt_customerbday.getText().length() && vyear == true && vmonth == true && vday == true){ lbl_bdaycheck.setForeground(Color.green); lbl_bdaycheck.setText("Good Format"); }
Above is the code that will change the label if the conditions are met
if(txt_customerbday.getText().length() == 2){ month = Integer.parseInt(txt_customerbday.getText().substring(0)); System.out.print(month); if(month > 12){ vmonth = false; }else{ vmonth = true; System.out.print(vmonth); } }
The code above is the validation of the month (ignore the print statement I just did that to check their values)
if(txt_customerbday.getText().length() == 5){ day = Integer.parseInt(txt_customerbday.getText().substring(3,5)); System.out.print(day); System.out.print(vday); switch(month){ case 1: if(day > 31 || day <= 0){ vday = false; }else{ vday = true; } break; case 2: if(day > 28 || day <= 0){ vday = false; }else{ vday = true; } break; case 3: if(day > 31 || day <= 0){ vday = false; }else{ vday = true; } break; case 4: if(day > 30 || day <= 0){ vday = false; }else{ vday = true; } break; case 5: if(day > 31 || day <= 0){ vday = false; }else{ vday = true; } break; case 6: if(day > 30 || day <= 0){ vday = false; }else{ vday = true; } break; case 7: if(day > 31 || day <= 0){ vday = false; }else{ vday = true; } break; case 8: if(day > 31 || day <= 0){ vday = false; System.out.print(vday); }else{ vday = true; System.out.print(vday); } break; case 9: if(day > 30 || day <= 0){ vday = false; }else{ vday = true; } break; case 10: if(day > 31 || day <= 0){ vday = false; }else{ vday = true; } break; case 11: if(day > 30 || day <= 0){ vday = false; }else{ vday = true; } break; case 12: if(day > 31 || day <= 0){ vday = false; }else{ vday = true; } break; default: vday = false; break; } System.out.print(vday); }
The code above is the code for the day validation this is the one where I think I have the problem
if(txt_customerbday.getText().length() == 10){ year = Integer.parseInt(txt_customerbday.getText().substring(6,10)); System.out.print(year); vyear = true; }
The last one is unfinished but this is for the year validation
-
Determine if values are different
Let's say I have 4 variables
bool value1 = false; bool value2 = false; bool value3 = true; bool value4 = false;
and they all don't have the same value (all are true
||
all are false)I have found 2 approaches, anyway none of them looks easy to understand.
bool areDifferent = !(value1 == value2 == value3 == value4);
and
bool areDifferent = new[] { value1, value2, value3, value4 }.Distinct().Count() > 1;
Question: Is there a other approach, which is better in terms of readability / understandability?
-
What, if any, are side effects of && (logical AND) in Linux shell?
As reported in this solution, what could possibly be the significant functional difference between?:
sudo apt-get update sudo apt-get install -y nvidia-container-toolkit
and
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
I've always assumed logical AND (
&&
) in this context would bear no impact other than bailing if the first command failed. But in this case it made all the difference with the installation succeeding. What could be the reason? -
Logicical operations in JS
I have a question regarding logical operations in js:
console.log('XXX' !== ('ZZZ' && 'XXX' && 'No Code Available' && '')) console.log(('XXX' !== 'ZZZ') && ('XXX' !== 'XXX') && ('XXX' !== 'No Code Available') && ('XXX' !== ''))
The first one is true, the second one is false. Could you tell me why? I thought the first and the second is the same statement, but the first one is shorter.
How can I shorten the second one if that is correct?
-
Why sometimes logical operator && doesn't work and I need to use if statement instead?
I recently realised that sometimes my logical conditional doesn't work in the way that I wanted. For instance, my intention was to run some instruction about if some property is true:
this.property && obj.instruction() // instruction is running as a result of logical statment
And it works in most cases, but sometimes instead of running function it's returning true:
this.property && obj.instruction() // returns true
It's quite easy to refactor code to work in the way that it is suppose to work:
if (this.property) { obj.instruction() }
So my question is why does JavaScript sometimes behave like this?
-
For loops vs Max function for finding the largest number in a list in Python (Beginner)
What is the difference between these both? For loop and Max Function. Which one should I choose? For loop:
numbers = [3, 6, 2, 8, 4, 10] max = numbers[0] for number in numbers: if number > max: max = number print(max)
Instead, why can't I use the max function, which is only 1 line of code? Max function:
print(max(3, 6, 2, 8, 4, 10))
Both of them show the same thing on the terminal. Why can't I choose this one?
-
values for which the difference is the closest to a specific number
Currently, I am looking into a python problem where I would like to find the two values in a list, for which the difference is the closest to a specific number.
For example, I would like to find the two values in
A
for which the difference is the closest to 2.A = [1,5,9,10,20,7]
In this case, the answer needs to be
5
and7
.The constrain is that you always will do
A[N+x] - A[N]
. For instance, you are not allowed to do5-9
. In that case, it will always be9-5
Would this be possible in python?
Thank you in advance for your help.
-
What is the difference between ios_base::sync_with_stdio(0); and ios::sync_with_stdio(0); in C++?
My mentors in CP recommended me to use ios_base::sync_with_stdio(0); since it increases the speed of program execution. While going through some videos on YouTube, I came across ios::sync_with_stdio(0); also.
So, What difference does adding or deleting _base make?
Which is better, ios_base::sync_with_stdio(0); or ios::sync_with_stdio(0);?
Kindly explain. Thanking you in advance.