Kohei Nozaki's blog 

Entries tagged [book]

What I learned from the book Core Java for the Impatient


Posted on Friday Jun 19, 2015 at 06:07PM in Java


I’m reading a book named Core Java for the Impatient. I leave some notes that what I learned from that book.

Array construction

You can omit the new int[] statement when you declare a variable as follows:

int[] array = {1, 2, 3};

You can’t omit it when you don’t declare a variable at the same time (in other words, when you use an existing variable).

For example, the following is illegal:

int[] array;
array = {2, 3, 4};

You need to write like the following instead:

int[] array;
array = new int[]{2, 3, 4};

NumberFormat

The NumberFormat class provides some useful text formatting functions for numeric variables.

Currency formatting

final BigDecimal val1 = new BigDecimal("0.126");
final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(currencyInstance.format(val1));

Yields:

$0.13

Percentage formatting

final BigDecimal val2 = new BigDecimal("0.126");
final NumberFormat percentInstance = NumberFormat.getPercentInstance(Locale.US);
System.out.println(percentInstance.format(val2));

Yields:

13%

They automatically adds the symbol and round up on 6 and round down on 5.

Wildcard for the classpath

Consider a class which depends on the class corejava.jar1.Foo and corejava.jar2.Bar:

package corejava.classpath;

public class Main {
    public static void main(String[] args) {
        System.out.println(new corejava.jar1.Foo());
        System.out.println(new corejava.jar2.Bar());
    }
}

You have these classes in separated three jar files in the /tmp/jar directory as follows:

$ ls -l /tmp/jar
total 24
-rw-r--r--  1 kyle  wheel  2192 Jun 19 16:06 classpath-1.0-SNAPSHOT.jar
-rw-r--r--  1 kyle  wheel  1962 Jun 19 16:04 jar1-1.0-SNAPSHOT.jar
-rw-r--r--  1 kyle  wheel  1962 Jun 19 16:04 jar2-1.0-SNAPSHOT.jar

In this setup, you can specify all of three jar files with the wildcard * as follows:

$ java -cp '/tmp/jar/*' corejava.classpath.Main
corejava.jar1.Foo@511d50c0
corejava.jar2.Bar@5e2de80c

Note that you can’t use complex wildcard that smarter UNIX shells recognize. in other words, it doesn’t recognize patterns like *.jar. for example, the following command won’t work:

$ java -cp '/tmp/jar/*.jar' corejava.classpath.Main
Error: Could not find or load main class corejava.classpath.Main

Including an image to javadoc

Consider you have a class named Dog in a Maven project. you want to include an image of a dog named chihuahua.jpg to the javadoc of the class. in such case you can write the class as follows:

package corejava.main;

/**
 * The class represents a dog. <br> <img src="doc-files/chihuahua.jpg" alt="a cute chihuahua dog">
 */
public class Dog {

    /**
     * The dog will say woof.
     */
    public void bark() {
        System.out.println("Woof!");
    }
}

Next, put the chihuahua.jpg in src/main/javadoc/corejava/main/doc-files. structure under the src/main will be:

.
|-- java
|   `-- corejava
|       `-- main
|           `-- Dog.java
|-- javadoc
|   `-- corejava
|       `-- main
|           `-- doc-files
|               `-- chihuahua.jpg
`-- resources

Then execute mvn javadoc:javadoc in the top of the project directory. javadoc will be produced in the target/site/apidocs directory. open index.html in that directory and go to the documentation of Dog class, you will see the image of a dog as follows:

a0e5b322 584d 4e5d a246 e6f824d1085c

Consider you have a class named Cat:

package corejava.main;

/**
 * Represents a cat.
 */
public class Cat {

    /**
     * The cat starts meowing.
     *
     * @see corejava.main.Dog#bark()
     * */
    public void meow() {
        System.out.println("Meow!");
    }
}

This produces the following javadoc:

34963f24 9e7f 4367 a56f b9a464553211

There are some of more useful syntaxes such as link to external URLs.

Method references

The following three statements produce identical Comparator:

Comparator<String> c1 = new Comparator<String>() {
    @Override
    public int compare(String x, String y) {
        return x.compareTo(y);
    }
};
Comparator<String> c2 = (x, y) -> x.compareTo(y);
Comparator<String> c3 = String::compareTo;

Constructor references

Constructor references are useful with Streams. consider you have a class named Dog:

public class Dog {
    private String name;

