## sg19 java QQ to brush up

  • — jxee
  • [10=10 min ]
  • [20] Containers
  • Cloud native? LG
  • JPA? LG
  • — other ecosystem items
  • containers, cgroups
  • jdk tools for heap analysis
  • tuning best practices
  • GC in general? bookish ok
  • Ajax integration? no one asked
  • — coreJava
  • java8/9 fundamentals?
  • high-level concurrency tools — how they are related to the low-level. Lower mkt value than low-level tools
  • serialization? not popular in IV
  • advanced generics like wildcard? not popular in IV
  • reflection? not popular in IV

float/int 1:1 mapping#java support

See last paragraph of https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html

you can use an AtomicInteger to hold byte values, and cast appropriately. You can also hold doubles using Double.doubleToRawLongBits(double) and Double.longBitsToDouble(long) conversions… because there’s a valuable 1:1 mapping between a 64-bit number and a 64-bit float number.

  • Useful if you need to precisely compare a 64-bit float value. For example, you can check if a value has changed at all. The value could be user-supplied or shared mutable.
  • Useful if you need to store float values in a hashtable, though I’m not sure about performance.
  • Useful if you need radix sort on floats

EnumSet^regular enum

[category javaOrphan]
A java enum type usually represents .. (hold your breath) .. a radio-button-group. A variable of this type will bind to exactly one of the declared enum constants.

eg: Continent — there are only 7 declared constants. A Continent variable binds to Africa or Antarctic but not both.
eg: SolarPlanet — there are only 8 declared constants
eg: ChemicalElement — there are only 118 declared constants
eg: ChinaProvince — there are only 23 declared constants

In contrast, enum type has a very different meaning if used within an EnumSet. (I will name a meaning soon). Each enum constant is an independent boolean flag. You can mix and match these flags.

Eg: Given enum BaseColor { Red,Yellow,Blue} we can have only 2^3 = 8 distinct combinations. R+Y gives orange color. R+Y+B gives white color.

Therefore, the BaseColor enum represents the 3 dimensions of color composition.

EnumSet was created to replace bit vector. If your bit vector has a meaning (rare!) then the underlying enum type would have a meaning. Here’s an example from [[effJava]]

Eg: enum Style {Bold, Underline, Italic, Blink, StrikeThrough, Superscript, Subscript… } This enum represents the 7 dimensions of text styling.

[[effJava]] reveals the telltale sign — if the enum type has up to 64 declared constants (only three in BaseColor.java), then entire EnumSet is actually represented as a single 64-bit integer. This proves that our three enum constants are three boolean flags.

java enum: elegant

“Elegant” (and “clean”) is probably the best adjective. My comments below are mostly based on [[effJava]]

  • 🙂 immutable in the face of serialization
  • 🙂 strict singleton in the face of serialization .. P311
  • 🙂 simple enough to be easily embedded as a static nested class
  • 🙂 you can add behavior (and data) unique to Jupiter, using a constant-specific class body
  • 🙂 You can switch on enum values
  • compiler adds two implicit static methods values() and valueOf(), not listed on the official javadoc 😦
  • values() returns a fixed array of the predefined enum values
  • valueOf(planetNameStr) would return the matching enum instance
    • Note this method is unrelated to String.valueOf()
    • you can even add your own fromString(abbrevatedPlanetName) method. see [[effJava]]

EnumSet (see separate blogpost) and EnumMap built on the strength of enum feature

##java8 features #MethodRef

  • lambda
  • default methods
  • streaming
  • method references? Supposed to be an important feature in the core language/syntax. related to lambda syntax, presumably a syntactic sugar coating.
  • java.util.Optional<T> introduced to reduce NPE. Compare boost::optional
  • CompletableFuture<T> to promote async, event-driven programming
  • PermGen disappears in java8
  • –not really java8 features:
  • java7 introduced G1 garbage collector though default GC remains ParallelGC, for both java8 and java7

Cloneable, Object.clone(), Pen.Clone() #java

A few learning points.

The Object.clone() implementation is not that important, because I should always override it in my class like Pen, but here are some observations about this Object.clone():

  • shallow copy, not deep copy, not very useful.
  • this protected method is mostly meant to be invoked by a subclass:
    • If your variable points to some Pen object that’s no Clonable, and you call it from either the same package or a subclass, then you hit CloneNoSupported.
    • if your Pen class implements Clonable, then it should override the clone() method
    • [1] The only way the default Object.clone() gets picked by compiler is when a Cat class implements Clonable but doesn’t override clone(), and you call it from either the same package or a subclass

