# 聊一聊Java中double精度去哪了(1)

``````package com.iovi.math;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* @author sunnycomes
*
*/
public class MathUtil {

/**
* Return a double whose value is a add b and the scale is specified to 4.
*
* @param a the first double value
* @param b value to be added to a
* @return a + b, the scale is 4
*/
public static double add(double a, double b) {
BigDecimal aa = new BigDecimal(a + "");
BigDecimal bb = new BigDecimal(b + "");

aa.setScale(4, RoundingMode.FLOOR);

return aa.doubleValue();
}

/**
* Return a double whose value is a subtract b and the scale is specified to 4.
*
* @param a the first double value
* @param b value to be subtracted from a
* @return a - b, the scale is 4
*/
public static double subtract(double a, double b) {
BigDecimal aa = new BigDecimal(a + "");
BigDecimal bb = new BigDecimal(b + "");

aa = aa.subtract(bb);
aa.setScale(4, RoundingMode.FLOOR);

return aa.doubleValue();
}

/**
* Return a double whose value is a multiply b and the scale is specified to 4.
*
* @param a the first double value
* @param b value to be multiply by a
* @return a * b, the scale is 4
*/
public static double multiply(double a, double b) {
BigDecimal aa = new BigDecimal(a + "");
BigDecimal bb = new BigDecimal(b + "");

aa = aa.multiply(bb);
aa.setScale(4, RoundingMode.FLOOR);

return aa.doubleValue();
}

/**
* Return a double whose value is a divide b and the scale is specified to 4.
*
* @param a the first double value
* @param b value by which a is to be divided
* @return a + b, the scale is 4
*/
public static double divide(double a, double b) {
BigDecimal aa = new BigDecimal(a + "");
BigDecimal bb = new BigDecimal(b + "");

aa = aa.divide(bb);
aa.setScale(4, RoundingMode.FLOOR);

return aa.doubleValue();
}
}
``````

# 打破沙锅问到底－－Java的@符号

``````@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLString {
int value() default 0;
String name() default "";
Constraints constraints() default @Constraints;
}
``````

``````Annotation type declarations are similar to normal interface declarations.

An at-sign (@) precedes the interface keyword. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause.

Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types. Methods can have default values.
``````

# 抛硬币也能抛出人生大赢家

``````1:如果前一次赢了，本次下注金额为1元。
2:如果前一次输了，本次下注金额为之前两倍，这样做到本次赢了，能把之前输的都回本。
3:第一次玩时，认为前一次是赢的，因此，开局赌注为1元。
``````

``````package com.iovi.flippingcoins;

/**
* @author sunnycomes
*
*/
public class FlippingCoins {

public static final int INIT_FUNDS = 10000;
public static final int PREFERED_FUNDS = INIT_FUNDS * 2;

public static final int MAX_TEST_CASE_CNT = 100;

// This value is related to PREFERED_FUNDS if you think carefully.
public static final int MAX_FLIPPING_CNT_IN_ONE_TEST_CASE = 20000;

public static void main(String[] args) {

for(int testCnt = 0; testCnt < MAX_TEST_CASE_CNT; testCnt++) {

int funds = INIT_FUNDS;
int lastResult = 1;
int lastChip = 1;
int flipCnt = 0;

while(funds <= PREFERED_FUNDS) {

if(flipCnt >= MAX_FLIPPING_CNT_IN_ONE_TEST_CASE) {
//System.err.print("Max flipping count in one test case reached#");
break;
}
flipCnt ++;

lastChip = lastResult == 1 ? 1 : lastChip * 2;
if(funds < lastChip) {
//System.err.print("No enough funds for next round#");
break;
}

lastResult = getResult();
funds = lastResult == 1 ? funds + lastChip : funds - lastChip;
}

if(funds > PREFERED_FUNDS) {
System.out.println("flipCnt=" + flipCnt + "#funds=" + funds);
}

//System.out.println("flipCnt=" + flipCnt + "#funds=" + funds);
}
}

/**
* The method is designed to create a random result simulating the coin flipping result.
*
* @return a pseudorandom int either 1 or 0, 1 stands for win and vice versa.
*/
public static int getResult() {
return (int) (Math.random() + 0.5);
}
}
``````

# Simple Git Guide

## Preliminary

### Remote

If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with

``````git remote add repo_path
``````

repo_path can be something like `https://github.com/sunnycomes/okcoin_pc`.

### branch

Branches are used to develop features isolated from each other. The master branch is the “default” branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.

create a new branch named “branch_xx”,

``````git branch branch_xx
``````

and switch to it using

``````git checkout branch_xx
``````

or,

``````git checkout -b branch_xx
``````

do the same work.

# Java Performance Tuning Tips

The 1st version is completed on 2013.12.11, the main content is from Performance Tuning of Java Applications.

Ever since the first version of Java Technology hit the streets, performance has been an important issue for Java developers. Java has improved dramatically and continually but, performance tuning is very essential to get the best results, especially when we think of J2EE applications.

# Best Practices to Improve Performance in JDBC

The 1st version is completed on 2013.11.11 20:38.

The 2nd version is completed on 2013.11.22, which modified completely, the main content is from Best practices to improve performance in JDBC, this is a great article about JDBC tuning.

This post is dedicated to illustrate techniques for optimizing JDBC API-based calls from the Java platform. As a result of this presentation, you will:

• Design better JDBC implementations
• Recognize potential performance bottlenecks
• Reduce cost during development

# Optimal JDBC Transaction Isolation Level

## Choose optimal isolation level

Isolation level represent how a database maintains data integrity against the problems like dirty reads, phantom reads and non-repeatable reads which can occur due to concurrent transactions. java.sql.Connection interface provides methods and constants to avoid the above mentioned problems by setting different isolation levels.

``````public interface Connection {
public static final int  TRANSACTION_NONE                 = 0
public static final int  TRANSACTION_READ_UNCOMMITTED     = 1
public static final int  TRANSACTION_READ_COMMITTED       = 2
public static final int  TRANSACTION_REPEATABLE_READ      = 4
public static final int  TRANSACTION_SERIALIZABLE         = 8
int    getTransactionIsolation();
void   setTransactionIsolation(int isolationlevelconstant);
}
``````