Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,067 members, 7,956,982 topics. Date: Tuesday, 24 September 2024 at 01:21 AM |
Nairaland Forum / Science/Technology / Programming / How Long Did It Take You Guys To Become Strong Java Programmers (5796 Views)
Help Me Out Java Programmers / Java Programmers ,I Need Help Fixing This Code. / How Are You Guys Coping With International Ban On Cards (2) (3) (4)
Re: How Long Did It Take You Guys To Become Strong Java Programmers by Fulaman198(m): 6:15pm On Jan 15, 2016 |
sinequanon: You are not getting it. So I'll explain it. They are pointing to (I should say referencing to since this is Java) to the same object. == never and I repeat never checks for the actual value of a string. If you don't believe me, you are more than welcome to check Java forums like coderanch.com or many other popular Java related forums they will tell you the same exact thing. |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by ChinenyeN(m): 6:27pm On Jan 15, 2016 |
Oh okay, I see what Fulaman198 is saying now. My question now would be... String string1 = "This is a test."; String string2 = "This is a test."; How do these two count as the same object? I'd have imagined that the JVM would distinguish string1 and string2 as separate objects. I'm asking because I honestly don't know. I'm a novice at Java. |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by sinequanon: 6:31pm On Jan 15, 2016 |
Fulaman198: You are not getting it. "== never checks" is meaningless. == is a syntax. The compiler is doing the work, and it is not doing it exactly the way you have described. You are fudging your use of the word "object". The reason it works is because the string is interned, which means that it gets only one copy at runtime. |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by ChinenyeN(m): 6:40pm On Jan 15, 2016 |
Just as a test, I created and compiled three different scenarios to see what the results would be when I ran the code... Here's what I got.
Judging by the output, I would say best practice would be to use the string method .equals(), unless you know ahead of time that both strings will be literals. If you do not have that foresight, then always stick with .equals(). 1 Like |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by sinequanon: 6:45pm On Jan 15, 2016 |
ChinenyeN: Correct! It you know that they will be string literal constants (or, Integers in some cases), it is safe. |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by ChinenyeN(m): 6:54pm On Jan 15, 2016 |
sinequanon: Okay. So this is what Fulaman198 meant when he referred to them being the same object. I understand now. Thanks, both of you. |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by Fulaman198(m): 6:56pm On Jan 15, 2016 |
ChinenyeN: Java has what is known as and referred to as a String Pool so sometimes == does indeed work. However == compares if objects are equal and not the value within the object itself. In the case of "test"=="" test" being true, these values are interned strings. == tests for reference equality and the Object.equals() method tests for value or in this case string text equality.
Does the explanation make sense? 3 Likes |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by ChinenyeN(m): 7:18pm On Jan 15, 2016 |
Yes, the explanation does make sense. Thanks. 1 Like |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by sinequanon: 7:18pm On Jan 15, 2016 |
Fulaman198: Object.equals() tests for "reference equality" (whether the same object is being referenced, just like == ) This is overridden in class String, to test for "value equality". 1 Like |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by Fulaman198(m): 7:22pm On Jan 15, 2016 |
sinequanon: What is object.equals() referencing? It's parsing and checking for a value. It's quite different from == |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by sinequanon: 7:24pm On Jan 15, 2016 |
Fulaman198: If the runtime type of the object is Object, then .equals will behave the same as ==. |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by Fulaman198(m): 7:32pm On Jan 15, 2016 |
sinequanon: Ok |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by Nobody: 7:34pm On Apr 17, 2016 |
holy bump |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by heywhy824(m): 9:51pm On Apr 17, 2016 |
ChinenyeN:it takes years to be a cute programmer but don't worry bout the years just keep on working towards it 1 Like |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by Nobody: 6:12pm On Aug 14, 2016 |
Dekatron: considering yhu almost broke my head, that method is wrong import java.util.Scanner; public class Colour { public static void main(String[] args) { Scanner in = new Scanner(System.in); String colour; System.out.println("Enter a colour:/n" colour =in.nextLine(); System.out.println("you entered:/n%s/n", colour); } } that should solve yhur problem just fine. |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by pxjosh(m): 6:46pm On Aug 18, 2016 |
khaynoni:I ran ur code on qpython3 and am having syntax error. Besides I've been reading books on Python and my syntax is still poor. Can I add u pls WhatsApp? |
Re: How Long Did It Take You Guys To Become Strong Java Programmers by khaynoni(m): 9:47pm On Aug 18, 2016 |
pxjosh: Corrected! You can edit the post and remove your number to avoid unsolicited messages. |
Programmers how much do you earn monthly? / Discover How To Be A Blockchain Developer & Blockchain Engineer / Php Challenge: External Authentication Of Phpbb3
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 35 |