Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

Last question Sir

 

String str1 ="abc"

String str2 ="abc"

 

you want to say vM store this as a same object . that why str1==str2 print 

but

String str1 ="ab"

String str2 ="abc"

 

you want to say vM store this as a different object . that why str1==str2  not print 

 

 

 

 

 

Please use plain text.
Developer
pankajace12
Posts: 198
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Need some help me to understand string concept

[ Edited ]

Hi Ravi,

When you assign the same value into both string. then its hascode will be same.

Like if, you put.

String str1="abc";
String str2="abc";

Here for Str1, Str2 hashCode is same. That is why it prints "Equal".

And, once you change the content of the string, then their hashCode is changed. So it does not print "Equal".

 

You may check hasCode in your code also. 

 

In other word, you can also say that hasCode is generate on the behalf of content of this variable. 

 

And, when the hascode will not match, their reference become different.

 

Thats why this statement does not print "Equal".

 

String str1 ="ab";

String str2 ="abc";


if(str1 == str2)
System.out.println("Equal");

 

Thanks
Pawan

Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

Hi

Pawan Thanks for reply.

 

String str1="test"

String str2 ="test"

 

when i print the hashcode they are same ..you are right.

 

But when 

String abc =new String("test")

 

but when i print abc hash code it also print the same value .but why abc==str1 not printing..?

Please use plain text.
Developer
pankajace12
Posts: 198
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Need some help me to understand string concept

Hi Ravi,

 

As you know that 

 

String str1="test", this is reference not an object. and 

 

String abc =new String("test") this is object, so when you try to compare this like... str1==abc, this will not be true. and not print the "Equal".

 

Even you may try it like this also.

 

String abc1 = new String("A");
String abc = new String("A");


if(abc == abc1)
System.out.println("Equal");
else
System.out.println("Not Equal");

 

Here it will print "Not Equal".  

 

As earlier @simon told you: str1==str2 will never succeed as the '==' operator compares the reference to the object. equals compares the object itself, so you need to use here equal method.

 

Thanks

Pawan

 

Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

Hello sir

i understand every thing but need some confusion about what is reference actually i know object ..

 

how i know which is reference ..?

 

String str1="test", this is reference .

from my point of view str1 is also an object we consider as literal .  b

 

please give some little bit explaination about reference ? 

Please use plain text.
Developer
pankajace12
Posts: 198
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Need some help me to understand string concept

Hi Ravi,

See, until we did not assign new keyword, that is reference. like

String str="pawan";

and as we assign

str = new String(); this become object.

And, one thing for you. reference store in stack memory and object is stored in heap memory.

Thanks
Pawan

Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

Thanks sir 

this mean if i check reference element by using ==;

 

and only object element is checked by equal method?

 

am i right ..?

 

 

Please use plain text.
Developer
pankajace12
Posts: 198
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Need some help me to understand string concept

Hi Ravi

You are right.

Thanks
Pawan
Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

Thanks Sir ...!!

is there any way to put image from one position to another along with zooming of screen as well as image in blackberry (touch device)
Please use plain text.
Developer
pankajace12
Posts: 198
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Need some help me to understand string concept

Hi Ravi,

Please create another thread if thread is completed.

Press kudo, if you feel my answer is right for you.

Thanks
Please use plain text.