Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!

henry leu

Ranch Hand
+ Follow
since Apr 23, 2016
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 Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by henry leu


Set only contains unique elements.

According to these 2 rules by an author, I try to apply it to the Set. But I don't see how rule #2 work.

Rule 1: If equals() return true when called with two objects, calling hashCode() on each of those objects must return the same result.

Rule 2: If equals() returns false when called with two objects, calling hashCode() on each of those objects does not have to return a different result. This means hashCode() results do not need to be unique when called on unequal objects.

My question: Is it possible that 2 objects have different hash codes and at the same time also the object's values/elements are the same?
                   When adding element to the collection like Set, which method is called first? hashCode() or equals()? I assume hashCode() first because it can quickly locate from cache memory, right? And that's my illustration code below.

For the following example, assuming I have overridden hashCode() and equals() in MyID object.

Line 3: 1st time adding a new MyID object to the set. And the value is 666. First it calls hashCode() and get a hash code. Let's say the hash code is 1. And look up in the hash table. Hash table is empty. So it adds this object to the set. No call to the equals() method.

Line 5: 2nd time adding another MyID object to the set. And the value is also 666. It calls hashcode() and get a hash number let's say 2 (hashCode() mostly returns an unique integer but sometimes same integer).
          Now it looks into the hash table and it doesn't see matching hash numbers. Hash codes are different so Java thinks object values are different/new. It adds to the set without calling the equals() method.
          But now the Set has duplicate "666" values!!!

2 days ago
Thanks Norm!

Comparing floating point numbers using "==" is a  bad idea. There's decimal difference.

This example just appear in a Java book that I bought. I don't understand why author use "==" to compare 2 floating numbers and say they are equal.
1 week ago
I don't understand why I cannot get the absolute values to be equal. It is printing false.
The answer should be true.

Any idea?

1 week ago
Thanks for all your explanations!

Problem solved!
1 week ago

I am reading Jeanne's OCP 1Z0-809 Oracle Certified Professional Java SE 8 Programmer II book.

I ran into a problem on Chapter 3, page 105. I tested the code shown in the book. But it gives me ClassCastException.

Is  down casting allow here? String[] array2 = (String[]) list.toArray(); This returns an Object array.
I know that String[] array2 = list.toArray(new String[0]) works by specifying the String[] array as a parameter for toArray() method. And it will give you a String array.

