Java Essence part1
- String, enum
Bram
Outline
 String
 enum
String
 String str = "Java";
 String str = new String("Java");
 String temp = "Java";
String str = new String(temp);
 ...
String
 String (Intern) Pool
 preserving immutable strings in pool
 one instance of the same string
 String str1 = "Ja...
String
 String.intern()
 If pool has this string, return it directly
otherwise, add to pool and return
 s.intern() == t...
String
 String.intern()
 Use only if needing to compare a lot of String
 Improve by == to replace equals()
 no way to ...
String
 String.substring()
 Instead of creating an new char array,
just „reuse‟ the old one
 Only the offset and count ...
enum
 Without Enum
 public interface Position {
public static final int TOP = 0;
public static final int RIGHT = 1;
publ...
enum
 After JDK5
 enum is available
 public enum Position {
TOP, RIGHT, LEFT, BOTTOM
}
 “enum” declare special class
...
enum
 After Decompile
 public final class Position extends Enum {
…….
private Position(String s, int i) {
super(s, i);
}...
enum
 java.lang.Enum
 public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
…..
private...
enum
 Extended enum
 public enum MethodSet {
INIT (“init”, -1),
START(“start”, 1),
STOP(“stop”, 0);
….
private string me...
enum
 After decompile
 public final class MethodSet extends Enum {
...
private MethodSet(String s, int i, string method,...
enum
 continue.
 static
{
INIT = new MethodSet("INIT", 0, "init", -1);
START = new MethodSet("START", 1, "start", 1);
ST...
enum
 More Extended enum, v1
 public enum Action implements Command {
STOP, RIGHT, LEFT;
public void execute() {
switch(...
enum
 More Extended enum, v2
 public enum Action implements Command {
STOP {
public void execute() {
System.out.println(...
enum
 After decompile
 public abstract class Action extends Enum implements Command {
...
static
{
STOP = new Action("ST...
Java Decompiler
 http://java.decompiler.free.fr/
 JD-GUI
– http://java.decompiler.free.fr/?q=jdgui
 JD-Eclipse
 JD-Core
Upcoming SlideShare
Loading in …5
×

Java essence part 1

615 views

Published on

Simply description some part of essence of Java
This one involves String & enum

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
615
On SlideShare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java essence part 1

  1. 1. Java Essence part1 - String, enum Bram
  2. 2. Outline  String  enum
  3. 3. String  String str = "Java";  String str = new String("Java");  String temp = "Java"; String str = new String(temp);  String str1 = "Java"; String str2 = new String(str1); System.out.println(str1 == str2); // false  Str1 is not the same object of str2
  4. 4. String  String (Intern) Pool  preserving immutable strings in pool  one instance of the same string  String str1 = "Java"; String str2 = "Java"; System.out.println(str1 == str2); // true  String str1 = "a" + "bc"; String str2 = "ab" + "c"; System.out.println(str1 == str2); // true
  5. 5. String  String.intern()  If pool has this string, return it directly otherwise, add to pool and return  s.intern() == t.intern() is true if and only if s.equals(t) is true  String str1 = "Java"; String str2 = new String(str1); System.out.println(str1 == str2.intern()); //true
  6. 6. String  String.intern()  Use only if needing to compare a lot of String  Improve by == to replace equals()  no way to de-intern  In general, not to call intern() on user-generated strings  In JDK 7  interned strings aren‟t allocated in the permanent generation, but in the main part of the Java heap
  7. 7. String  String.substring()  Instead of creating an new char array, just „reuse‟ the old one  Only the offset and count changes  public final class String{ private final char value[]; private final int offset; private final int count; public String substring(int beginIndex, int endIndex) { return new String (offset + beginIndex, endIndex - beginIndex, value);} } }
  8. 8. enum  Without Enum  public interface Position { public static final int TOP = 0; public static final int RIGHT = 1; public static final int LEFT = 2; public static final int BOTTOM = 4; }  Valuables in Interface  public : open for others to understand  static : there is no instances, so it must belong to Class  final : regards Interface as treaty – Add by compiler even not declare those key-words
  9. 9. enum  After JDK5  enum is available  public enum Position { TOP, RIGHT, LEFT, BOTTOM }  “enum” declare special class  Inherit from java.lang.Enum – CANNOT extent directly
  10. 10. enum  After Decompile  public final class Position extends Enum { ……. private Position(String s, int i) { super(s, i); } public static final Position TOP; public static final Position RIGHT; public static final Position LEFT; public static final Position BOTTOM; ... static { TOP = new Position("TOP", 0); RIGHT = new Position("RIGHT", 1); LEFT = new Position("LEFT", 2); UP = new Position("UP", 3); ... } }
  11. 11. enum  java.lang.Enum  public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable { ….. private final String name; private final int ordinal; protected Enum(String name, int ordinal) { this.name = name; this.ordinal = ordinal; } // ordinal is the enum order with beginning of 0 … }  http://grepcode.com/file/repository.grepcode.com/java/root/jdk/ openjdk/7-b147/java/lang/Enum.java
  12. 12. enum  Extended enum  public enum MethodSet { INIT (“init”, -1), START(“start”, 1), STOP(“stop”, 0); …. private string method; private int value; private MethodSet(string method, int value) { this.methid = method; this.value = value; } public string method() { return method; } public int value() { return value; } }
  13. 13. enum  After decompile  public final class MethodSet extends Enum { ... private MethodSet(String s, int i, string method, int value) { super(s, i); this.methid = method; this.value = value; } public int value() { return value; } ... public static final MethodSet INIT; public static final MethodSet START; public static final MethodSet STOP; private string method; private int value; private static final MethodSet $VALUES[];
  14. 14. enum  continue.  static { INIT = new MethodSet("INIT", 0, "init", -1); START = new MethodSet("START", 1, "start", 1); STOP = new MethodSet("STOP", 2, "stop", 0); $VALUES = (new MethodSet[] { INIT, START, STOP }); } }
  15. 15. enum  More Extended enum, v1  public enum Action implements Command { STOP, RIGHT, LEFT; public void execute() { switch(this) { case STOP: System.out.println(“Animation Stop"); break; case RIGHT: System.out.println(“Animation Right"); break; case LEFT: System.out.println(“Animation Left"); break; } } }
  16. 16. enum  More Extended enum, v2  public enum Action implements Command { STOP { public void execute() { System.out.println(“Animation Stop"); } }, RIGHT { public void execute() { System.out.println(“Animation Right"); } }, LEFT { public void execute() { System.out.println(“Animation Left"); } }; }
  17. 17. enum  After decompile  public abstract class Action extends Enum implements Command { ... static { STOP = new Action("STOP", 0) { public void execute() { System.out.println("u64AD…xxxxx"); } }; RIGHT = new Action("STOP", 1) { public void execute() { System.out.println("u64AD…xxxxx"); } }; ... } ... }
  18. 18. Java Decompiler  http://java.decompiler.free.fr/  JD-GUI – http://java.decompiler.free.fr/?q=jdgui  JD-Eclipse  JD-Core

×