Here is the code:
import java.util.*; public class HelloWorld { public static void main(String[] args) { String string1 = "randomString1"; System.out.print(f(string1)); } public static int f(String string) { try { String string2 = "randomString2"; if(string.equals(string2) == true); return 0; } catch(Exception e){ e.printStackTrace(); } return 1; } }//end of class
Now the output of this program
0
But I expected it to be:
1
Because I wanted the function fto return 0if the strings were the same and 1if they were not.
f
So, there is definitely something wrong I know, and I assume this has something to do with the method equals.
equals
This is problem:
if(string.equals(string2) == true); ----------------------------------^
you will need to remove the second colon, it is rather an expression in itself:
if(string.equals(string2) == true) return 0;
if(string.equals(string2) == true);you have a semicolon that does not belong. Honestly, you don’t even need a part == true.
if(string.equals(string2) == true);
== true
- if:
if (string.equals(string2) == true); //---------------------------------^ return 0;
, , , , , return 0 , 0, 1
return 0
, :
if (string.equals(string2) == true){ //do nothing } //pass to the next statement return 0;
if (string.equals(string2)){ return 0; }
...
(;) if , ,
return 0;
, .
if(string.equals(string2)) return 0;
. .
if semilocon. :
if(string.equals(string2)) { return 0; }
.
public static int f(String string) { String string2 = "randomString2"; return (!TextUtils.isEmpty(string) && string.equals(string2)) ? 0 : 1; }
try-catch, TextUtils null .
Source: https://habr.com/ru/post/1674748/More articles:looping on dates and applying a function to pandas dataframe - pythonHow to limit the DllReferencePlugin web package to specific pieces? - webpackStarting a service during Docker build - dockerСблизить отношения Laravel Eloquent Collection - collectionsApplication Insights - how to sort by user size - azureSpring Boot annotation @PreDestroy not working - javaProblem with updating Visual Studio 2017: unable to load sqlite3 dll file: the specified module was not found. (Exception from HRESULT: 0x8007007E) - visual-studio-2017Converting a datatable to an array of doubles - arraysDjango: running code every time it starts, but after database migration - pythonAngular 2: where to store data - angularAll Articles