+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Java:Tutorial - "Hello World"

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Java:Tutorial - "Hello World"

    Object:
    To build your very first Java App

    The Idea:
    Like every first tutorial, this tutorial will show you how to display "Hello World," but in two ways. The first most simple way, will print "Hello World" to the console, the second (although I wont fully explain everything until later tutorials) will display "Hello World" using the OOP aspect of Java.

    Prerequisites:
    No Previous knowledge of Java is needed but you should have read this tutorial:
    http://forum.codecall.net/tutorials-...g-started.html

    The Tutorial:
    1. Create a new class called MyFirstApp

    [highlight=java]package helloworld;
    public class MyFirstApp {
    }
    [/highlight]

    2. Above is the basic structure of a class. The first line is the package declaration. It serves as a "folder" that holds a bunch of class's. The second line is the class declaration header. The next step is to create a constructor.

    Code:
    package helloworld;
    public class MyFirstApp {
        public MyFirstApp(){
        }
    }
    3. The third line is the constructor declaration. It is started with an access modifier public/private and then followed by the name of the class with an closed set of parentheses. At this point we don't have much so lets get to the good stuff!

    Code:
    package helloworld;
    public class MyFirstApp {
        public MyFirstApp(){
        System.out.println("Hello World!");
        }
    }
    4. At this point we have a fully functioning class. However we need to create a means of starting it. There are two ways, you could implement an applet or make it an application. At this point, lets not get hung up on the differences or the what the syntax means just know it works. In our example we will be using an application

    Code:
    package helloworld;
    public class MyFirstApp {
        public MyFirstApp(){
        System.out.println("Hello World!");
        }
        public static void main(String[] args){
        new MyFirstApp();
        }
    }
    5. public static void main(String[] args){ is the method java looks for to initialize the application. The "new" creates a new instance of the class in the computers memory and viola your code works.

    VIDEO TUTORIAL: Temp

    However, printing words to the console can get pretty boring, to show you some of Javas capabilities, I will give you a brief introduction to working with swing. Although I wont explain in depth the reason behind each line of code as that is beyond the scope of this tutorial.

    1. Create a class as shown above, but this time name it MySecondApp

    2. Create a constructor and instantiate it in the main method. At this point we should have something like this:
    Code:
    package helloworld;
    
    public class MySecondApp {
        
        public MySecondApp(){
        }
        public static void main(String[] args) {
            new MySecondApp();
        }
    }
    3. Next we are going to import the swing package that is avalable to us
    Code:
    package helloworld;
    
    import javax.swing.*;
    
    public class MySecondApp {
        
        public MySecondApp(){
        }
        
        public static void main(String[] args) {
            new MySecondApp();
        }
    
    }
    4. Next we are going to extend JFrame (which is a class in the swing package) so we can inherit its capabilities.

    [highlight=Java]package helloworld;

    import javax.swing.*;

    public class MySecondApp extends JFrame {

    public MySecondApp(){

    }
    public static void main(String[] args) {
    new MySecondApp();
    }
    }[/highlight]

    5. Finally we are going to create a window using the the JFrame methods. I will discuses these more in depth in later tutorials.
    [highlight=Java]package helloworld;

    import javax.swing.*;

    public class MySecondApp extends JFrame {

    public MySecondApp(){
    setSize(150,60);
    setLocation(20,20);
    JLabel myLabel = new JLabel("Hello World!");
    add(myLabel);
    setVisible(true);
    }
    public static void main(String[] args) {
    new MySecondApp();
    }

    }[/highlight]

    VIDEO TUTORIAL: Temp

    And your done

    Originally posted as Java – Hello World
    Last edited by John; 08-01-2010 at 10:11 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    AfTriX is offline Programming God
    Join Date
    Jan 2007
    Location
    Chicago
    Posts
    586
    Rep Power
    0
    Pretty Simple Step by Step guidance. Thanks for the best efforts, Which helped me a lot. Better if you could include the way of compiling and running the program.
    Last edited by AfTriX; 01-18-2007 at 09:32 AM.

  4. #3
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    My tutorials assume you are using Eclipse which allow you to compile and run the app within the IDE

  5. #4
    AfTriX is offline Programming God
    Join Date
    Jan 2007
    Location
    Chicago
    Posts
    586
    Rep Power
    0
    If I am writing the codings in Notepad and Running through Command Prompt then what are the procedures that I got to follow.

    I am not aware of saving the file properly
    - File Name
    - How to Compile
    - How to run

    I get the following errors when I try to run.

    Code:
    javac helloworld.java
    helloworld.java:3: class MyFirstApp is public, should be declared in a file name
    d MyFirstApp.java
    public class MyFirstApp {
           ^
    1 error
    MyFirstApp is compiled perfectly, but not aware of running the prog.

    I'm starting Programing Java through your Tutorials. Help me go on with Java.
    Last edited by AfTriX; 01-18-2007 at 08:50 PM.

  6. #5
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    Well you should then know that javac is the command to compile the java file. Therefore MyFirstApp is not compiled perfectly. The file should be named MyFirstApp.java not helloworld.java. Once you have the file renamed, compile it using
    javac MyFirstApp.java
    which should create another file called MyFirstApp.class which is your java code converted into machine language. To run your application you should use the command
    java MyFirstApp
    of course those commands assume you have the java directory defined in your system variables
    Last edited by John; 01-18-2007 at 11:33 PM.

  7. #6
    AfTriX is offline Programming God
    Join Date
    Jan 2007
    Location
    Chicago
    Posts
    586
    Rep Power
    0
    Once again Thanks a lot for your guidance, I'll try this way and come back to you. I'm keeping on improving my skills on Java. Hope to have a great Programmer "SideWinder" as my guide.

  8. #7
    PCsmasher is offline Newbie
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    How does this happen?

    I have the hello world program from the Sun Java tutorial and it is as follows:

    /**
    * The HelloWorldApp class implements an application that
    * simply prints "Hello World!" to standard output.
    */
    class HelloWorldApp {
    public static void main(String[] args) {
    System.out.println("Hello World!"); // Display the string.
    }
    }

    the code from this tutorial is:

    package helloworld;

    public class MyFirstApp {
    public MyFirstApp(){
    System.out.println("Hello World!");
    }
    public static void main(String[]args){
    new MyFirstApp();
    }
    }

    Both do the same, why?

  9. #8
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    My tutorial prints the code in the constructor and uses the main method to instantiate the class. Once the class is instantiated, the constructor is the first code to run.

    Essentially, in my code, the main method runs, which then calls the constructor which then prints to the console.

    Their tutorial just prints to the console as soon as the main method is invoked rather than calling the constructor.
    Last edited by John; 07-17-2007 at 02:16 PM.

  10. #9
    PCsmasher is offline Newbie
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    as I continue

    I am very new to this, just started in fact. As I continue will I learn about constructors, instantiating, calling, and mains and stuff. Sorry if I sound like a dolt, just trying to understand this and don't know if there is a more basic level I should start on, or if I will learn this stuff as I go.

  11. #10
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    The constructor is a "method" with the same name as the class.

    If you instantiate something, you create a new object, which you can do "things" to.

    public static void main(String[] args) is your main method. The first method that is ran when you run any java application.

    If you instantiate the class in the main method by using the new keyword, you create an object of the class, and the constructor is the first method that is ran after the main method.


+ Reply to Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. INFECTION "Arcophage" Programmer needed for tutorial
    By Asyranok in forum Services for Buy/Sell/Trade
    Replies: 1
    Last Post: 12-08-2010, 08:43 PM
  2. Replies: 2
    Last Post: 11-01-2010, 11:52 PM
  3. Tutorial: PHP "Hello World"
    By Void in forum PHP Tutorials
    Replies: 5
    Last Post: 03-23-2010, 05:27 PM
  4. C++ Tutorial(1) "Hello World".
    By CommittedC0der in forum C Tutorials
    Replies: 11
    Last Post: 01-29-2010, 09:28 AM
  5. Replies: 9
    Last Post: 01-09-2007, 07:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts