cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Cloud



Etc


Reference














.

programming:java:index.html



Programming Languages

Java

References

Documentation

Java APIs

Tutorial


Install

Framework

Framework Memo
Apache Struts http://struts.apache.org/
Spring Framework http://www.springsource.org/
JSF
log4j http://logging.apache.org/




Grammar

Basic

class

public class Sample {

}

main

public class Sample {

    public static void main( String[] args ) {
         //
     }

}

System.out.println

System.out.println( "Hello world" );

Comment

//aaaa

/*
  aaaaaaa
  bbbbbb
*/

/**
   JavaDoc
*/


Variables

int num;

int num;
char c;
float value;
double value;
boolean flag;

String s;
Date d;
String[] array;

int i = 2;
int i = 100000000;
float num = 1.234f;
double num = 1.234;

num = 1 + 1;
num = 1 - 1;
num = 1 * 2;
num = 1 / 2;

num = 1 / 2;  // 0
num = 1.0 / 2;    // 0.5
num = 1 / 2.0;    // 0.5
num = 1.0 / 2.0;  // 0.5

String str = "abc";
String join = "aaa" + "bbb";

String[] record = "aaa,bbb,ccc".split( "," );

int length = "abcdef".length();

"abcd".substring( 0, 2 )   // abc

int result = "abcd".indexOf( "cd" ) 

Array

int[] array;

int [] array;

array = new int[5];

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

int[] array2 = new int[5];

array[0]
array[1]

array[0] = 1;
array[1] = 2;

Conditionals

if

if ( condition ) {

}

if else

if ( condition ) {

} else {

}

if else if

if ( condition ) {

} else if ( condition ) {

}

while

int i = 0;
while ( i < 5 ) {
    
    //
    
    ++i;
}

for

for ( int i = 0; i < 5; ++i ) {

}

for each

int[] fields = new int[] { 1, 2, 3 };

for ( int field: fields ) {

}

method

static int sum( int num1, int num2 ) {
    int total;

    total = num1 + num2;

    return total;
}

File

import java.io.*;

Input

BufferedReader reader = null;

try {
    reader = new BufferedReader( new FileReader( filename ) );

    String line;
    while ( ( line = reader.readLine() ) != null ) {

    }

} catch ( IOException e ) {
    //
    
} finally {
    if ( reader != null ) {
        try {
        	reader.close();
        } catch ( IOException e ) {}s
    }
}

Output

PrintWriter writer = null;

try {
    writer = new PrintWriter( new BufferedWriter( new FileWriter( filename ) ) ); 

    writer.println( "abc" );
    writer.println( "def" );
    writer.println( "fgh" );

} catch ( IOException e ) {
    //
    
} finally {
    if ( writer != null ) {
        writer.close();
    }
}




programming/java/index.html.txt ยท Last modified: 2015/07/12 by admin

Page Tools