Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,074 members, 7,818,215 topics. Date: Sunday, 05 May 2024 at 10:32 AM

How Long Did It Take You Guys To Become Strong Java Programmers - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / How Long Did It Take You Guys To Become Strong Java Programmers (5581 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)

(1) (2) (Reply) (Go Down)

Re: How Long Did It Take You Guys To Become Strong Java Programmers by Fulaman198(m): 6:15pm On Jan 15, 2016
sinequanon:


I said it works in some situations. You queried this. You were wrong.

Your explanation is not quite right, either. In my example, string1 and string2 are two separate objects.

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. 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.

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.

class SomeClass {

public static void main(String[] args) {

// Scenario 1 .. String Literals
String string1 = "This is a string test.";
String string2 = "This is a string test.";

// Scenario 2 .. String Literal vs String Object
String string1 = "This is a string test.";
String string2 = new String("This is a string test." );

// Scenario 3 .. String Input via Scanner
Scanner in = new Scanner(System.in);
String string1 = in.nextLine(); // input "This is a string test."
String string2 = in.nextLine(); // input "This is a string test."

System.out.println((string1 == string2));
System.out.println(string1.equals(string2));

// Scenario 1 output .. true true
// Scenario 2 output .. false true
// Scenario 3 output .. false true

}

}


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:
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().

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:
The reason it works is because the string is interned, which means that it gets only one copy at runtime.

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:
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.

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.


// These two have the same value
new String("test" ).equals("test" ) // --> true

// ... but they are not the same object
new String("test" ) == "test" // --> false

// ... neither are these
new String("test" ) == new String("test" ) // --> false

// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true

// checks for nulls and calls .equals()
Objects.equals("test", new String("test" )) // --> true
Objects.equals(null, "test" ) // --> false


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:
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.

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:


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".

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:


What is object.equals() referencing? It's parsing and checking for a value. It's quite different from ==

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:


If the runtime type of the object is Object, then .equals will behave the same as ==.

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:
Honestly, I'm still trying to wrap my mind around Java (I'm going the self-taught route). It's been almost a month now since I started learning, and so far, I've come to understand the logic behind frameworks and packages. I've also come to understand classes and inheritance in Java, but it's taking me longer than I imagined it would to really get a handle on the language. So far, the best I've done, in my self-taught journey is to build a simple package to test out/learn some OOP principles.

I would like to get better with Java though. Overall, I'd say I've made some decent progress in the span of a few weeks of actual learning. Java has a ridiculously steep learning curve. C is so easy compared to this. Anyway, I imagine it would take me more time to become a strong Java programmer, but I believe I can at least get past the learning curve within the next coming months and probably get a good foundation in 3 - 6 months, maybe sooner if I prioritize. I project that I would be strong by the end of the year. It's not to say that I will claim to know the language inside-out, but I will at least be strong enough to know how to use Java to get things done.
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:
Looks like I was right in time


Please how do I get input in java??

Like . . I want to accept a string & then print. . . I am confused on the BufferedReading and Scanner.



I did :-

String c1 = getInput("Enter a colour"wink;



It said I should create method getInput(String)


When I did I had :-

private String getInput (String String) {
return null;
}


If I return c1 instead of null, it will accept the String, but won't print. Will just continue accepting



Help me jare. . . Am stuck


cc : fulaman198, teempakguy (oya, Java is here sad sad ), dhtml18 & other guru programmers


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"wink;
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:


A shorter solution but with a different programming language.



Python 3
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:
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?

Corrected!
You can edit the post and remove your number to avoid unsolicited messages.

(1) (2) (Reply)

Solution To Tokenmismatchexception Problem In Laravel 5.1 / See Money Making Programmers In Nairalans / Programmers how much do you earn monthly?

(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
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.