== and .equals() not working in java [duplicate]
So I was trying to compare the string in PackageInfo and ApplicationInfo and retrieve their package name in Android Studio.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
for (PackageInfo app : apps)
for(ApplicationInfo packageInfo:packages)
String packageName = packageInfo.packageName;
String sysName = app.packageName;
if (packageName != sysName)
{
Log.d("", "NOPE DOESNT MATCH");
break;
}
if (packageName.equals(sysName.toString())) ;
{
Log.d("", "IT MATCHES");
//do something after that
So the problem came by when I saw inside the debug mode, they actually HAD the same string
Example in debug mode :
com.android.quicksearchbox
com.android.quicksearchbox
It returns as "NOPE DOESNT MATCH"
I'm really curious why this will happen and I have tried other form like trim to take off away white spaces between and no, it still does not return me "IT MATCHES", can anyone help please?
Thanks.
Replace: if (packageName != sysName)
With: if (!packageName.equals(sysName.toString()))
Also, you have a ;
at the end of if
which shouldn't be there. (Here: if (packageName.equals(sysName.toString())) ;
)
Actually, to form your condition better, you should have an if-else
block instead of 2 if
s.
something like:
if (packageName.equals(sysName.toString())){
Log.d("", "IT MATCHES");
} else {
Log.d("", "NOPE DOESNT MATCH");
}