This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!

ken luong

Greenhorn
+ Follow
since Sep 24, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
1
Total given
4
Given in last 30 days
4
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ken luong

Junilu Lacar wrote:

           "No, you would get a ClassCastException. The Java runtime will always check the actual type and if it finds that the object cannot be cast to Persons, it will throw the exception."

You are right Junilu Lacar. Any object that isn't of a class which is derived from Persons would cause a ClassCastException to be thrown. I hope I thought it through this time😁. Thank you for your correction!
4 days ago
Thanks Campbell Ritchie! You're absolutely right. Not only the cast is not needed, it's wrong to have it in the definition of saySomething(Object). Suppose another object of type different than that of Persons is passed in, my original definition would call the toString() method  of Persons every time, which is not the desired result.

I picked up learning Java a few weeks ago and have been part of this site for the same amount of time. In this short period of time, I've gained valuable information, particularly from you. Thanks very much again!
4 days ago
Hi guys. I am trying to get a handle on understanding the passing of primitive and reference data type in to functions. It seems to me that passing primitive data into a function is analogous to passing a reference data. I have written some code to help me explain myself.

Basically, I created a Persons class which contains, among  other things, a redefinition of the toString method that was inherited from the Object class. In main, I have two functions, function1(double) and function2(Object).
When int number is passed into function1, I understand number is implicitly promoted to a double to match the signature of function1. Is it right to say that the same effect happens when a derived object Persons is passed into function2? An alternative way of looking it is that since Persons extends Object, every Persons instance is an instance of Object.
4 days ago
When to declare variables and methods static?

Variables and methods of a class should be declared as static if they are independent of any specific object. In other words, if variables and methods are of class level, they should be declared as static.
Static methods only have access to static member variables and can't be overriden in derived classes.
Static methods can't invoke member functions but member functions can invoke static methods and static member variables.
Static methods and variables can be accessed via class name as well as objects.

Please add to my understanding of static, Thanks!
4 days ago
Campbell Ritchie

Don't use the same object operator == on reference types (line 42)

What happens if the player enters the same digit repeatedly, e.g. 000, 111 100, 666?



The usage of the == operator compares the reference variables rather than the content of the objects which are referenced by the reference variables. Let's consider s1 and s2, which are String objects created dynamically. Suppose that s1 = "hi" and s2 = "hi". In this scenario, s1 == s2 would returns false, since s1 and s2 are different variables. String.equals() should be used instead of ==. This is what i gathered after some reading.


If the winning number is 123 and the user enters 111, counter would be 3, which is wrong. I can see the logic error. Thanks. Campbell Richie!
4 days ago
Hello guys. Please help to improve my code and logic for the following. I just started learning programming. I hope my inexperience doesn't make you want to scream out loud as you read my code
5 days ago
Thank you so very much for your insights Campbell Ritchie! I definite will keep your advices in mind as I learn more about Java.
1 week ago
Thank you Tim Moores.!
3 weeks ago
Hello guys. I am learning Java on my own. I have written a Fraction class. I would very much appreciate if you guys could offer some advices and inputs as to how I could improve it. Since I am learning on my own, chances are that I make many mistakes. Your comments will help me tremendously. Thanks.

3 weeks ago
3 weeks ago