Home Java
Java PDF Print E-mail
Written by Willy Gerber   
Saturday, 21 February 2009 12:32
Java Tutorial from Sun

Variables 

type variable = value;

Dynamic variable definition, for example

int a=2;

double x, y; 


type static variable

Static variable definition (fix memory location)

byte
short
int
long
float
double
boolean
String (or any object)
char

Formats

type[] variable;

Array definition, for example

int[] anArray;

String[] buffer = new String[10]; 


anArray = new int[10];
anArray[0] = 100;

Value assignment

int[] anArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},{"Smith", "Jones"}};

Array value assignment

byte[] anArray;
short[] anArray;
long[] anArray;
float[] anArray;
double[] anArray;
boolean[] anArray;
char[] anArray;
String[] anArray;

Other type of array definitions

java.awt.Color.red
java.awt.Color.white
java.awt.Color.blue

Color

java.text.NumberFormat formatter = new java.text.DecimalFormat("0.0E00");
stext = formatter.format(fvalue);


Format
Operators 

expr++

expr=expr + 1

expr--

expr=expr - 1

++expr

 

--expr

 

+expr

 

-expr

 

* / %

Multiplicative

+ -

Additive

== !=

Equality

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

<<

Signed left shift

>>

Signed right shift

>>>

Unsigned right shift

< > <= >=

instanceof relational

~

Unary bitwise complement

&

Bitwise AND

^

Bitwise exclusive OR

|

Bitwise inclusive OR

&&

Logical AND

||

Logical OR

?:

Ternary

=

 

+=

 

-=

 

*=

 

/=

 

%=

 

&=

 

^=

 

|=

 

<<=

 

>>=

 

>>>=

Assignment

instanceof

Compares an object to a specified type

static double variable

Variables that are common to all objects

static final double PI = 3.141592653589793;

Constants

Enum Types
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

Arrays of fix values
Functions 

public type function( arguments ) {}

Public function

privat type function( arguments ) {}

Privat function

public void function() {}

public int function() {}

public double function(int a, double b) {}

Examples of functions

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

Function with an array argument
Control Flow Statements  

if (condition) statement1;

if-then, for example

if(a>0){ n = 1; } 


if (condition) { statement1; }
else { statement2; }

if-then-else, for example

if(a>0){ n = 1; }else{ n = 0; } 


if (condition) { statement1; }
else if { statement2; }
else { statement3; }

if-then-else if-then-else

switch (variable) {
case 1: statement1; break;
case 2:
case 3: statement2; break;
default: statement3; break;
}

Switch Statement

while (expression) {
    statement(s)
}

While Statements

do {
    statement(s)
} while (expression)

Do-while Statements

for (initialization; termination; increment) {
    statement(s)
}

for Statement

for/while-do loop {
    if (conditions) { break; }
}

Break Statement (unlabeled break)

label: statement;

for/while-do loop {
    if (conditions) { break label; }
}
statement(s)

Break Statement (labeled break)

for/while-do loop {
    if (conditions) { continue; }
}

Continue Statement: goes back to the loop condition

return;
return variable;

Exist the program with or without returning a value
Classes 

public class class_name {
    fields
    constructor
    methods
}

Structure, constructor builds initial fields, methods acts on fields in application of the class

class MyClass extends MySuperClass implements YourInterface {
    fields
    constructor
    methods
}

Extends an existing class

public class class_name {
  privat:
    fields
  public:
    constructor
    methods
}

Differentates between public and privat fields/methods

methods ()
methods (int x)

Posibility of calling the same method in different ways (Overloading Methods)

public class Point {
    public int x = 0;
    public int y = 0;
    //constructor
    public Point(int a, int b) {
    x = a;
    y = b;
    }
}

Initializing an Object; declaration uses the same name as the class and it has no return type

objectReference.methodName(argumentList);

Call a method

public class Point {
    public int x = 0;
    public int y = 0;
    
    //constructor
    public Point(int x, int y) {
    this.x = x;
    this.y = y;
    }
}

Us of this with a Field

public class Rectangle {
  private int x, y;
  private int width, height;
  
  public Rectangle() {
    this(0, 0, 0, 0);
  }
  public Rectangle(int width, int height) {
    this(0, 0, width, height);
  }
  public Rectangle(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }
  ...
}

Use of this with a Constructor

class OuterClass {
  ...
  class NestedClass {
    ...
  }
}

Nested Class

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Call in a Nested Class
Special commants 

JOptionPane.showMessageDialog(frame, text, "Warning",JOptionPane.WARNING_MESSAGE);

Message Popup

@Author(
    name = "Benjamin Franklin",
    date = "3/27/2003"
  )

Author of the code

@SuppressWarnings(value = "unchecked")
  void myMethod() { }

Suppress compilation warnings

@Override
  int overriddenMethod() { }

Override existing methods

@Deprecated
  static void deprecatedMethod() { }

Warning at compliation if old function is used
Signed AppletOracle, Tutorial, Chapter 10

javac {java-program}.java


Compile the Applet

jar cvf {java-applet}.jar {java-program}.class


Make a JAR File

keytool -genkey -alias signFiles -keystore {local_store}
     -keypass {key password} -dname "cn={name}"
     -storepass {store password}


Generate Keys

jarsigner -keystore {local_store} -storepass {store password}
     -keypass {key password}
     -signedjar
     S{java-applet}.jar {java-applet}.jar signFiles


Sign the JAR File

keytool -export -keystore {local_store} -storepass {store password}
     -alias signFiles -file {company_certificate}.cer


Export the Public Key Certificate

keytool -import -alias company -file {company_certificate}.cer
     -keystore {customer_store} -storepass {customer_password}


Import Certificate as a Trusted Certificate

Eclipse IDE Tutorial
Last Updated ( Tuesday, 21 December 2010 11:51 )
 
Copyright © 2012 gPhysics. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.