Java Basic Knowledge

A Java program can be defined as a collection of objects that communicate via invoking each other’s methods.

  • Java is a Object-Oriented Language:
    • Polymorphism
    • Inheritance
    • Encapsulation
    • Abstraction
    • Classes: A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.
    • Objects: Objects have states and behaviors.
    • Instance
    • Method
    • Message Parsing

Inheritance:

In Java, classes can be derived from classes. Basically if you need to create a new class and here is already a class that has some of the code you require, then it is possible to derive your new class from the already existing code.
This concept allows you to reuse the fields and methods of the existing class without having to rewrite the code in a new class. In this scenario the existing class is called the superclass and the derived class is called the subclass.

Interfaces:

In Java language, an interface can be defined as a contract between objects on how to communicate with each other. Interfaces play a vital role when it comes to the concept of inheritance.
An interface defines the methods, a deriving class(subclass) should use. But the implementation of the methods is totally up to the subclass.

Variable Types inside Classes:

  • Local variable: There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.
  • instance variable: When a space is allocated for an object in the heap, a slot for each instance variable value is created. Within static methods, instance variable should be called using the fully qualified name: ObjectReference.VariableName.
  • class variable(with the static keyword): There would only be one copy of each class variable per class, regardless of how many objects are created from it: ClassName.VariableName

Java Data Types:

  • Primitive Data Types:
    • byte: 8-bit signed, -128(-2^7) ~ 127 (inclusive)(2^7 -1)
    • short: 16-bit signed, -32,768(-2^15) ~ 32,767 (inclusive) (2^15 -1)
    • int: 32-bit signed, -2,147,483,648(-2^31) ~ 2,147,483,647(inclusive)(2^31 -1)
    • long: 64-bit signed, (-2^63) ~ (2^63-1)
    • float: single-precision 32-bit,
    • double: double-precision 64-bit, the default data type for decimal values.
    • boolean: one bit, true and false.
    • char: single 16-bit Unicode character, Minimum value is ‘\u0000’ (or 0), Maximum value is ‘\uffff’ (or 65,535 inclusive).
  • Reference Data Types: Class

Java Class Types:

  • abstract classes
  • final classes
  • Inner classes
  • Anonymous classes.

Java Modifiers:

Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There are two categories of modifiers:

  • Access Modifiers:
    • default: Visible to the package
    • public: Visible to the world
    • protected: Visible to the package and all subclasses
    • private: Visible to the class only
  • Non-access Modifiers:
    • The static modifier for creating class methods and variables
    • The final modifier for finalizing the implementations of classes, methods, and variables.
    • The abstract modifier for creating abstract classes and methods.
    • The synchronized and volatile modifiers, which are used for threads.

Java Basic Operators

http://www.tutorialspoint.com/java/java_basic_operators.html
The Bitwise Operators:
works on bits and performs bit-by-bit operation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Test {
public static void main(String args[]) {
int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
System.out.println("a & b = " + c );
c = a | b; /* 61 = 0011 1101 */
System.out.println("a | b = " + c );
c = a ^ b; /* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );
c = ~a; /*-61 = 1100 0011 */
System.out.println("~a = " + c );
c = a << 2; /* 240 = 1111 0000 */
System.out.println("a << 2 = " + c );
c = a >> 2; /* 15 = 1111 */
System.out.println("a >> 2 = " + c );
c = a >>> 2; /* 15 = 0000 1111 */
System.out.println("a >>> 2 = " + c );
}
}

The instance of Operator:

1
boolean result = "str" instanceof String;

Java Loop Control

1
2
3
4
5
6
7
8
9
10
11
12
13
while (x < 10){
do sth;
x++;
}
for (int i=0; i<10; i++) {
do sth;
}
do {
sth;
x++;
} while (x < 10)

Advanced for loop

1
2
3
4
5
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}

Java Decision Making

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if(x){
xxx
} else if {
xxx
} else {
xxx
}
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
Contents
  1. 1. Inheritance:
  2. 2. Interfaces:
  3. 3. Variable Types inside Classes:
  4. 4. Java Data Types:
  5. 5. Java Class Types:
  6. 6. Java Modifiers:
  7. 7. Java Basic Operators
  8. 8. Java Loop Control
  9. 9. Java Decision Making
|