Skip to content

general

Keyhan Hadjari edited this page Oct 2, 2016 · 13 revisions
  • A local variabel has to be set, unlike class or instance variables they do not get default values. The following code would not compile
public void foo() {
String bar;
System.out.println(bar) // The code breaks here.
}

String Pool

Basically, a string intern pool allows a runtime to save memory by preserving immutable strings in a pool so that areas of the application can reuse instances of common strings instead of creating multiple instances of it. Since String is immutable this address is never overwritten until garbage collected.

When we use double quotes to create a String, it first looks for String with same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference.

However using new operator, we force String class to create a new String object in heap space. We can use intern() method to put it into the pool or refer to other String object from string pool having same value.

In the example below s is equal to u since the double quoting is used, s and u are however are not equal.

String s = "a" + "bc";
String t = "ab" + "c";
System.out.println(s == t); // Returns true
String u = new Stringbuilder().append("ab").append("c").toString();
System.out.println(s == u); // Returns false

Look at here for more information.

Classes

  • Class does not need to be implemented as public, it could be package default and accessed only in the package.

Access Modifiers

  • public: can be access from everywhere.
  • private: can be accessed only within the class.
  • protected: can be accessed only within the class, its sub-classes and classes in the same package .
  • package private: No modifiers is written, can be accessed only by its class and other classes in same package.

protected modifier

The protected modifier allows the methods and fields of the class to be available to its subclass, but only be accessed by referencing subclass.

package a;
public class AB {
protected void method foo() {}
}

package b;
public class ABBA extends AB {

public void bar() {
new AB().foo(); // NOK
new ABBA().foo(); // is OK
new BAC().foo(); // Poor design but code is OK
}
}

package b;
public class BAC extends ABBA{}

Declaring and initialaizing variables

You could declare multiple variables at once like

String name, address, phone; //OK
int age, id=0; //declared and id initialized

Importing Packages

Importing package can be either all the way to the class name or pointing to the directory where the class is and then an asterisk *.

package com.farm
pulic class Sheep {}
//Accesses Sheep by asterisk
import com.farm.* 
public class Farmer {
Sheep sheep;
}

or

//Accesses Sheep class directly
import com.farm.Sheep
public class Farmer {
Sheep sheep;
}
  • In your imports you cannot access 2 classes with the same name. But you could access one of them directly and access the other ones wildcard, this result in that all other classes in the same wildcard expected.

Clone this wiki locally