Clonable is a special marker interface. It could trigger the CloneNotSupported exception, but if you override clone() then this exception may not hit. It’s an obscure detail.

  • I think you can override clone() without implementing Clonable, but this is tricky and non-standard.
  • You could also implement Clonable without overriding clone() .. see [1]

java: protected^package-private

https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html (java8) shows two nice tables:

  • There’s no more “private protected
  • default access level is better known as “package-private” — strictly more private more restrictive than Protected . (Protected is more like Public). The 2nd table shows that
    • a “package-private” member of Alpha is accessible by Beta (same package) only, whereas
    • a “protected” member of Alpha is accessible by Beta and Alphasub
    • Therefore, “neighbors are more trusted than children”

I find it hard to remember so here are some sound bytes

  1. “protected” keyword only increases visibility never decreases it.
  2. So a protected field is more accessible than default (package-private)
    • As an example, without “protected” label on my field1, my subclasses outside the package cannot see field1.
  3. same-package neighbors are local and trusted more than children outside the package, possibly scattered over external jars

For a “protected” field1, a non-subclass in the same package can see it just as it can see a default-accessible field2

Not mentioned in the article, but when we say “class Beta can access a member x of Alpha”, it means that the compiler allows you to write, inside Beta methods, code that mentions x. It could be myAlpha.x or it could be Alpha.x for a static member.

string pool IV notes

String str = new String("Cat"); //[1] inefficiency

Based on https://www.quora.com/String-s-new-string-ABC-How-many-objects-are-created-after-the-above-statement .. Net effect of above statement — either 1 or 2 instantiations

> At class-loading time, JVM picks up all string literals in the class file and populates the string pool. IIF this “Cat” is the first occurrence then it would be instantiated in the string pool.

Net effect so far is 0 or 1 instantiation.

> At runtime, the new() forces JVM to instantiate a brand new object, in the regular heap, unrelated to the string pool.

Net effect now is 1 or 2 instantiations.

[1] Note this is (almost) never needed according to P20 [[effJava]]

more MCQ questions on coreJava skills #GregR

You can find sample MCQ questions in Brainbench, Select2perform and IKM. Let me know if you want me to help you review and select some good questions — not obscure, not ambiguous…

Below are my own inventions, including those I sent before. (So you can discard the last mail.)

For candidates — Please pick the most likely answer for the most typical case, even if the question is not 100% clear. You can ignore corner cases.

  • Q1: which command produces java bytecode [correct answer is marked with *]
    • A) java (lower case)
    • B) * javac (lower case)
    • C) jar (lower case)
    • D) jvm (lower case)
    • E) None of the above
  • Q1b: which command executes the bytecode
  • Q1c: which command is the java debugger
  • Q1d: which command could launch the JVM
    Q1e: which command can read but not execute java bytecode
  • Q2: which among these is a modifier on a class
    • A) finally
    • B) finalize
    • C) * final
  • Q2b: which is actually the name of a well-known method? [B]
  • Q2c: which is a keyword only used inside methods? [A]
  • Q3: which keywords are legal for a method? (multiple correct answers)
    • A) * synchronized
    • B) virtual
    • C) * final
    • D) mutable
    • E) * abstract
    • F) const
    • G) override
    • H) deprecated
    • I) volatile
    • J) atomic
  • Q3b: which keyword is related to concurrency (all lower case, multiple correct answers) [ A/I ]
  • Q3c: which ones are not java language keywords [Advanced question. B/D/F/G/H/J]
  • Q4: do I need at least one “import” to use basic java threads? [Answer is no]
  • Q4b: how about java collections?
  • Q4c: how about java generics, assuming I avoid generic collections
  • Q5: if your java app creates no thread, will there be any other thread beside the main thread, in a typical java app? [Answer is yes]
  • Q11: if I have a typical java application’s bytecode compiled under 32-bit, can I use it in a 64-bit JVM?
    • A) * Usually yes, sometimes with minor changes.
    • B) Usually no, as it requires recompilation under 64-bit
  • Q11b: how much more memory can the 64-bit java use compared to 32-bit, in theory?
    • A) About double
    • B) * Much, much more
    • C) About the same.
  • Q11c: how much faster will the 64-bit jvm run that same bytecode, compared to the 32-bit jvm, assuming it needs minimal memory? [Advanced]
    • A) about double or higher
    • B) much, much faster
    • C) * About the same
  • Q12: which download has more content — jdk or jre? [ answer is jdk]
  • Q13: does garbage collection slow down a typical java server on a single-processor machine?
    • A) yes. Clearly Noticeable and very significant
    • B) * slightly
    • C) Should have no impact
  • Q13b: what if the app uses very little memory? [Answer is C]
  • Q13c: what if the app creates a huge amount of short-lived objects? [Advanced. Answer is B]
  • Q13d: what if the app creates a fair but not excessive amount of long-lived objects? [Advanced. Answer is B]
  • Q14: have you used any java debuggers? If yes, can you see the content of protected fields?
  • Q14b: can you see the content of protected static fields?
  • Q14c: can you modify its value using the debugger? [Advanced question. answer is yes]

