Eclipse - Disconnect project from SVN

To disconnect project from SVN, right-click on the project and select Team->Disconnect.

Java - Find the degree of a slope

/**
 * Show how to find the degree of a slope.
 * @author Xuan Ngo
 */
public class Trigonometry
{
 
  public static void main(String[] args)
  {
    /**
     * All trigonometric functions in java.lang.Math return values in radians.
     * Therefore, you can use the Math.toDegrees() to convert to degree.
     * The inverse of sin, cosine and tangent are arcsine, arccosine and arctangent.
     */
    double dSlope = 1; // 1=y/x=1/1
    double dRadian = Math.atan(dSlope);
    double dDegree = Math.toDegrees(dRadian);
 

SQL - Create indexes to speed up SQL query

-- Check what indexes are already defined for a table.
SHOW INDEX FROM tablename;
 
-- Create the index.
CREATE INDEX index_name ON tablename(col1, col2, ...);

SQL - Put a table into memory

CREATE TABLE new_table ENGINE=MEMORY AS SELECT * FROM old_table;

SQL - Get the spaces used by each database

SELECT table_schema "Database Name"
     , sum( data_length + index_length ) / 1048576  AS "Database Size in MB"
     , sum( data_free )/ 1048576  AS "Free Space in MB"
FROM information_schema.TABLES
GROUP BY table_schema ;

Output:

+--------------------+---------------------+------------------+
| Database Name      | Database Size in MB | Free Space in MB |
+--------------------+---------------------+------------------+
| bt_96580           |              1.1242 |           0.0014 |

Java - Take screenshots of the full screen

import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
 
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import javax.imageio.ImageIO;
 
/**
 * Take screenshots of the full screen.
 * @author Xuan Ngo
 */
public class PrintScreen
{
  public static void main(String[] args)
  {
    // Print help how to use this application.
    if (args.length != 3)
    {

Java - Performance of primitive data type vs object data type

Using the object data type instead of its primitive counter part is very expensive. In fact, object data type is approximatively 43 times slower than its primitive counter part. However, I have to scale up to 1 billion iterations to see the difference.

/**
 * Performance of primitive data type vs object data type.
 * @author Xuan Ngo
 *
 */
public class PrimitiveVsObject
{
  public static void main(String[] args)
  {
    long lStart = 0;
    long lEnd = 0;
    int lMaxSize = 1000000000; // 1 billion.
 
    // Primitive data type

SQL - Data type int, double, float are not nullable

When you use primitive data types(i.e int, double and float) with hibernate, you will not be able to convert them to null because Java doesn't allow them to be null. In order for these data types to be null, use their object counter part.(i.e Integer, Double, Float). However, it is very expensive to use the object data type. Therefore, use them only when needed.

Java - Paramterized String

/**
 * Add variable values in a string.
 * @author Xuan Ngo
 *
 */
public class ParamterizedString
{
  public static void main(String[] args)
  {
    String[] aAnimals = new String[]{"horse", "lion", "zebra", "elephant"};
    String s = String.format("The zoo contains: %s, %s, %s, %s.", aAnimals);
    System.out.println(s); // The zoo contains: horse, lion, zebra, elephant.
  }
 
}

Eclipse - Export/Import code style formater

It is located at Window->Preferences->Java->Code Style->Formater.

Syndicate content