The error is:

Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String; ([Ljava.lang.Object; and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
at com.packt.learnjava.ch01_start.PrimitiveTypes.main

Here is what I found.

Object[] name = {"Hello"};
String[] result = (String []) name; // Error

For non-array, this works:
Object name = "Hello";
String result = (String) name; // OK

If I change the array creation to new. The code work. See below examples. I don't know why?

Example 1: This is the original code that didn't work.
Casting Object array to String array. 

Example 2: working code
This time I changed the way I declare the String array to use "new".
Casting Object array to String array.

Example 3: working code

This time I cast String array to Object array.

1 week ago

So this is what I perceived. Please advise if this is correct.

Java sees this:

Positive: swap the element
Negative: no swap
Zero: no swap

If obj1 should APPEAR BEFORE obj2 in a sorted list, then we return a negative number. (No swap)
If obj1 should APPEAR AFTER obj2 in a sorted list, then we return a positive number. (Swap)
If we don't care which object comes first (because they might be equal), then we return zero. (no swap)


Yes it is Jeanne and Scott's book.

Here are the exact wordings from the paragraph:

"Comparator<Integer> ints = (i1, i2) -> i1 - i2;

The ints comparator uses natural sort order. If the first number is bigger, it will return a positive number. Try it. Suppose we are comparing 5 and 3. The comparator subtracts 5-3 and gets 2. This is a positive number that means the first number is bigger and we are sorting in ascending order".

I think it should be descending order. Correct me if I am wrong.

If the first number is bigger than the second number and gives you positive, isn't it descending? First number is bigger and second number is smaller...and so on.
In the middle paragraph, it says:

"This is a positive number that means the first number is bigger and we are sorting in ascending order".

I think it should be descending order. Correct me if I am wrong.

If the first number is bigger than the second number and gives you positive, isn't it descending? First number is bigger and second number is smaller...and so on.

I have found solution to my problems.

I visited Jeanne Boyarsky's website and saw her post about a book that she recommended while she took the Java OCP 11 exam. This book is called "The Java Module System" by Nicolai Parlog.
I bought the book. This is a very detailed oriented book. But for a Java beginner like me and I'm also new to this module topic, I could not digest it all. So I contacted the author Nicolai. And he was so kind to answer my question.

I would like to share his explanation below.


* both `javac` and `java` can handle "exploded JAR files" (that's how
  `appmodules\appfuncs` and `appmodules\appstart` are called) on the
  module path
* `javac` requires folder names to match module names
* `java` doesn't care about folder names, only about the module names
  in the module descriptors

I rarely deal with exploded JARs (in the book, I always use `jar` to
package one modules' class files before compiling the next one) and when
I did, I apparently always had folder names that matched module names.
Hence, I never noticed the discrepancy between the last two bullet
points, but they explain what you observe.

In your example, module names (e.g. appfuncsNEW) and folder names (e.g.
`appfuncs`) don't match and so when compiling appstartNew with
`--module-path appmodules`, the compiler sees that appstartNew requires

(1) It checks the "blank" module path `appmodules` but there's
    no `` in there, so
(2) it creates the path `appmodules/appfuncsNEW` from module path
    and module name, but can't find it

Apparently it doesn't scan the folders in `appmodules` for module
descriptors and gives up instead. When you change the compiler command
to `--module-path appmodules/appfuncs`, step (1) finds the module and it

The JVM you launch with `java --modules-path appmodules` is apparently
cleverer in searching for modules and scans the module path for folders
with module descriptors in there and so it finds modules regardless of
the names of the folders that contain them.
2 weeks ago
Hi Guys!

Thanks for all the answers.

I think I found the problem.

And here is my solution.

I was on Windows Powershell. So I need to use the single quote on the class name.
java 'Outer'
java 'Outer$StaticNestedClass'

And in Windows Command Prompt, I can just run it without the single quote.
java Outer
java Outer$StaticNestedClass


How to run main() method from nested static class?

I compile and run both. But they all seem to call the main() method from Outer class only. It only prints "Print from Outer class".

See attached screenshot.

java Outer
java Outer$StaticNestedClass

[Edit: quoted text removed]

Hi JJ,

Thanks for your illustration. It is the same procedure like mine.

Yes, I cannot use "--module-source-path" here because my module  name is not the same as the module folder name.

And this is what I want to point out. Because in Jeanne's book, she has different name for module and folder.

My original questions are:

1. --module-path in my step #2 see above for my original post
   When my module name (not module filename) are the same with the module folder, I can just tell the javac compiler like this "--module-path appmodules" to point to the source root folder that is one level up from my module folder. I don't need to tell javac compile to point to the module folder appfuncs "--module-path appmodules\appfuncs". This will work and compile.

2. But when my module names are different than the module folder name (module name is appfuncsNEW and appstartNEW. Module folders are appfunc and appfuncs), I can't say "--module-path appmodules". Compiler says cannot find module appfuncsNEW. I cannot just point to the source root folder which is one level up the module folder. I need to point one level down to the module folder. So I have to say "--module-path appmodule\appfuncs". This way javac compiler can find my appfuncsNEW module.

3. Also for java JVM, when I run the program using "java", it doesn't matter if my module name is the same as the folder or different than the folder name. I can always use "--module-path appmodule". I don't know how JVM dig into my module folders this way even if names are different.

I just want to know what is happening here.

I've read a lot of resources about Java module. They all use the same module name and same folder name.

I just want to know the theory behind this.


1 month ago

Jeanne Boyarsky wrote:I'm not sure, but I think it is because yo uare just compiling the first module and not jarring it up. So when you try to compile the second module, it doesn't match how the book presents it.

You can see the command to jar (and all commands used in the modules chapter of the book here)

Yes, by making .class to JAR file, the program can compile and run.

But I thought Java also allows program to compile and run it in loose classes.

1 month ago
I still cannot resolved this problem.
1 month ago

Jeanne Boyarsky wrote:

henry leu wrote:Step #1: compile OK! & (These files have no dependencies on module. So I don't use --module-path.)
javac -d appmodules\appfuncs

Is the exact command you ran? You  have in the file layout description in your post, but here.

Sorry, that was a typo.

It should be

I ran with
1 month ago