==和.equals()不能在Android Studio中的Java工作 [英] == and .equals() not working in android studio java

查看:342
本文介绍了==和.equals()不能在Android Studio中的Java工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想比较PackageInfo和ApplicationInfo字符串和Android Studio中找回自己的包名。

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

例在调试模式:

com.android.quicksearchbox

com.android.quicksearchbox

这是返回 NOPE犯规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?

感谢。

推荐答案

您错误地比较字符串(因为你其实比较参考)

Short answer:

You are comparing the Strings wrongly (because you are in fact comparing references)

string类在Java中java.lang包的定义,这正是一个的而非原始如int或boolean。

String Class in java is defined in java.lang package and it is exactly that, a class and not a primitive like int or boolean.

字符串的开发提供了许多字符ANS操作中几乎所有的Java应用程序使用commmonly

Strings are developed to offer operations with many characters ans are commmonly used in almost all the Java applications

在字符串不变,并最终在Java中,在这种情况下,JVM使用字符串池来存储所有的String对象。

String in immutable and final in Java and in this case JVM uses String Pool to store all the String objects.

我们可以创建String对象的使用new运算符就像任何普通的Java类,或者我们可以用双引号(文字分配),以创建一个String对象。

We can create String object using new operator like any normal java class or we can use double quotes (literal assignment) to create a String object.

有在String类可从字符数组,字节数组,StringBuffer的和StringBuilderetc等。

There are too several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilderetc etc.

当我们使用双引号创建一个字符串,JVM在查找字符串池中寻找是否有其他字符串存储具有相同的值。如果找到,它只是返回的引用,该String对象否则它创建一个具有给定值,并将其存储在字符串池一个新的String对象。

When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.

当我们使用new操作符,JVM创建String对象,但不把它存储到字符串池中。我们可以使用实习生()方法来存储String对象转换成String池或返回引用,如果已经有在泳池同等价值present一个字符串。

When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.

所以,当你做

String s1 = "abc";
String s2 = "abc";

那些在StringPool,自S1检查已经存在那里,S2将采取相同的参考,因此,S1 S2 ==是真实的。

those are checked in the StringPool and since s1 already exist there, s2 will take the same reference, hence, s1 ==s2 is true.

但是当你做的:

String s3 = new String("abc");
String s4 = new String("abc"); 

您正在使用的新运营商,所以JVM不检查,如果有一个字符串已在堆中,它只是分配一个空间S4,所以S3 S4 = =?当然没有。

you are using the new operator, therefore the JVM is not checking if there is an string already in the heap, it will just allocate a new space for s4, so is s3==s4 ??? of course no.

请看看下面的一个更能说明问题的例子形象。

Please take a look at the image below for a more illustrative example.

这篇关于==和.equals()不能在Android Studio中的Java工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