    public Dog(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Dog{" + "name='" + name + '\'' + '}';
    }
}

You can convert a List<String> that contains name of dogs to a Stream<Dog> as follows:

List<String> dogs = Arrays.asList("Snoopy", "Spike", "Olaf");
Stream<Dog> dogStream = dogs.stream().map(Dog::new);

If you need to create a typed array from that Stream<Dog>, you can use following idiom:

Dog[] dogArray = dogStream.toArray(Dog[]::new);


Progress of reading 2 #packt5dollar - thoughts about build tool


Posted on Friday Jan 23, 2015 at 09:51PM in Technology


I haven’t read IntelliJ IDEA Essentials yet except some parts of Run configuration but it gives me that better view of the mechanism to run an application on the application server or standalone. I read that with Mastering Apache Maven 3 at same time. I didn’t understand particulary how my application will be built and run, but these books improved my understanding drastically.

I guess that every beginner Java programmer would better to learn that how build tool and Java program works through development with simple text editor and cli of build tool before start using a modern IDE. also launching an application server and deploying their application with build tool is good for server-side programmer. it’s important to know what each tool done. I don’t know how much waste my time for stucking in various problem of automated build and deploy mechanism. this can be avoided with correct understanding of the mechanism.

I realized how Maven has many function and flexibility. the book gave me basic knowledge about it but still I have to learn more advanced topics such as real-world use case which used multi module scheme. there are more Maven books published that mentioned about more specific topic so I would read these books later.

Also I should look back that book sometimes because I might misunderstood or simply couldn’t understand at some part of the book. also there are many plugins that the book didn’t mentioned comprehensively (I know it’s impossible for only one book) so I need to learn about it on the web or another books.


Progress of reading #packt5dollar


Posted on Monday Jan 19, 2015 at 08:48PM in Technology


I bought iPad Air 2 for reading eBooks that bought at #packt5dollar sale. it gave me better experience for eBooks reading than my MacBook Pro Retina 15". I prefer epub than PDF because epub can adjust font size freely and I like continuous scroll mode in iBooks. PDF is well-layouted but some of PDF has wider margin at around the content and fonts are too small still.

I finished these books:

  • WildFly Configuration, Deployment, and Administration - Second Edition

    Description about deployment mechanism was good for me because I did’t know how to deploy applications as exploded. the book has many description for large scale use case such as domain, clustering and load-balancing but that doesn’t make sense for me much because I used WildFly in standalone. also sections of detailed configuration are good, especially a section about web container Undertow. I would look back this book as a reference sometimes later.

  • Responsive Web Design with HTML5 and CSS3

    I’m terrible at client side web technology but the book doesn’t demand much background knowledge so I think I understand most. my knowledge level is something like that I can barely create typical multi-column website which fixed width and non responsive with struggling. I learned such things: how responsive design and fluid layout are important, and basic of HTML5/CSS3 (including media queries) and how to apply polyfill to these latest specs for older web browsers such as IE 6/7/8. at first, I would make my blog to responsive for practice.


Purchased book list in #packt5dollar sale


Posted on Tuesday Jan 06, 2015 at 11:18PM in Technology


The list grown rapidly and widely covered my interests. it's hard to pick one to read first and maybe I need iPad or something because I have too much to read...

  • Mastering JavaServer Faces 2.2
  • JBoss Weld CDI for Java Platform
  • Mastering Apache Maven 3
  • WildFly Configuration, Deployment, and Administration - Second Edition
  • Git Best Practices Guide
  • Jenkins Continuous Integration Cookbook - Second Edition
  • Responsive Web Design with HTML5 and CSS3
  • IntelliJ IDEA Essentials
  • PostgreSQL 9 Admin Cookbook
  • Highcharts Essentials
  • Learning OpenShift
  • OpenShift Cookbook
  • Apache Roller 4.0 – Beginners Guide

I'm surprised that many WildFly books are published from Packt Publishing. also I found Apache Roller book so I bought it too because I'm a beginner of Roller. it targets to version 4.0 and sounds little obsolete, but it would make sense for me.

I found someone wrote that $5 sale is held for every year-end holidays at Packt Publishing so I would buy many books again in next time if the sale will be held surely.


Recent technical book wishlists


Posted on Friday Jan 02, 2015 at 10:45PM in Technology


I'd like to study more about these technologies: Java EE 7(JSF, JPA, CDI and more), HTML5, CSS3, and related ones.

Java EE:

Others: And these discussions are really interesting because I have only few space for books: I prefer paper books than eBooks after some eBooks experience because eBooks are simply hard to read for me even though I have 15inch Retina MacBook Pro. and I don't want to be worried about which eBook reader is best. but paper books are expensive and needs many space so I still don't know which one is better.

References:

  1. What is the best reference book for HTML5 and CSS3? - Quora
  2. Inspired by Actual Events: Book Review: JBoss Weld CDI for Java Platform