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]);
    }
} 

Saturday, May 14, 2016

It always starts with Hello, doesn't it.

There are so many names flying around these days, web, app, mobile, responsive, e-commerce, not to mention all the inhuman acronyms. It's really confusing and daunting, isn't it?

Let me tell you, you do not need a CS or EE degree to understand it. If you can read instructions and replace the flapper in your toilet, you can assemble a webpage. It's easier than that.

I suppose you have an text editor, so try this now. (Need help?) Here is an example from w3schools.com.

HTML Example:

<!DOCTYPE html>
<html>
<title>HTML Tutorial</title>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

If you open the file in your browser window, it will look like this.


W3schools.com provides a very helpful interactive tool for you to try . You can replace the phrase and click the "See Result" button. See what happened?

You see where everything goes, right? First, the element "<!DOCTYPE html>" always has to be the first line of the file and it tells the browser what language the file is written in.

Just like how a period is used to end a sentence, in HTML, we use tags to indicate the beginning and end of a section and these tags are enclosed with angled brackets. There are a few exceptions but usually they come in pairs, with "<tag_name>" to mark the beginning and "</tag_name>" to mark the end.

In this example, there are
<html> The Whole Program </html>
<title> Title on Tab </title>
<body> Body content </body>
<h1> Heading </h1>
<p> Paragraph </p>

You can imagine this is like a Russian doll with layers nesting layers. The <html> and </html> pair enclose everything on a page in HTML. Then there is usually a "head" part and a "body" part. In our example, the "head" part isn't specified explicitly.

With in "head", there are many things you can specify, such as the style or some special characters used herein. These don't show up in the content you'll see on the page in the browser. The "title" we see here is to specify the name shown on the tab of this page. The "body" part is where the contents go. We'll examine that in next posting.



NOTES:


Text Editor (NotePad or TextEdit will do. If you don't want to use the text editor that comes with your computer or you want something that works across multiple platforms, Sublime Text is a popular choice. You can download an evaluation copy for free and purchase a license later. No, I don't get paid if you do. More about it.)

Resources!
Jargons!

Useful Links Bonanza!

Useful Links


   Unix/Linux Like

   Useful Links for Programmers

   Useful Tutorials for Text Editor

   Useful Tutorials and Docs for Tools


Protocols and Languages

Downloads

Past Posts



    Demystify Jargons!


    -- Don't let it scare you off!





    A | B | H | T | Google!


    A

    anchor tag - An element to make it easy for readers to navigate the page.
    use <a name="where_you_want_to_go_to"> to hold the place and use <a href="#where_you_want_to_to_to"> to link to that place on the same page. To link to a web page, use <a href="http://www.google.com">. Don't forget to end it with </a> .

    B

    C

    CRUD - In computer programming, create, read, update and delete (as an acronym CRUD) are the four basic functions of persistent storage.

    H

    HTML - Hyper Text Markup Language.
    It uses a set of markup tags to describe how the documents should be presented.

    T

    test editor - a system or program that allows a user to edit text such as Microsoft Word or Google Docs. It can be as simple as the Notepad or TextEdit that comes with Microsoft Windows or Mac.


    Back to top.

    Mission Statement.

    This blog is to help those who would like to maintain and manage their own website to begin with.

    A lot of bloggers and small business owners have someone develop websites for them and don't want to call them back for minor changes. It's the same thing that we take care of some fix and touchup in our house.

    I am an engineer by trade and I am going to teach you how to do some twist and touchup that you can manage on your own.

    I will start with something very basic so that anyone who can type and can access a browser will be able to try. As the blog goes, you will be able to do more, if you want to.

    Happy coding.

    - Ann