Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * - 1 votes

Java:Tutorial - "Hello World"

hello world

  • Please log in to reply
20 replies to this topic

#1 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 08 December 2006 - 10:06 PM

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.codecal...ng-started.html

The Tutorial:
1. Create a new class called MyFirstApp

package helloworld;
public class MyFirstApp {
}


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.

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!

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

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:
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
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.

package helloworld;

import javax.swing.*;

public class MySecondApp extends JFrame {

public MySecondApp(){

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


5. Finally we are going to create a window using the the JFrame methods. I will discuses these more in depth in later tutorials.
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();
}

}


VIDEO TUTORIAL: Temp

And your done :D

Originally posted as Java – Hello World

Edited by John, 01 August 2010 - 09:11 AM.

  • 0

#2 AfTriX

AfTriX

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 541 posts

Posted 18 January 2007 - 09:15 AM

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.
  • 0

#3 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 18 January 2007 - 12:00 PM

My tutorials assume you are using Eclipse which allow you to compile and run the app within the IDE
  • 0

#4 AfTriX

AfTriX

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 541 posts

Posted 18 January 2007 - 08:39 PM

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.

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.
  • 0

#5 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 18 January 2007 - 11:30 PM

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
  • 0

#6 AfTriX

AfTriX

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 541 posts

Posted 19 January 2007 - 09:04 PM

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.
  • 0

#7 Guest_PCsmasher_*

Guest_PCsmasher_*
  • Guest

Posted 17 July 2007 - 12:34 PM

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?
  • 0

#8 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 17 July 2007 - 01:12 PM

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.
  • 0

#9 Guest_PCsmasher_*

Guest_PCsmasher_*
  • Guest

Posted 17 July 2007 - 01:23 PM

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.
  • 0

#10 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 17 July 2007 - 01:51 PM

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.

:)
  • 0

#11 Guest_PCsmasher_*

Guest_PCsmasher_*
  • Guest

Posted 17 July 2007 - 01:58 PM

Ok, after looking at your two posts and the code from the program, it is starting to make sense to me. Thanks for you time and patience.
  • 0

#12 Maze

Maze

    CC Regular

  • Just Joined
  • PipPipPip
  • 32 posts

Posted 07 December 2008 - 03:05 AM

For the first step, i think it is better to do tutorials over Console. for example:

This code is simpler and much more clear for any newbie:
System.out.println("Hello World");

Rather than doing some complex operations (For a new one yeah "Complex").

Thanks.
  • 0





Also tagged with one or more of these keywords: hello world

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download