Java
  Home arrow Java arrow Page 4 - Java Beginning Programming
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
eWeek
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Beginning Programming
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2007-09-18

    Table of Contents:
  • Java Beginning Programming
  • Terminology
  • Creating the Goodbye Cruel World application
  • Understanding the Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Java Beginning Programming - Understanding the Code
    (Page 4 of 4 )

    Now that you've created a program to bid good riddance to those who laughed when you told them about your impending job with NASA, it's time to examine the code you've just written to understand how it works.

    The first section we will look at is the Source Code Comments, shown below.

    /*
    * This application will print the words “Goodbye Cruel World” across
    * your monitor. Also note that these comments are in your code for
    * other programmers, and is invisible to the compiler.
    */

    As you can see, the /* informs the computer that this is where you will be making comments and that it is not allowed to read these, no matter how curious it might get. These comments are for other programmers, should they ever need to fix your code or add something to it. After all, with all your brains and chutzpah, you won't be in the same position forever.

    To close the Comment section and let the computer know that it is okay to look again, simply type */ to end the comment. You can open another comment anywhere within the application you are creating and in fact, it is recommended that you do so, and often.

    // text

    In the above example, the computer ignores everything from // to the end of the line.

    Next we will look at the Class Definition section of the Code. Don't fret too much over this new phrase at the moment; I will discuss it in more depth in the second part of this tutorial.

    class GoodbyeCruelWorldApp {
     
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
      }
    }

    In the above sample, the bold lines indicate the opening and closing parts of the Class Definition.

    Finally, we come to the Main Method:

    public static void main(String[] args)

    Every program you write within Java must contain this Main Method. Basically, it passes information to your application that it will need to perform the function you created it to do.

    And lastly, the line

    System.out.println("GoodbyeCruelWorld!");

    tells the program to print the phrase “GoodbyeCruelWorld!” (or any other phrase you place within it) to your screen.

    Let's say we want to work with numbers. In this example, you will see something called variables. Don't worry about those just yet, as we will be touching upon them in great detail in future articles. In this sample, the program is going to multiply one number by another number and print out the result.

    /*
    A program to calculate numbers.
    We will name this program “TheMathWizard.java”.
    */
    class TheMathWizard {
      public static void main(string args[]) {
        int num; // this is how you declare a variable and name it num
        num=4000 //this places the value 4000 in our variable named num
        System.out.println(“This is your puny number!: “ + num);
        num = num*2; //this tells the program to multiply our variable num
        // by two
        System.out.print (“The total of your puny number * 2 is “);
        System.out.println(num);
      }
    }

    After running this mathematically complex program, you should see the following on your screen:

    This is your puny number! : 4000
    The total of your puny number *2 is 8000

    If we wanted to complicate matters, we could have the program ask you for a number to multiply by or have the user enter a number to be multiplied (or both). But we'll save that for another day.

    You are now free to take a break. Go update your resume and get ready to send it to NASA. But don't mail it off just yet. The second part of the "How to Program in Java" tutorial will be coming soon.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The last example is missing a semicolon @ num=4000and in public static...
       · Hey thanks for catching that one...not sure how I missed it. -James Payne
       · Thank you for taking the time to read my article covering the basics of Java...
       · This information is not understood by me & my friends. So please stop your new...
       · Hey, sorry you didn't like my article. I try to make em easy to understand, but...
     

       

    JAVA ARTICLES

    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections
    - Combating the ‘Object Crisis’

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway