Monday, July 28, 2014

Convert integer to String base on various radix


package java_stringof;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class Java_StringOf {

    public static void main(String[] args) {
        int src = 0xFF;
        System.out.println("radix=2: " + Integer.toString(src, 2));
        System.out.println("radix=8: " + Integer.toString(src, 8));
        System.out.println("radix=10: " + Integer.toString(src, 10));
        System.out.println("radix=16: " + Integer.toString(src, 16));
    }
    
}

Sunday, July 27, 2014

Divide a number (int, float, double) by zero

Here show that if you divide a int by zero, it will throw ArithmeticException of by zero. But it you divide a float or double by zero, no exception will be thrown. It return Infinity or -Infinity, and equal to POSITIVE_INFINITY or NEGATIVE_INFINITY.


package java_dividebyzero;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class Java_DivideByZero {

    public static void main(String[] args) {
        
        System.out.println("\n--- Test Divide By Zero on int: 100/0 ---");
        int int1 = 100;
        int intResult;
        
        try{
            intResult = int1/0;
            System.out.println(intResult);
        }catch (ArithmeticException ex){
            System.out.println("catched: " + ex);
        }
        
        System.out.println("\n--- Test Divide By Zero on float: 100/0 ---");
        float float1 = 100;
        float resultFloat;
        
        try{
            resultFloat = float1/0;
            System.out.println(resultFloat);
            System.out.println("resultFloat == Float.POSITIVE_INFINITY : " + (resultFloat == Float.POSITIVE_INFINITY));
            System.out.println("resultFloat == Float.NEGATIVE_INFINITY : " + (resultFloat == Float.NEGATIVE_INFINITY));
            System.out.println("resultFloat == Float.NaN : " + (resultFloat == Float.NaN));
        }catch (ArithmeticException ex){
            System.out.println("catched: " + ex);
        }
        
        System.out.println("\n--- Test Divide By Zero on double: -100/0 ---");
        double double1 = 100;
        double resultDouble;
        try{
            resultDouble = -1 * double1/0;
            System.out.println(resultDouble);
            System.out.println("resultDouble == Double.POSITIVE_INFINITY : " + (resultDouble == Double.POSITIVE_INFINITY));
            System.out.println("resultDouble == Double.NEGATIVE_INFINITY : " + (resultDouble == Double.NEGATIVE_INFINITY));
            System.out.println("resultDouble == Double.NaN : " + (resultDouble == Double.NaN));
        }catch (ArithmeticException ex){
            System.out.println("catched: " + ex);
        }
    }
    
}

Wednesday, July 23, 2014

Check and parse number from String


package javafx_parsenum;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ParseNum extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        TextField textField = new TextField ();
        Label answer1 = new Label();
        Label answer2 = new Label();
        Label answer3 = new Label();
        
        Button btnCheck = new Button();
        btnCheck.setText("- Check -");
        btnCheck.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                
                String q = textField.getText();

                try{
                    double parseDouble = Double.parseDouble(q);
                    answer1.setText("Double: " + String.valueOf(parseDouble));
                }catch(NumberFormatException ex){
                    answer1.setText("Not a valid Double");
                }
                
                try{
                    float parseFloat = Float.parseFloat(q);
                    answer2.setText("Float: " + String.valueOf(parseFloat));
                }catch(NumberFormatException ex){
                    answer2.setText("Not a valid Float");
                }
                
                try{
                    int parseInt = Integer.parseInt(q);
                    answer3.setText("Integer: " + String.valueOf(parseInt));
                }catch(NumberFormatException ex){
                    answer3.setText("Not a valid Integer");
                }

            }
        });
        
        VBox vBox = new VBox();
        vBox.setSpacing(5);
        vBox.getChildren().addAll(textField, btnCheck,
            answer1, answer2, answer3);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 560, 315);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Tuesday, July 22, 2014

Beginning Programming with Java For Dummies

A practical introduction to programming with Java

Beginning Programming with Java For Dummies (For Dummies (Computer/Tech))

Beginning Programming with Java For Dummies, 4th Edition is a comprehensive guide to learning one of the most popular programming languages worldwide. This book covers basic development concepts and techniques through a Java lens. You'll learn what goes into a program, how to put the pieces together, how to deal with challenges, and how to make it work. The new Fourth Edition has been updated to align with Java 8, and includes new options for the latest tools and techniques.

Java is the predominant language used to program Android and cloud apps, and its popularity is surging as app demand rises. Whether you're just tooling around, or embarking on a career, Beginning Programming with Java For Dummies, 4th Edition is a great place to start. Step-by-step instruction, easy-to-read language, and quick navigation make this book the perfect resource for new programmers. You'll begin with the basics before moving into code, with simple, yet detailed explanations every step of the way.

Topics include:
  • Learn the language with sample programs and the Java toolkit
  • Familiarize yourself with decisions, conditions, statements, and information overload
  • Differentiate between loops and arrays, objects and classes, methods and variables
The book also contains links to additional resources, other programming languages, and guidance as to the most useful classes in the Java API. If you're new to programming languages, Beginning Programming with Java For Dummies, 4th Edition provides the instruction and practice you need to become a confident Java programmer.

Tuesday, July 15, 2014

Java For Dummies, 6th edition - updated for Java 8

Java For Dummies (For Dummies (Computer/Tech))

Learn to:
  • Combine several smaller programs to create a bigger program
  • Use new features and tools in Java 8
  • Create basic Java objects and reuse code
  • Handle exceptions and events
Jump into Java! The bestselling Java book for beginners is now fully updated for Java 8

Java is everywhere, runs on almost any computer, and is the engine that drives the coolest applications. If you’ve always wanted to learn Java, Java For Dummies is your ticket to Java success! Featuring updates on Java 8, this hands-on guide is written in an easy-to-read manner and is the perfect resource to get you running with Java in a jiffy!
  • The Java scoop — get an overview of Java, featuring new tools and enhancements in Java 8, including new libraries, parallel frameworks, and more
  • Building blocks — find out how to work with Java classes and methods
  • Get loopy — understand the value of variables and learn to control program flow with loops or decision-making statements
  • Stay classy — explore classes and objects, constructors, and subclasses, and find out how to reuse code
  • A click ahead — jump into variables, use arrays and collections, and create programs that respond to mouse clicks

Open the book and find:
  • Definitions of terms associated with Java
  • Java syntax and an object-oriented programming overview
  • How to save time and effort by reusing code
  • All about if, for, switch, and while statements
  • The enhanced multimedia capabilities of Java 8
  • Updates for Android and other new tools
  • How to write Java applets
  • Tips to avoid common mistakes

Tuesday, July 8, 2014

Java 8 and JavaFX

Stuart Marks shows you how to effectively use Lambda expressions, Streams, Date & Time, etc to make your life easier.

Saturday, July 5, 2014

Scala for Java Developers


Scala for Java Developers

Build reactive, scalable applications and integrate Java code with the power of Scala

Overview
  • Learn the syntax interactively to smoothly transition to Scala by reusing your Java code
  • Leverage the full power of modern web programming by building scalable and reactive applications
  • Easy to follow instructions and real world examples to help you integrate java code and tackle big data challenges
In Detail

Scala for Java Developers is a step-by-step guide full of easy-to-follow code taken from real-world examples explaining the migration and integration of Scala in a Java project.

With this book, you will first get comfortable with the Scala syntax and its Java-like ecosystem, and then dive into new ways of building reactive web apps using the Typesafe stack including the actor-based Akka framework, the Play web framework, and the emerging Slick framework for persistence. The book will then teach you how to review useful tools for unit, integration, and functional testing; demonstrate how integrating with external systems applies to the Scala world and what its benefits are.

From learning the Scala syntax interactively to writing modern, scalable, reactive applications, this book will help you to take your skills to the next level by solving complex problems in a concise and maintainable way.

What you will learn from this book
  • Apply and control the Scala Ecosystem
  • Migrate Java code to Scala
  • Discover Play Framework web development
  • Test data using Scala's testing frameworks
  • Manipulate XML and JSON in Scala
  • Learn the Scala syntax interactively
  • Integrate Java projects in Scala
  • Build reactive web apps using the Typesafe stack
  • Use new systems including the Akka framework, the Play web framework, and the emerging Slick framework
  • Tackle big data challenges
Approach

This step-by-step guide is full of easy-to-follow code taken from real-world examples explaining the migration and integration of Scala in a Java project.

Who this book is written for

If you are a Java developer or a Java architect working with Java EE-based solutions and want to start using Scala in your daily programming, then this book is ideal for you. This book will get you up and running quickly by adopting a pragmatic approach with real-world code samples. No prior knowledge of Scala is required.

Thursday, July 3, 2014

Java: A Beginner's Guide, Sixth Edition



Essential Java Programming Skills--Made Easy!

Java: A Beginner's Guide, Sixth Edition

Fully updated for Java Platform, Standard Edition 8 (Java SE 8), Java: A Beginner's Guide, Sixth Edition gets you started programming in Java right away. Bestselling programming author Herb Schildt begins with the basics, such as how to create, compile, and run a Java program. He then moves on to the keywords, syntax, and constructs that form the core of the Java language. This Oracle Press resource also covers some of Java's more advanced features, including multithreaded programming, generics, and Swing. Of course, new Java SE 8 features such as lambda expressions and default interface methods are described. An introduction to JavaFX, Java's newest GUI, concludes this step-by-step tutorial.

Designed for Easy Learning:
  • Key Skills & Concepts -- Chapter-opening lists of specific skills covered in the chapter
  • Ask the Expert -- Q&A sections filled with bonus information and helpful tips
  • Try This -- Hands-on exercises that show you how to apply your skills
  • Self Tests -- End-of-chapter quizzes to reinforce your skills
  • Annotated Syntax -- Example code with commentary that describes the programming techniques being illustrated
The book's code examples are available FREE for download.

Wednesday, July 2, 2014

Java: The Complete Reference, Ninth Edition



Fully updated for Java SE 8

Java: The Complete Reference, Ninth Edition

Java: The Complete Reference, Ninth Edition explains how to develop, compile, debug, and run Java programs. Bestselling programming author Herb Schildt covers the entire Java language, including its syntax, keywords, and fundamental programming principles, as well as significant portions of the Java API library. JavaBeans, servlets, applets, and Swing are examined and real-world examples demonstrate Java in action. New Java SE 8 features such as lambda expressions, the stream library, and the default interface method are discussed in detail. This Oracle Press resource also offers a solid introduction to JavaFX.

Coverage includes:

  • Data types, variables, arrays, and operators
  • Control statements
  • Classes, objects, and methods
  • Method overloading and overriding
  • Inheritance
  • Interfaces and packages
  • Exception handling
  • Multithreaded programming
  • Enumerations, autoboxing, and annotations
  • The I/O classes
  • Generics
  • Lambda expressions
  • String handling
  • The Collections Framework
  • Networking
  • Event handling
  • AWT and Swing
  • The Concurrent API
  • The Stream API
  • Regular expressions
  • JavaFX
  • JavaBeans
  • Applets and servlets
  • Much, much more