Friday, May 20, 2016

JAVA 101.

In traditional programming, source code is compiled to executable code, such as .exe files on Windows systems. A Java program is compiled to bytecode, which is portable, cross-platform and system independent. Bytecode can only run on a Java Virtual Machine (JVM) that interprets bytecode to machine dependent native code. Currently JVMs are available for Windows, Mac OS, Unix, Linux, Free BSD and all major operating systems. This model with JVMs to run the bytecode also makes Java language more secure than other languages.


Command:
// If you have a class named "MyClass" defined in a file
// named "MyClass.java",
// the following will output a compiled class in a new file
// called "MyClass.class"
% javac MyClass.java

// Use JVM (java) to run the class
% java MyClass

// If you have multiple classes defined in a file, all the
// classes will be produced in individual class files.

JDK, JRE, JVM
  • JRE - Java Runtime Environment, which contains both a JVM and class libraries.
  • JDK - Java Design Environment, which includes JRE, a compiler and other tools.

Java 2, J2SE, J2EE, J2ME, Java 8
  • Java 2 - JDK 1.2
  • J2SE - Java 2 Platform, Standard Edition, which is basically the JDK.
  • J2EE - Java 2 Platform, Enterprise Edition. It defines the standards for developing component-based multi-tier enterprise applications, including web services support and development tools.
  • J2ME - Java 2 Platform, Micro Edition.
  • Java 8 - Java SE 8.

Java EE 6 and 7 Servers
  • Oracle WebLogic
  • IBM WebSphere
  • GlassFish
  • JBoss
  • Wildfly
  • Apache Geronimo
  • Apache TomEE

IDE and Links

First Program:
// This is my first program : Print "Java Rocks!"
// Java comments
/* Java comments */
// /* Java comments */
class firstProgram {
   public static void main (String[] args) {
      System.out.println ("Java Rocks!");
   }
}
Common Classes:

  • java.lang.Math
  • java.lang.String
  • java.util.Scanner

Primitives:

  • byte : 8-bit (-128 to 127)
  • short : 16-bit (-32768 to 32767)
  • int : 32-bit (-2,147,483,648 to 2,147,482,647) (default type for integer literals)
  • long : 64-bit long integer
  • float : 32-bit single-precision floating point (14e-45 to 3.4028234e38)
  • double : 64-bit double-precision floating point (default type for number literals with decimals)
     (4.9e-324 to 1.7976931348623157e308)
  • char : A Unicode character 
  • boolean : true or false
  • Default values:
boolean byte short int long char float double object reference
false 0 0 0 0L \u0000 0.0f 0.0d null

Variables:

  • Two types of variables:
    • Reference types. (Usually reference to an object)
    • Primitive types.
  • Variable name is its identifier. (legal: x2, _x3, addr_count, userName, $var) 
  • Variable Declaration
    • In a class body as class fields.
    • As parameters of a method or constructor.
    • In a method's body or a constructor's body.
    • Within a statement block.
  • Variable Scope
    • refers to the accessibility of a variable.
    • Variables defined in a block are only accessible from within the block.
  • Variable Passing
    • Primitive variables are passed by value.
      (JVM copies the value of the passed-in variable to a new local variable.
      If you change the local value, it will not affect the passed in primitive variable.)
    • Reference variables are passed by reference.
      (Local variable will refer to the same object as the passed in reference variable.
      If you change the contents of the object referenced within the method, the change
      will be reflected in the calling code.)

Java Keywords:
abstract continue for new switch
assert default if package synchronize
boolean do goto private this
break double implements protected throw
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
Examples:
// Use 'final' for constants
final int rowCount = 50;
final boolean switchOn = true;

//--------------------------------------------------------------
// The fixed value assigned to a variable is called a "Literal".
// 200 is a "Integer Literal". 'c' is a "Character Literal".
// More literals and the format
byte binNum = 0b1100; // Java 2
short octNum = 0567;
int hexNum = 0x222;
int million= 1_000_000; // Java 2
long a = 123L;
// An integer literal without a suffix 'L' is regarded as an 'int'.
long a = 123; // WRONG! 
              // Above line will generate a compile error
// A floating literal has to come with a suffix 'F' or 'f'
float floatNum = 3.14F; // CORRECT!
              // Without 'F' it will be regarded as an 'double'.
double dblNum = 9e-9d; // Here the 'D' or 'd' can be skipped.

//--------------------------------------------------------------
char specialBackspace = '\b';
char specialTab = '\t';
char specialBackslash = '\\';
char specialSingleQuote = '\'';
char specialDblQuote = '\"';
char specialLinefeed = '\n';
char specialCarriageReturn = '\r';
char unicodeBritishPound = '\u00A3';

//--------------------------------------------------------------
// widening conversion and narrowing conversion
int a = 10;
long b = a; // widening a to fit b
int c = (int) b;
Operators:

= > < ! ~ ? : instanceof
== <= >= != && || ++ --
+ - * / & | ^ % << >> >>>
+= -= *= /= &= |= ^= %= <<= >>= >>>=

Arrays:

Array Demo:

class ArrayDemo {
    public static void main(String[] args) {
        // declares an array of integers
        int[] anArray;

        // allocates memory for 10 integers
        anArray = new int[10];
           
        // initialize first element
        anArray[0] = 100;
        // initialize second element
        anArray[1] = 200;
        // and so forth
        anArray[2] = 300;
        anArray[3] = 400;
        anArray[4] = 500;
        anArray[5] = 600;
        anArray[6] = 700;
        anArray[7] = 800;
        anArray[8] = 900;
        anArray[9] = 1000;

        System.out.println("Element at index 0: "
                           + anArray[0]);
        System.out.println("Element at index 1: "
                           + anArray[1]);
        System.out.println("Element at index 2: "
                           + anArray[2]);
        System.out.println("Element at index 3: "
                           + anArray[3]);
        System.out.println("Element at index 4: "
                           + anArray[4]);
        System.out.println("Element at index 5: "
                           + anArray[5]);
        System.out.println("Element at index 6: "
                           + anArray[6]);
        System.out.println("Element at index 7: "
                           + anArray[7]);
        System.out.println("Element at index 8: "
                           + anArray[8]);
        System.out.println("Element at index 9: "
                           + anArray[9]);
    }
} 

No comments:

Post a Comment