• Save
Java essence part 1
Upcoming SlideShare
Loading in...5
×
 

Java essence part 1

on

  • 389 views

Simply description some part of essence of Java

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

Statistics

Views

Total Views
389
Views on SlideShare
376
Embed Views
13

Actions

Likes
0
Downloads
0
Comments
0

2 Embeds 13

http://theexamtime.com 10
http://www.theexamtime.com 3

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Java essence part 1 Java essence part 1 Presentation Transcript

  • 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 str1 = "Java"; String str2 = new String(str1); System.out.println(str1 == str2); // false  Str1 is not the same object of str2 View slide
  • 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 View slide
  • 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
  • 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
  • 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);} } }
  • 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
  • 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
  • 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); ... } }
  • 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
  • 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; } }
  • 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[];
  • 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 }); } }
  • 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; } } }
  • 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"); } }; }
  • 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"); } }; ... } ... }
  • Java Decompiler  http://java.decompiler.free.fr/  JD-GUI – http://java.decompiler.free.fr/?q=jdgui  JD-Eclipse  JD-Core