Difference between revisions of "Misc"

From Suhrid.net Wiki
Jump to navigationJump to search
 
(13 intermediate revisions by the same user not shown)
Line 15: Line 15:
 
* String $;
 
* String $;
  
== Classes and Interfaces ==  
+
== Comments ==  
  
<u> Class Modifiers </u>
+
* Comment start sequences like //, /*. /** are ignored within comment blocks.
 
+
* So trying to nest multiple-line comments will result in an error
Regular Classes:
 
 
 
* Access Modifiers: public and default only
 
* Non access: strictfp, abstract and final only
 
* Adding any other modifier will result in a compiler error.
 
 
 
* A final class cannot be overriden (e.g. String)
 
* An abstract class cannot be instantiated (e.g. DateFormat)
 
 
 
Interfaces:
 
 
 
* All interface methods are implicitly '''public and abstract'''
 
* Even if some modifiers are left out, the above modifiers will be applied.
 
* Since they have to be overriden,''' methods cannot be static.'''
 
* Similarly they '''cannot be final, strictfp, or native.'''
 
 
 
* All variables are implicitly '''public, static and final'''
 
* Even if some modifiers are left out, the above modifiers will be applied.
 
* Since variables are final, they must be initialized
 
 
 
* If no access modifier is specified, the member is public, not package !
 
* Similarly, specifying protected/private is not allowed for methods and variables
 
* Watch out when classes implement interfaces and don't specify the method as public - this wont compile.
 
* Similarly, watch out when classes try to reassign the value of an interface.
 
 
 
* Note, the interface itself can be public OR package accessible, but members get default public access as described above.
 
  
 
<syntaxhighlight lang="java5">
 
<syntaxhighlight lang="java5">
  
public interface Mashable {
+
/* This is the foo algorithm
  public int COUNT; //illegal not initialized !
+
   /* which takes foo's and sort's bars */
  int MINCOUNT = 3; //this is public, static and final implcitly.
+
*/
   public static final int MAXCOUNT=10; //full correct declaration
 
 
 
  void foo(); //Implicity: public abstract void foo();
 
  static void foo(); //Illegal;
 
}
 
  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Class Members ==
+
== Final, Transient, Volatile ==
  
* Members can use all the access modifiers: public, private, default(package), protected
+
<u> Final </u>
* Private members are not inherited.
+
* Variables marked as final and once initialized, cannot be reinitialized.
* This means private members can be redeclared in subclasses.
+
* Marking primitives as final is equivalent to making them a constant.
* Instance level variables cannot be synchronized, abstract, strictfp, native.
+
* For object references, final only prevents the reference from being assigned to another object.
 +
* The object however can still be modified.
 +
* There are no final objects, only final references.
 +
* A local final variable must be initialized before it is used. If the variable is never used (blank final) then it wont cause a compiler error.
  
<u> Protected </u>
+
* Final variables can be passed to non-final arguments
* Only accessible through inheritance in the subclass.
 
* The subclass cannot use a reference to super to access the protected member.
 
  
 
<syntaxhighlight lang="java5">
 
<syntaxhighlight lang="java5">
  
package pkg1;
+
public static void main(String[] args) {
 
+
final int x = 5;
public class Foo {
+
System.out.println(go(x)); //6
  protected String str = "Hello";
+
System.out.println(x); //5
}
 
 
 
package pkg2;
 
 
 
public class Bar extends Foo {
 
  public void go() {
 
    Foo f = new Foo();
 
    String s = f.str; //Won't work ! Compiler error
 
    System.out.println(str); //Will work - through inheritance
 
  }
 
 
}
 
}
 
+
package pkg2;
+
private static int go(int y) {
 
+
return ++y;
public class Fubar {
 
  public void goo() {
 
        Bar b = new Bar();
 
        System.out.println(b.str); //wont'w work - str only accessible through inheritance
 
  }
 
 
}
 
}
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<u> Abstract Methods </u>
 
 
As they go against the logic of overriding :
 
 
* abstract methods '''cannot''' be static.
 
* abstract methods '''cannot''' be private.
 
 
== Constructors ==
 
 
* NO return type.
 
* Can have all the access modifiers.
 
* Can't be marked as abstract or final or static.
 
 
== Final, Transient, Volatile ==
 
 
<u> Final </u>
 
* Variables marked as final and once initialized, cannot be reinitialized.
 
* Marking primitives as final is equivalent to making them a constant.
 
* For object references, final only prevents the reference from being assigned to another object.
 
* The object however can still be modified.
 
* There are no final objects, only final references.
 
  
 
<u> Transient </u>
 
<u> Transient </u>
Line 129: Line 62:
 
* Tells the JVM that a thread accessing the variable must synchronize its private cache copy of the variable with the master copy in main memory.
 
* Tells the JVM that a thread accessing the variable must synchronize its private cache copy of the variable with the master copy in main memory.
 
* This is rarely used. Synchronization is used, rather than volatile to keep data thread-safe.
 
* This is rarely used. Synchronization is used, rather than volatile to keep data thread-safe.
 
== Literals ==
 
 
* Default type of an integer literal is int.
 
* Long can be specified by adding l or L
 
* Octal prefix with 0
 
* Hex prefix with 0x or 0X
 
 
 
* Floating point literals default type is double.
 
 
<syntaxhighlight lang="java5">
 
