JAVA Basics

Questions about JAVA:

  1. static (shared by all objects, owned by class), combined with final (unchanged)
  2. 序列化, serializable:通用的语言 for communication and persistence
  3. string builder
  4. 正则表达式:regex, //s, //s+
  5. Integer/int, Character/char?
  6. singleton
  7. Iterator
  8. iterable
  9. static block
  10. this() in constructor
  11. gnu trove
  12. script language


  • 232 − 1 = 4294967295 = 4 GiB − 1
  • 32 bits will give you an addressable space of 2^32 items, each item being a full byte.

PRIMITIVE DATATYPES(stores in stack)

  • byte: 8 bit signed integer, -128(2^7)~127
  • short: 16 bit signed integer,  -2^15~2^15
  • int: 32 bit signed integer, -2^31~2^31
  • long: 64 bit signed integer, -2^63, 2^63
  • float: 32 bit single-precision floating point, 0.0f
  • double: 64 bit double-precision floating point, 0.0d

double i = 5.0;  i/2 = 2.0; (精度损失)i/2.0 = 2.5;

  • boolean: 1 bit
  • char: 16 bit Unicode character


String Class (object)

  • Strinig is a class, any quoted string is an object, so we cannot use “==” to compare to strings, because we are comparing addresses of the objects in this way. We need to use str1.equals(str2) to compare two strings
  • String objects are immutable, what about string builders?
  • Use “+” to append strings
  • s.length();
  • s.charAt(0);
  • s.toLowerCase();
  • s.trim();
  • s.toCharArray();
  • s.valueOf(); return the string value of the argument
  • s.contains(charSequence); if the argument is a character, use String.valueOf(arg) (423)
  • s.parseInt

Integer Class (object)

  • Integer.valueOf(“123”);
  • Integer.parseInt(“123”);

Character Class

  • Character.isLetterOrDigit(ch);

Copy value:

  • We cannot implement swap in java??


  • Inheritance
  • Encapsulation
  • Access level: private, public, …


  • Variable model: we can copy value for primitive types, which can be expressed in 0 and 1, with fixed size. The variable and the value are together.
  • Object model: do not have fixed size, the variable is a reference (8 bytes for 64-bits system, 4 bytes for 32-bits system), reference points to an object, which is stored in heap, the reference stores the address of the object in heap, the reference is stored in stack? Without initialization, the default value for a reference is null. Thus, in order to avoid NullPointerException, we need to initialize a reference with an object.
  • For example, Student student1 = new Student(); it creates an Student type object stored in heap, this object has no name. The reference student1 points to the object. To wrap up, it can be summarized as “Reference = Object“. To count how many objects are created by the program, the only thing we need to count is how many “new”s there are.
  • Reference array needs to be initialized independently.                                                             Student[] students = new Student[2]; When we get access to students[0], it points to a null object, we have to intialize it like this: students[0] = new Student();


  • heap
  • stack


  • No modifiers, no variables, only constant
  • A class implements an interface, and observe the rules. The reference of an interface can only use the methods defined in the interface, ignore other methods in the class selectively.
  • A class can implement more than one interface.


  • Similar to interfaces.
  • In java, a class can only inherit one class, which is different from interface.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s