RMI could send more than simple VO

[[java the good parts]], written by a RMI authority, is a thin book with a concise but in-depth chapter on RMI. One of the things pointed out was —

In a distributed system like RPC or RMI, sometimes the object sent over network are more than monolithic POJO “value objects” with trivial methods. For example, you could send a special FXDeal object having specialized virtual reval() method. Interface would declare a BaseDeal data type for it. Obviously, receiving end should NOT “slice” this FXDeal object down to BaseDeal and lose the specialized reval().

Not sure how CORBA handles it, but RMI was designed to support it. Basically the serialization stream of the object includes the location to download the FXDeal class bytecode.

## java 7 features – my picks

(Java 8 and java 5 are major changes compared to java 7)

#1) [advanced] fork and join
#2) [advanced] File change notifications
#3) new filesystem io API
#4) try( FileOutputStream fos = newFileOutputStream(“movies.txt”); DataOutputStream dos = newDataOutputStream(fos) ) {
dos.writeUTF(“Java 7 Block Buster”);
} catch(IOException e) {
// log the exception
}

# catch(ExceptionOne | ExceptionTwo | ExceptionThree e) {
# diamond operator. List> my = new List<> ();

[[practical api design]] java

Written by the founder and initial architect of NetBeans, this book has a java root. Freely available in pdf, I find this ebook rather detailed, not a lot of high-level (vague) concepts like most design/architecture books including the classics. It seems to deal with some real world coding and design decisions. I would say these are not life-and-death decisions but still everyday decisions. (In contrast those other books address decisions I don’t encounter or care at all — seem to belong to another level.) Therefore this book is closer to my life as a developer.

There’s a chapter against “extreme” (unhealthy) advice. Unconventional critique:
** an api must be beautiful
** an api must be correct
** an api must be simple
** an api must have good performance
** an api must be 100% compatible
** an api must be symmetrical

chapter on creating a threadsafe API, to be used by clueless and untrained programmers.

chapter on supervising the development of an API

Section on overcoming the fear of committing to a stable API

Section on Factory being better than constructor

Section on making everything final

fail fast:fundamental+Practical principle #java

Fail-fast is one of those low-level coding habits that deserve its place among the valuable habits to be adopted by practicing app developers in industry. In contrast, library developers (including open-source authors) generally adopt fail-fast by default.

Principle — prefer crashing the entire program. Don’t keep going. Don’t hope the dubious condition will be tolerated. Don’t hope the corrupted data will be left alone and left untouched. Sooner or later what you fear will happen. In such a case it can be very hard to find the root cause. The crash site might be far away from the buggy code.

Example — when dealing with DAM issues, there are specific tools to make the program crash as soon as detected. I think they intercept, replace or integrate with malloc/free.

Example — if null pointer can cause problem then check as early as possible. In java and c#, a lot of error messages simply say null pointer (like “unknown exception”) . It can take hours to find out the real cause.

Example — fail-fast iterators.

java singleton – y nested class implementation important

http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom is the recommended solution. My friend Rohan pointed out why this solution is sometimes necessary.

Say you can’t eagerly init the static field because it must wait for some service object.

Say you also don’t want to incur the static synchronization every time. Though un-contended lock acquisition is cheap, it’s never free.

Spring self-quiz 2

Q1: spring bean scopes? prototype/singleton/request/session…
A1: http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes

Q3(common): why use spring?
%A3: JMS, MVC, tx, Spring security, AOP,  hibernate,

Q4: how do u use spring tx management
A4: xml or annotation

Q: what's so special about DI, if we can easily use method/constructor injection?

Q: auto wiring and @Inject …?:

Q: singleton bean?

SmartLifecycle used in neo

Neo classes include a lot of start() and stop(). These are inherited from SmartLifecycle.java.

