Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!

Junilu Lacar

+ Follow
since Feb 26, 2001
Junilu likes ...
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
Columbus OH
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Junilu Lacar

Koen Ursem wrote:

Sam Peterson wrote:
Also, since we still have the R3 object that's not eligible for garbage collection at the end of this program, what is the lifespan of that object and the two reference variable after the program finishes?

I wonder if R3 would be up for GC at line 10 when there is no further use of the variable two in the current scope, or on line 12 where the scope of the current codeblock ends?

There's the concept of weak references vs strong references in Java that you might want to research some more if you're really interested in those nitty-gritty details. In this case, whether R3 is GC-eligible on line 10 or when the main() method terminates is rather like splitting hairs and, as I said previously, a moot point. Since the program will terminate when you exit from main(), the GC probably won't even bother to sweep up any of those Rabbits since the whole zoo is getting vaporized anyway!
4 hours ago
and Welcome to the Ranch!

Can you tell us what how that benchmark program is being run? Is that in JShell or some other interactive REPL-like for Java? I ask because I find this intriguing:

That won't work unless the chess, jar1, and stone fields are public. If they're private, I'd expect some kind of access error to be reported.
1 day ago
You're missing a closing brace for your class Jar. You might want to fix that first and retry.
1 day ago
But then again, Optional can open up the possibility for some nice alternatives:
1 day ago

Liutauras Vilda wrote:But in order to ensure, that returned result (sum) is in fact the result of the collection/array you intended to pass (and not somewhat wiped out, meaning with no elements), one has to introduce such check. Isn't that what Optional would serve, just in nicer way?

I've always preferred to make design choices based on present needs, not on speculation that something might happen. To me, the most sensible first cut would be:

and expect that to show $0.0 if the cart was empty.

Now, if somebody said, "We need to display the message 'Your cart is empty' because users might get confused if they see $0.00 when they haven't even picked anything to buy yet."

I would be like "Hmmm... ok..." (mumble grumble under my breath and say something along the lines of "If you make a program that even idiots can use, only idiots will use it."). Then I'd write something like this:

Then someone on the team says, "Hey, we should use Optional because that's supposed to be better." So I might say, "Ok..." and refactor to this:

Then I'd go back to that person and review the code. I'd probably say, "So this is better, huh?"  

EDIT: Of course, that person might say, "Did you test this? Because that code is backwards." (I went ahead and edited it from before, where the display logic was backwards. What you're seeing now is the corrected version)  
1 day ago
I don't understand why returning 0.0 when there are no elements to be added is such a big deal or why it would be considered worse than other options. To me, a 0.0 return value would be the LEAST surprising behavior and the most convenient because I wouldn't need to check for an empty collection unless I really needed to.

How much do I have to pay if I have zero items in my shopping cart? A BIG FAT ZERO.

How much tax do I have remit if I have zero taxable items? A BIG FAT ZERO.

If having nothing to add is really a big deal, then I can always do a precheck before calling the method that adds things up:

Why make things any more complicated than it needs to be?
1 day ago

Marlon Mellow wrote:i saw codes that uses methods but i dont know if its applicable to this one? if it is, then do you have suggestions on where to use it?

I suggest you use it everywhere you can break down the problem into a smaller task.

A large task can be broken down into many smaller tasks. Similarly, a long program can be reorganized so that it consists of many short programs. Think of a method as a very short program that does ONE and only one thing.

Methods are great for eliminating duplication. On lines 38 and 55, you have duplicated code. You can eliminate that by extracting that intent out to a method, like so:

Which would make your code easier to read:

1 day ago

Marlon Mellow wrote:any suggestions that doesn't used advanced codes (since it's not allowed) is welcome

I suggest you find better names to use in your code. Names like conDonate, forDonate, forconDonate2, conDonate2, toShowDonate make your code very cryptic and difficult to understand. You're also using way too many temporary variables, so you can actually eliminate some of them.

For example, you wrote this:

Compare that to this:

This also would have prevented a NullPointerException and would simply cause items where you hit "Cancel" to show up as "null".

Better yet, as of Java 8, you can do this:

And if you renamed "donateList" to eliminate the implementation detail "List" in it, your code would be much more self-explanatory and readable:

Tip: Make your code express your intent, not the implementation. This makes code more readable.
1 day ago
This is where you're getting an NPE

It will happen if at any point you hit "Cancel" when prompted to enter the item to donate.

Similarly, you will get an exception if you hit "Cancel" when prompted to enter the money to donate.

Your code is very long and deeply nested. There's a name for this kind of coding horror: Arrow Code
1 day ago
Welcome to the Ranch, Marlon "Balong" Mellow!

Unless you're adding hundreds of thousands of items into the ArrayList, I doubt you're running up against the maximum number of elements an ArrayList can actually hold.

Can you please copy-paste the exact error you're getting so we have a better idea of what's going on?

My lola (that's grandmother for the rest of you) used to call me "Balong" when I was little
1 day ago

Piet Souris wrote:But the Zen is getting worse, here: what is the sound of ZERO  clapping hands?    

At the risk of pushing this even closer to the edge of MD, I'd offer "The same sound that ZERO falling trees make in the forest when ZERO people are around to hear it."
1 day ago
@MS, I agree with just about everything Stephan has advised you except for one small but important detail, which I'm sure was unintentional on Stephan's part: Java is an object-oriented language, not a procedural language, so please learn how to write proper object-oriented programs in Java. Procedural Java programs are just about the worst kind of code I've ever had the misfortune to have to work with. I wouldn't wish that on anyone so please try not to add to that hellspawn if you can.

What I think Stephan meant to say was to stop comparing imperative against functional style programming. That is to say, the style most people learn when programming Java is imperative style (using for-loops, if statements, and other imperative-style flow control structures). Once you've mastered that style and have a solid understanding of what its advantages and disadvantages are, then learn functional style programming where flow control statements for iterations and decisions get abstracted away as the code becomes more declarative.
Don't know why these things happen to me with fair regularity. Was reading the 2nd edition of Extreme Programming Explained: Embrace Change by Kent Beck just now and came upon this:

Kent Beck wrote:... people get passionate about coding style. While there are undoubtedly better styles and worse styles, the most important style issue is that the team chooses to work towards a common style. Idiosyncratic coding styles and the values revealed by them, individual freedom at all costs, don’t help the team succeed.

I guess I shouldn't be surprised though. After all, Kent Beck has been one of my great influences in anything to do with programming.
2 days ago

Wikipedia entry for 0 wrote:Zero is a number which quantifies a count or an amount of null size

Math theory and pedantics aside, either return 0 or throw an exception for an empty collection of numbers. Those are the two most sensible things I can think of doing, the former IMO being the more sensible of the two.
2 days ago