Java - Tutorial 00 Reference

Discussion in 'Tutorials' started by SWH.PVT, Feb 11, 2012.

  1. SWH.PVT Lance Corporal

    Contributions:
    18
    What is Java?

    Java is a general-purpose, object-oriented language that is specifically designed to have as few implementation dependencies as possible.

    The language derives much of its syntax from C/C++; however, there are substantial differences between Java and C/C++ internally. For
    example, Java programs can run on different operating systems without having to recompile. Java achieves this through the use of the Java
    Virtual Machine (JVM), which is part of the Java Runtime Environment (JRE).

    You can think of the JVM as being a mini-computer that can be downloaded to your computer. The "machine code" that the JVM needs is
    called bytecode, and the JVM is capable of translating bytecode into the real machine code that your computer actually needs in order to
    execute a given Java program. So, what this all means is that as long as a computer has the appropriate JVM installed, the computer will be
    able to execute a Java program regardless of the operating system.


    So Why use Java?

    For one thing it's a very portable language! Even more so than C/C++.

    While it's true that any language can be compiled or interpreted, most languages are designed to go one way or the other.
    For example, C/C++ was designed to be compiled whereas Java was designed to be interpreted.

    Some interpreted languages work by executing the source code directly, one line at a time.
    However, Java works by first converting source code into intermediate code (bytecode) and then executing this pseudo-code instead.

    You can see how portability was a major motivator here for this design choice. For example, it's possible to write a highly portable Java
    program that results in bytecode that can run on any operating system that has a JRE available.

    This is also the reason that Java has become so popular on the web where internet sites have utilized Java in the creation of online games
    and various embedded applications.


    This All Sounds Good but What is the Catch?

    In general, interpreted languages run slower than compiled languages.

    The reason for this is simple. A compiled language translates source code directly into binary machine code that can be executed on a
    particular operating system. On the other hand, an interpreted language like Java has an additonal step of translating bytecode into machine
    code before the program can be executed.

    Since the language first appeared in the mid 90's, Java has seen several updates, new features, enhanced optimizations, etc. There have
    been sophisticated optimizations made in the compilation-phase (JIT) that have made some dramatic improvements to running-times.

    In fact, there have been benchmark testings that have placed the running-times for Java programs approaching that of C/C++ programs, for
    example, and it's even possible for a Java program to actually run faster than C/C++ in some architectures due to these sophisticated
    optimizations.

    You probably asked yourself how in the world is this possible? It's a valid question since it seems by nature contradictory. You can think
    about it this way, C/C++ programs will most likey be compiled on a particular computer architecture using some mixed optimizations that
    works well for most computers (of that operating system) but that haven't fully taken advantage of the full power of specific computers.
    Some of these mixed optimizations are a direct result of - portability!


    How do I Install Java?

    You can find more information about Java and relevant software downloads here.

    In general, if you only want to execute Java programs then all you need is the JRE. However, if you want to write and compile your own
    programs then you will need a Java Development Kit (JDK).

    ====================================================================================================

    How to Compile Your First Java Program

    I will be describing how to use the command prompt (or terminal) to compile and execute Java programs. Since I'm currently writing from
    a computer that has Microsoft Windows 7 installed, I will be giving directions specific to this operating system.

    After you have downloaded the appropriate JDK for your operating system, we are going to alter your environment path variable so that
    your computer can locate the necessary files to compile your first Java program, e.g. javac, etc.

    Before we start it would be a good idea to find the directory location of the bin folder of your JDK installation. This is the directory where all
    of the important files are located to compile and execute Java programs.

    For me, this directory is located here: C:\Program Files\Java\jdk1.7.0_02\bin
    If you have multiple JDK directories, pick the latest and most recent one.

    For Windows 7, follow these directions:

    1. Open Windows Explorer
    2. Navigate to Control Panel\System and Security\System (you can copy and paste this path)

    Note: There are multiple ways to navigate to control panel.
    3. Click on Advanced System Settings.
    4. Under the Advanced tab, click on Environment Variables.
    5. Under System variables, scroll to the Path variable.
    6. Select this row and click on Edit...
    7. Under the Variable value, add a semicolon, i.e. ; to the very end of this variable value
    8. Copy/Paste the directory location of the bin folder of your Java installation.
    9. Click OK out of everything!


    Now on with the example code. Copy the following code and save it as a text file with the following name: HelloWorld.java

    Code:
    public class HelloWorld
    {
         public static void main(String[] args)
         {
              System.out.println("Hello, World!");
         }
    }
    
    Open a command prompt and navigate to where you saved your program. I saved mine to my desktop.

    You use the following programs to compile and execute your Java program:

    1. javac.exe to compile Java source code into Java bytecode.
    2. java.exe to execute your Java class files (bytecode).

    This is what my command prompt looks like after I executed my program:

    C:\Users\James>cd desktop
    C:\Users\James\Desktop>javac HelloWorld.java
    C:\Users\James\Desktop>java HelloWorld
    Hello, World!


    This is it for now but I will post another tutorial that further describes the Java language with code examples.
  2. loeri loeri.tk

    Contributions:
    45
    Specialties:
    Mapper, Scripting, Programming, Support
    Processing:
    Graphics:
    PREMIUM
    I Donated
    Ah i love java :) I ll now have to make a game-project in java for school.
    Still have to learn the GUI part :L
    I think i can ask for help here? :bigrin:
  3. SWH.PVT Lance Corporal

    Contributions:
    18
    You should look over java's api and know what's all available here.

    Sure you can ask for help if you get stuck on something.

Share This Page