Pages

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sep 15, 2015

Install Maven on OSX causes Error

If you get error "Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0" when installing Maven on mac, try the following command (make sure you modify the version of jdk on your computer):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home 

Jul 7, 2015

Static and Dynamic Binding in Java

Static Binding or Early Binding
The binding which can be resolved at compile time by compiler is known as static or early binding. All the static, private and final methods have always been bonded at compile-time . Why binding of Static, final and private methods is always a static binding? You would understand it better after reading dynamic binding. Still let me explain this – Compiler knows that all such methods cannot be overridden and will always be accessed by object of local class. Hence compiler doesn’t have any difficulty to determine object of class (local class for sure). That’s the reason binding for such methods is static.
Dynamic Binding or Late Binding
When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding.

Reference:
http://beginnersbook.com/2013/04/java-static-dynamic-binding/

The "finally" block in Java

The "finally" block will get executed if we insert a return statement inside the try block of a try-catch-finally. Even when we attempt to exit within the try block (via a return statement, a continue statement, a break statement or any exception), the finally block will still be executed.

Note that there are some cases in which the finally block will not get executed, such as the following:

  • If the virtual machine exits during try/catch block execution.
  • If the thread which is executing the try/catch block gets killed.

Jul 5, 2015

"final" in Java

"final" doesn't guarantee immutable. It only protect the reference, not the object it references to.

ex:
private final int[] x; //x cannot change, but x[0] can change.

Access control in Java inheritance and package













  • the variable or method in a interface is actually public even if we left no modifier before it.

Jun 20, 2015

3 ways to prevent a class from being subclassed in Java

  1. Access control: Even though a class can't be marked private, a class can be non-public (by not declaring it as public). A non-public class can be subclassed only by classes in the same package as the class.
  2. final keyword: A final class means that it's the end of the inheritance line.
  3. If a class has only private constructors. Ex. like built-in Math class in Java.

MIN_VALUE in Java

It is no difference between declaring a MIN_VALUE with a "-" notation or without a "-" notation in Java. Both of the variables will be -2147483648.

Jun 17, 2015

Default initial value of variables in Java

For instance variables, Java has default initial value for those variables. But, the wrapper class of primitive variable, like Integer, Float..., will not have default initial value even they are declared as instance variables.

For variable within a method, Java has NO initial value for those variables.

Jun 15, 2015

Java Visualizer

"Java Visualizer" is a very helpful tool that can visualize the relationships between references and objects. See the following example.


















Link: http://cscircles.cemc.uwaterloo.ca/java_visualize/

Jun 10, 2015

[CS61B-2015] Notes for class 5-Binary and Bits



8 primitive types of Java:

  • The 5 integer types
    • Exact representations of integers.
    • byte, short, int, long, char.
  • The boolean type
  • The 2 floating point types
    • Approximate representation of reals.
    • float, double.
Integer:











bit operations:

Jun 8, 2015

[CS61B-2015] Notes for class 4-Testing



https://berkeley-cs61b.github.io/public_html/materials/lab/lab2/lab2.html

import org.junit.Test;
import static org.junit.Assert;
assertEquals(expected, actual);

To make testing output more concise
1. Annotate each test with @org.junit.Test
2. Make methods non-static
3. jh61b.junit.textui.runClasses

example:
https://github.com/Berkeley-CS61B/lectureCode/blob/master/lec4/live1/TestSort.java


Jun 7, 2015

Using the "final" keyword in Java

Using the "final" keyword in Java has different meaning depending on what it applied to:

  • Variable: the value cannot be changed once initialized.
  • Method: the method cannot be overridden by a subclass.
  • Class: the class cannot be subclassed.

Notes for "Head first Java"


  • A boolean and an integer are not compatible types in Java
    • int x = 1;
    • While(x)...
  • Objects are stored in heap (Garbage-collectible heap).
  • If you're running low on memory, the Garbage Collector will run, throw out the unreachable objects (no reference points to them).
  • There is no "global variable" in Java. Use public static instead. Any code, in any class of your application, can access a public static method/variable.
  • If the end-user doesn't have a JVM, then you'll also need to include that with your application's classes.

[CS61B-2015] Notes for class 3




A variable name in Java is a name for a simple container.
The 9 things that can go in simple containers:

  • primitive values(byte, short, int, long, float, double, boolean, char
  • references to Objects (an arrow), references may be null

Java is "Pass by value" (in some case, the "value" is a reference?)


Jun 5, 2015

[CS61B-2015] Notes for class 2-Defining and using classes



Static vs. Non-static
A class may have a mix of static and non-static members.

  • A variable or method defined in a class is also called a member of that class.
  • Static members are accessed using class name, e.g. ClassName.StaticMember.
  • Non-static members cannot be invoked using class name: Dog.makeNoise().
  • Static methods must access instance variables via a specific instance, e.g. an object.
  • Static members are shared in a class.

Jun 4, 2015

[CS61B] Notes for class 5-Linked lists




Array-based lists
Advantage: very fast access of each item
2 disadvantages:
    (1) Insert item at beginning or middle takes time proportional to length of array.
    (2) Array has a fixed length.                        

Linked lists (a recursively data type)
Made up of nodes. Each node has:
(1) an item
(2) a reference to next node in list

Linked lists vs. Array lists
Advantages of linked lists:
(1) Inserting item into middle of linked lists takes constant time, if you have a ref to previous node.
(2) List can keep growing until memory runs out.

Disadvantage of linked lists: Finding n-th item of a linked list takes time proportional to n. (Start at head, walk n-1 nodes.)

The "public" and "private" keywords
Private method or field: invisible and inaccessible to other classes.
Why?
(1) To prevent data form being corrupted by other classes.
(2) You can improve the implementation without causing other classes to fail.


The interface of a class: prototypes for public methods, plus description of their behaviours.

Abstract Data Type (ADT): A class with well-defined interface, but implementation details are hidden from other classes.

Invariant: A fact about a data structure that is always true. "A Date object always stores a valid date." Enforced by allowing access only through method calls.
If we don't have any restriction of a field, than we could declare this filed to "public"; Otherwise, we should declare it as "private."

Not all classes are ADTs! Some classes are data storage units, no invariants; field can be public.

Double-Linked Lists
Insert and delete items at both ends in constant running time.

Sentinel: A special node that does not represent an item. It points to the first and last nodes.













[CS61B] Notes for class 6-Stack frames




The heap stores all objects, including all arrays and all class variables.

The stack stores all local variables, including parameters.

When a method is called, Java creates a stack frame (aka activation record); stores the parameters and local variables.

Stack of tack frame.


Garbage collection only performs on heap.

When a method finishes, its stack frame is erased.

Method "Thread.dumpStack()": Prints a stack trace of the current thread to the standard error stream. This method is used only for debugging.

Parameter Passing
Java passes all parameters by value: copied.
When parameter is a reference, the reference is copied, but the object is shared.