Compared to super-interface Lifecycle.java, SmartLifecycle interface provides more sophisticated integration with the container's startup and shutdown phases.

I think Neo uses this to implement a lot of “services”. IOC Container must be alive throughout.

biggest changes af JDK 1.4

Xia Rong,

(Another blog.)

We once discussed the common interview question “what changes do you consider significant in 1.5”. Now i feel the concurrency changes are significant, perhaps second only to generics. Generics are a fundamental change affecting many parts of the core language.

On to concurrency. Reading a few threading books, a pattern seems to be there — since the late 90's, multi-threading developer had been “rolling their own” concurrency tools  because the JDK threading tools are too basic. To my surprise, a lot of these home-made components are very similar

* thread pool
* dispatcher thread
* worker threads
* task queue
* blocking queue
* semaphore….
* condition variables

After 1.5, every java trading system i know uses thread pools and concurrent collections. I don't know anyone still using wait/notify — too low-level.

Personally, i would point out the importance of Lock interface and atomic variables (esp the Compare-And-Set ie CAS functionality). CAS relies on *native* support just as wait/notify. Some people claim that the new concurrent collections derive huge performance gains partly due to atomic variables.

Why are threading improvements so important? I feel the biggest reason is the multiprocessor machines. Chip makers spent decades increasing clock speed but they reached a plateau. The way to add more power in a machine is going multi-core. Multiprocessor machines are hard to fully utilize until applications are multi-threaded.

constructors called from strategic places (super, subclasses etc

* You can call a constructor C() from an instance method in C.java. This means an existing C instance can create a “friend” C instance. Often parent-child nodes

* Call C() from its base class B?

* Call C() from its derived class D? Actually every D constructor has to call a C constructor.

* If C object has a pointer to a J object, does it make sense to make J.java can call C() constructor? Yes but we can easily add into J object a pointer back to the original C object, so the link becomes bi-directional and tight-coupling.

These unusual ways to call constructor are the basis of many OO designs.

java serialization – a few tips

See link — covers serialization in RMI.

* serialization = writing a *graph* of objects into a stream of data

* How to Make a Class Serializable
1. Implement the Serializable interface.
2. Make sure that instance-level, locally defined state is serialized properly.
3. Make sure that superclass state is serialized properly.
4. Override equals( ) and hashCode( ).

Making a class serializable rarely involves significant changes to its functionality and shouldn’t result in any changes to method implementations. This means that it’s fairly easy to retrofit serialization onto an existing object hierarchy. The hardest part is usually implementing equals( ) and hashCode( ).

on (2) above, i personally try to compose my objects with primitives, strings and simple arrays. What if there’s a map field? I guess i have to customize its serialization.

unchecked exceptions

based on [[ hardcore java ]] and http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html

Runtime exceptions represent problems that are the result of a *programming* problem. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.

For example, a method can check if one of its arguments is incorrectly null. If an argument is null, the method might throw a NullPointerException.

Here’s the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

USP@ejb @@

— USP ] %% xp
#1 CMR/CMP to automatically load/store related database tablesssss just like hibernate/JDO. THE real value of ejb in the eyes of Union Bank of Switzerland.

[[ ejb ]] book obviously treat CMP as the heart of ejb technology

#2 biz-tier cluster, behind the web tier cluster
THE real reason for oceanlake

— USP ] theory
#1 XA. according to both [[ ejb ]] and Rod Johnson — XA ie (declarative?) dist tx. Other solutions support tx or remoting(J2SE RMI) but not both.

#2 distributed objects?
corba
dcom
dotnet remoting?

#x MDB is the finest breed of EJB (Rod Johnson?) [[ ejb ]] author also singled out MDB as a top 5 j4EJB. Unique? not sure

#x ejb security? declarative? ejb fits like a glove if your biz logic are well-represented by security roles
[[ ejb ]] frequently mentions “transactional and security features” in one breath

====Well, almost Unique. U can call them “valuable features@ejb”

simple(@@) topics ] java

singleton,
factory,
dao,
db connection obj
db connection set-up
thread-safe collections
jdbc conn pool
stack^heap
vector^arraylist

–jsp:
forward/redirect
4 scopes

#

simple? Is shell scripting simple?

These tend to be interview questions, because
* most of us are expected to have used them or know them well
* => yardstick to measure and compare candidates
* familiar to most interviewers

jruby — briefly

jruby implements most of the ruby features.
Support for interacting with and defining java classes from within ruby.

