Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!

henry leu

Ranch Hand
+ Follow
since Apr 23, 2016
Cows and Likes
Cows
Total received
3
In last 30 days
1
Total given
0
Likes
Total received
11
Received in last 30 days
0
Total given
22
Given in last 30 days
7
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by henry leu

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 day 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 day ago
Thanks for all your explanations!

Problem solved!
3 days ago
Hi,

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.

3 days ago
Hi,

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)

Example:



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

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

(1) It checks the "blank" module path `appmodules` but there's
    no `module-info.java` 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
compiles.

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.
1 week 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.
javac Outer.java
java 'Outer'
java 'Outer$StaticNestedClass'

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

Hi,

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.

javac Outer.java
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.


Thanks,

Henry
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! modules-info.java & SimpleMathFuncs.java. (These files have no dependencies on module. So I don't use --module-path.)
javac -d appmodules\appfuncs
appsrc\appfuncs\modules-info.java
appsrc\appfuncs\appfuncs\simplefuncs\SimpleMathFuncs.java



Is the exact command you ran? You  have module-info.java in the file layout description in your post, but modules-info.java here.



Sorry, that was a typo.

It should be module-info.java.

I ran with module-info.java.
1 month ago
Last sentence:

The second example only gives that privilege to the zoo.staff package.
It should be zoo.staff module.
zoo.staff is a module. Not a package.