float pi = 3.14; // This will be a compiler error, because 3.14 is a fp literal which is double by default
 
double pi = 3.14; //OK
 
float pi = 3.14f; //OK
 
</syntaxhighlight>
 
 
* Double can also be explicitly by adding the suffix d or D
 
* Float is denoted by suffix f or F
 
 
* Integers are stored in 2 complements's notation
 
* Leftmost bit is the sign bit, the remaining bits are used to store the value
 
e.g. of Byte
 
* Byte - 1 byte = 8 bits.
 
* Leftmost bit is to store the sign. So 7 bits are available for the value, so 2^7 = 128 values
 
* Negative range is -128 to -1 (128 numbers)
 
* Positive range is 0 to 127 (128 numbers, 0 is stored as a positive number)
 
* So a byte's range is -2^7 to (2^7 - 1)
 
 
* char's take up 2 bytes
 
* they are stored as '''unsigned''' 16-bit integers. This means they will represent a total of 2^16 positive values (0 to 65535 (2^16-1) )
 
* contrast with short which also uses 2 bytes. But it's max positive range is 2^15 -1. (Since 1 bit is to used to represent the sign)
 
 
<u> Comments </u>
 
* Comment start sequences like //, /*. /** are ignored within comment blocks.
 
* So trying to nest multiple-line comments will result in an error
 
 
<syntaxhighlight lang="java5">
 
/* This is the foo algorithm
 
  /* which takes foo's and sort's bars */
 
*/
 
</syntaxhighlight>
 
 
== Assignments ==
 
 
Watch out!
 
 
<syntaxhighlight lang="java5">
 
float f = 2;  //This is legal. 2 is an int which fits in a float.
 
float f = 2.0; //Illegal. 2.0 is a double which by default won't fit in a float.
 
</syntaxhighlight>
 
  
 
== Local variables ==
 
== Local variables ==
Line 213: Line 95:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
== Shadowing ==
  
 +
* The more local variable will always mask the instance level variable with the same name.
 +
* "this" can be used to qualify the variable.
 +
 +
<syntaxhighlight lang="java5">
 +
 +
class Bar {
 +
  private int i = 10;
 +
 +
  public void go() {
 +
      foo(20);
 +
  }
 +
 
 +
  private void foo(int i) {
 +
        System.out.println(i); //local i - shadows instance i. Will print 20;
 +
        System.out.println(this.i); //Refers to instance i, will print 10;
 +
  }
 +
 +
}
 +
 +
</syntaxhighlight>
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Latest revision as of 11:27, 6 September 2011

Legal Identifiers

  • Can be composed of characters, numbers, currency-symbols and connecting chars such as underscore.
  • Definition of characters are interpreted according to the charset.
  • NO special chars such as #, :, ;, @, -, etc...
  • Must start with letter, currency char, underscore but CANNOT start with a digit !
  • Java identifiers are case-sensitive.

Illegal identifiers:

  • int 23age;
  • String em@iladd;
  • List all-accounts;

These are legal!:

  • int __;
  • String $;

Comments

  • Comment start sequences like //, /*. /** are ignored within comment blocks.
  • So trying to nest multiple-line comments will result in an error
/* This is the foo algorithm
   /* which takes foo's and sort's bars */
*/

Final, Transient, Volatile

Final

  • Variables marked as final and once initialized, cannot be reinitialized.
  • Marking primitives as final is equivalent to making them a constant.
  • For object references, final only prevents the reference from being assigned to another object.
  • The object however can still be modified.
  • There are no final objects, only final references.
  • A local final variable must be initialized before it is used. If the variable is never used (blank final) then it wont cause a compiler error.
  • Final variables can be passed to non-final arguments
public static void main(String[] args) {
		final int x = 5;
		System.out.println(go(x)); //6
		System.out.println(x); //5
}
	
private static int go(int y) {
		return ++y;
}

Transient

  • Applies only to instance variables.
  • Tells the JVM to skip this variable when serializing the object it is part of.

Volatile

  • Applies only to instance variables.
  • Tells the JVM that a thread accessing the variable must synchronize its private cache copy of the variable with the master copy in main memory.
  • This is rarely used. Synchronization is used, rather than volatile to keep data thread-safe.

Local variables

  • No access modifier is applicable to local variables.
  • The only modifier that can be applied to local variables is final.
  • Local variables are not automatically initialized, unlike instance and static variables.
  • Compiler will complain when uninitialized local variable is first USED. If you don't try to use an uninitialized variable, it will be OK.
  • Compiler can however detect in very simple conditional cases, that a local variable will be initialized, but will complain in complex cases.

e.g.

private void foo() {
   int i;
   System.out.println("i : " + i); //Compiler error

   if(true) {
      i = 10;
   } else {
      i = 20;
   }
   System.out.println("i : " + i); //OK. Compiler can figure out i will be definitely initialized.

   int j;
   boolean b = true;
   if(b) {
     j = 10;
   } 
   System.out.println("j : " + j); //Compiler error - can't figure out if j will be initialized or not.
}

Shadowing

  • The more local variable will always mask the instance level variable with the same name.
  • "this" can be used to qualify the variable.
class Bar {
  private int i = 10;

  public void go() {
       foo(20);
  }
  
  private void foo(int i) {
        System.out.println(i); //local i - shadows instance i. Will print 20;
        System.out.println(this.i); //Refers to instance i, will print 10;
  }

}