Q: j4 jruby
A: using Ruby for tools and tasks where Java only incurs overhead is a
very compelling for Java developers

Q: How do I use it if i already know how to program and run a ruby program?
A: you should be able to just use your Ruby scripts with JRuby

rule@try-catch-finally execution order { catch block

— In the absence of finally —
[ try3 = try block in unfinished method #3 on call stack ]

When catch3a (among catch3b, 3c) is triggered by a *propagated* exception pe,

* we know (since it’s propagated) that try3 must have called a try4 which threw pe, and
* we know that upon catch3a completion, the statements following catch3a run, not those following catch4

What if there’s a finally? Finally can easily derail this rule. Let’s hope that finally doesn’t return, exit, break/continue or throw.

rule@try-catch-finally execution order: cover-up

throw
-> catch?
-> finally
-> propagate
-> catch?
-> finally
-> propagate

Upon throw, jvm tests the innermost catches. Before propagating, the innermost finally runs. See P475 [[ thinking in java ]].

Propagating = searching for a catch at the next level.

In fact, finally has the capability to suppress the uncaught and to-be-propagated exception by throwing a cover-up exception. See P477 [[ thinking in java ]]. Similar to a county communist official covering up a murder case scheduled for escalation. This is the only guy we know with the power to cover up a murder case.

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html shows a clear example “suppressed exception”.

Q: if a finally says “return”, will the method return normally, leaving the uncaught exception covered up?
%%A: I think so. see earlier blogs.
%%A: I think P 56/P65 [[java precisely]] says no the uncaught exception would propagate out

rule#55@try-catch-finally execution order–suspension

“exit-type statements (exit, return, break or continue…. ) in a catch{} are /suspended/ until finally runs its course.”

think of catch{} as a naughty kid. jvm will let catch{} run its course right before catch{} attempts to exit the try-catch-finally. At that point, finally{} takes over. As a good care-taker, finally{} usually [1] keeps the “exit-plan” of catch{}.

[1] see other posts on this execution order.

Q: How about a throw instead of an exit-type?
A: “If a try block throws an exception and the catch block propagates the exception (throws it again), the finally clause will still execute. If the finally clause executes a return statement, it overides (covers up) a thrown exception so the exception will not be thrown; instead the return will occur.”

A: look at the java precisely example 90

Q: what if finally /attempts to/ override that exit-type statement?
A: it will prevail.

A: look at the java precisely example 90

in java, "inaccessible" means

q: a private (static/non-static) member priv2 in class P is “inaccessible” in a client class C. Exactly what does it mean?

A(short): “class C can’t mention priv2 “

A: “a statement in class C will trigger a compile-time error if it mentions priv2 “

I think for each method call the compiler identifies
* the target-object (“target-class” for static) [1]
* the source-object of the caller method — upstream on the call stack

[1] but the target object is determined at run time by dynamic binding (– polymorphism) even if you prefix a variable (“remote-control”) in front of the method name. OK you are right about
* compile-time access checking
* run-time binding.

Here’s another case for IS-A and method-overriding rules. The long explanation is complicated. Let’s try a short explanation. What the compiler says actually holds at runtime, because the overriding version (that actually runs [2]) is as accessible as the overriden version.

[2] that binds to the method name at run time. “runtime binding”

IV: what are java "class objects"

One of the wall street interviews asked “what are class objects” and I said objects are instances of a class, so class object is imprecise phrase.

Now I think “class object” refer that special object instantiated when a class is loaded by a java classloader. 2 classloaders can instantiate 2 such objects for the same myClass.java? Yes.

One possible reason for this iview question might be mem-leak. See posts on [[ root objects ]].

java exception q&&a – nontrivial

A minimum custom exception : “class e1 extends Exception {}”
Q: Where’s the constructor? where’s is the e1(String) constructor?
a: a constructor is needed for the exception obj’s “new” operation

q3: when an exception is thrown, control passes to…?

a1: wrap the exception-prone statements in a try-catch

A: constructor is the invisible e1(){super();} There’s no e1(String)
constructor in this case, since u didn’t define it.

q: is there a constructor for each exception obj?
A: i think so, including the no-arg

a4: on the heap

q1: how do u keep your method “alive” if u know an exception can be thrown
prior to a normal return?

a2: inside the exception obj and the exception type (ie the full class
name), but often there’s nothing inside the obj

q4: is exception obj created with new()? on the heap?

A3: exception handler for this particular (type of) exception

q2: there is specific information about each exception. who needs this
info? where is this info stored?