Java: Difference between revisions

From miki
Jump to navigation Jump to search
Line 126: Line 126:


== Debug ==
== Debug ==
;References
* http://blog.zvikico.com/2007/11/five-ways-for-t.html

Logging:
* http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/
* http://commons.apache.org/logging/
* http://logging.apache.org/log4j/1.2/index.html

Dynamic Proxy:
* http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html


;Java Trace
;Java Trace
:cfr [http://download.oracle.com/javase/1.5.0/docs/guide/jpda/trace.html]
* cfr [http://download.oracle.com/javase/1.5.0/docs/guide/jpda/trace.html]

;Profilers
* http://www.eclipse.org/tptp/

;Aspect-Oriented Programming:
* http://en.wikipedia.org/wiki/Aspect-oriented_programming


== References ==
== References ==

Revision as of 15:34, 12 October 2011

Other pages

Links

java.exe Command-Line

-Djava.library.path=<dllpath>
Set path to find DLL Libraries
-jar <jarfile>
Set jar file to load (should contain main())
java -Djava.library.path=.\var -jar "$(MYROOT)\var\main.jar"

Sample Program

  • Create a file HelloWorld.java, with content:
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
    for (int i = 0; i < args.length; i++)
      System.out.print(0 == i ? args[i] : " " + args[i]);
    System.out.println();
  }
}
  • Compile (javac must be in the path):
javac HelloWorld.java
  • Run:
java HelloWorld

Syntax 1.4

Arrays

  • Index is 0-based
  • Array base type can be any primitive type or reference types.
  • new allocates room for primitive types, or for references (not for object instance themselves).
  • After creation, arrays cannot grow or shrink. For dynamic arrays, consider using ArrayList class.
// Declaration
int [] arrayOfInts;                                // preferred declaration style
int arrayOfInts [];                                // ... c-style

// Creation and Initialization
int arrayOfInts [] = new int [42];                 // declaration and creation (set to 0)
arrayOfInts[0] = 69;

String [] someStrings = new String [ 3 ];          // ... idem (set to null)
String [] someStrings = { null, null, null };      // ... same as above
String [] someStrings = { "hello", new String(), someStuff.toString(), null };

// Anonymous arrays
setPets ( new Animal [] { new Dog("gray"), new Cat("grey"), new Cat("orange") });

// Get array length
int aLen = arrayOfInts.length

Syntax 1.5

For loops

Foreach loops (available since 1.5) [1]:

  • More readable
  • Series of values
  • Arrays and Collections
  • Applies to Iterable<E>.

But

  • Only access.
  • Only single structure.
  • Only single element.
  • Only forward.
  • At least Java 5.
FOR EACH loop Equivalent FOR loop
for (type var : arr) {
    body-of-loop
}
for (int i = 0; i < arr.length; i++) { 
    type var = arr[i];
    body-of-loop
}
for (type var : coll) {
    body-of-loop
}
for (Iterator<type> iter = coll.iterator(); iter.hasNext(); ) {
    type var = iter.next();
    body-of-loop
}

Miscellaneous

Class Literal
A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a . and the token class. The type of a class literal is Class. It evaluates to the Class object for the named type (or for void) as defined by the defining class loader of the class of the current instance.
Eg:
public class MyClass //...
//...
printf ("Class name is %s\n",MyClass.class.getName());
Throwable.getStackTrace()
Could be used to get the name of current class / methods for debug...

Debug

References

Logging:

Dynamic Proxy:

Java Trace
Profilers
Aspect-Oriented Programming

References

String Integer.toHexString(byte);                    // Convert an integer to string in hexadecimal

"00" + Integer.toHexString( i )                      // Pad with leading zeros (then take rightmost characters)

String.format("%04x",0x2a);

//Prepend 0 if needed
String hex = Integer.toHexString(abyte & 0xff);
StringBuffer hexbuf = new StringBuffer();
if (hex.length() == 1) hexbuf.append('0');
hexbuf.append(hex);

//Prepend 0 if needed
String hex=Integer.toHexString(aInt);                  // Assume 0<= aInt <= 255 (i.e. positive!)
System.out.print((hex.length()>1? "" : "0") + hex + ", ");