Blog Archive

Friday, July 29, 2016

Java Programming

JAVA – LESSON No. 01
The Java Programming Language
  • The Java programming language is a high-level language that can be characterized by all of the following buzzwords:
    • Simple
    • Architecture neutral
    • Object oriented
    • Portable
    • Distributed
    • High performance
    • Multi threaded
    • Robust
    • Dynamic
    • Secure
The Java Platform
  • A platform is the hardware or software environment in which a program runs.
  • Some of the most popular platforms are Microsoft Windows, Linux, Solaris OS, and Mac OS.
  • Most platforms can be described as a combination of the operating system and underlying hardware.
  • The Java platform differs from most other platforms is that it's a software-only platform that runs on top of other hardware-based platforms.
  • The Java platform has two components:
    • The Java Virtual Machine
    • The Java Application Programming Interface (API)

The Java Virtual Machine
  • In the Java programming language, all source code is first written in plain text files ending with the .java extension.
  • Those source files are then compiled into .class files by the javac compiler.
  • A .class file does not contain code that is native to your processor; it instead contains byte codes— the machine language of the Java Virtual Machine (Java VM).
  • The java launcher tool then runs your application with an instance of the Java Virtual Machine.

The API is a large collection of ready-made software components that provide many useful capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known as packages.

What Can Java Technology Do?
  • The general-purpose, high-level Java programming language is a powerful software platform. Every full implementation of the Java platform gives you the following features:
    • Development Tools: The development tools provide everything you'll need for compiling, running, monitoring, debugging, and documenting your applications. As a new developer, the main tools you'll be using are the java compiler, the java launcher, and the Javadoc documentation tool.
    • Application Programming Interface (API): The API provides the core functionality of the Java programming language. It offers a wide array of useful classes ready for use in your own applications. It spans everything from basic objects, to networking and security, to XML generation and database access, and more. The core API is very large.
    • Deployment Technologies: The JDK software provides standard mechanisms such as the Java Web Start software and Java Plug-In software for deploying your applications to end users.
    • User Interface Toolkits: The Swing and Java 2D toolkits make it possible to create sophisticated Graphical User Interfaces (GUIs).
    • Integration Libraries: Integration libraries such as the Java IDL API, JDBC™ API, Java Naming and Directory Interface™ (JNDI) API, Java RMI, and Java Remote Method Invocation over Internet Inter-ORB Protocol Technology (Java RMI-IIOP Technology) enable database access and manipulation of remote objects.
The NetBeans Software



  • We're going to be create a Java Application, so select Javaunder Categories, and then Java Application under Projects. Click the Next button at the bottom to go to step two;


  • In the Project Name area at the top, type a Name for your Project. Notice how the text at the bottom changes to match your project name (in the text box to the right of Create Main Class):
    • Main
  • If we leave it like that, the Class will have the name Main. Change it to FirstProject:


  • Now, the Class created will be called FirstProject, with a capital "F", capital "P".
  • The package is also called firstproject, but with a lowercase "f" and lowercase "j".
  • The default location to save your projects appears in the Project Location text box.
  • You can change this, if you prefer. NetBeans will also create a folder with your project name, in the same location.
  • Click the Finish button and NetBeans will go to work creating all the necessary files for you.
  • When NetBeans returns you to the IDE, have a look at the Projects area in the top left of the screen (if you can't see this, click Window > Projectsfrom the menu bar at the top of the software):


Click the plus symbol to expand your project, and you'll see the following:




Now expand Source Packages to see your project name again. Expand this and you'll see the Java file that is your source code.




This same source code should be displayed to the right, in the large text area. It will be called FirstProject.java. If you can't see a code window, simply double click FirstProject.java in your Projects window above. The code will appear, ready for you to start work.




The coding window that appears should look like this (we've changed the author's name):

Java Comments
  • When you create a New Project in NetBeans, you'll notice that some text is grayed out, with lots of slashes and asterisks.
  • The grayed-out areas are comments.
  • When the program runs, comments are ignored. So you can type whatever you want inside of your comments.
  • But it's usual to have comments that explain what is you're trying to do.
  • You can have a single line comment by typing two slashes, followed by your comment:
    • //This is a single line comment
  • If you want to have more than one line, you can either do this:
    • //This is a comment spreading
      //
      over two lines or more
  • Or you can do this:
    • /*
      This is a comment spreading
      over two lines or more
      */

  • There's also something called a Javadoc comment.
  • You can see two of these in the coding image on the previous page.
  • A Javadoc comment starts with a single forward slash and two asterisks ( /** ) and ends with an asterisk and one slash ( */ ).
  • Each line of the comment starts with one asterisk.

  • /**
    *
    This is a Javadoc comment 
    */

  • Javadoc comments are used to document code. The documented code can then be turned into an HTML page that will be helpful to others. You can see what these look like by clicking Run from the menu at the top of NetBeans. From the Run menu, select Generate Javadoc.
  • Hello World Application
  • When you created this project, you left the Create Main Class checkbox selected in the New Project wizard.
  • The IDE has therefore created a skeleton class for you.
  • You can add the "Hello World!" message to the skeleton code by replacing the line:
    • // TODO code application logic here
  • with the line:
    • out.println("Hello World!"); // Display the string.

Warning : Type all code, commands, and file names exactly as shown. Both the compiler (javac) and launcher (java) are case-sensitive, so you must capitalize consistently.

Compile the Source File into a .class File
  • To compile your source file, choose Build | Build Main Project from the IDE's main menu.
  • The Output window opens and displays output similar to what you see in the following figure:

  • If the build output concludes with the statement BUILD SUCCESSFUL, congratulations! You have successfully compiled your program!
  • If the build output concludes with the statement BUILD FAILED, you probably have a syntax error in your code.
  • Errors are reported in the Output window as hyper-linked text.
  • You double-click such a hyper-link to navigate to the source of an error.
  • You can then fix the error and once again choose Build | Build Main Project.
  • When you build the project, the byte code file FirstProject.class is generated.
  • Now that you have built the project, you can run your program.

The main Method
  • In the Java programming language, every application must contain a main method whose signature is:
    • public static void main(String[] args)
  • The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above.
  • You can name the argument anything you want, but most programmers choose "args" or "argv".
  • The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
  • The main method accepts a single argument: an array of elements of type String.
    • public static void main(String[] args)
  • This array is the mechanism through which the runtime system passes information to your application.
    • For example:java My App arg1 arg2
  • Each string in the array is called a command-line argument.
  • Command-line arguments let users affect the operation of the application without recompiling it.
  • For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:
    • descending

THANK YOU!

No comments: