Lost Password?

  #1 (permalink)  
Old 12-09-2006, 12:06 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,345
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default 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

java Code:
  1. package helloworld;
  2. public class MyFirstApp {
  3. }

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: http://www.extreme-hq.com/other/Vide...loWorldOne.wmv

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.

Java Code:
  1. package helloworld;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class MySecondApp extends JFrame {
  6.    
  7.     public MySecondApp(){
  8.  
  9.     }
  10.     public static void main(String[] args) {
  11.         new MySecondApp();
  12.     }
  13. }

5. Finally we are going to create a window using the the JFrame methods. I will discuses these more in depth in later tutorials.
Java Code:
  1. package helloworld;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class MySecondApp extends JFrame {
  6.    
  7.     public MySecondApp(){
  8.         setSize(150,60);
  9.         setLocation(20,20);
  10.         JLabel myLabel = new JLabel("Hello World!");
  11.         add(myLabel);
  12.         setVisible(true);
  13.     }
  14.     public static void main(String[] args) {
  15.         new MySecondApp();
  16.     }
  17.  
  18. }

VIDEO TUTORIAL: http://www.extreme-hq.com/other/Vide...loWorldTwo.wmv

And your done

Last edited by John; 08-29-2007 at 02:51 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-18-2007, 11:15 AM
AfTriX AfTriX is offline
Programming God
 
Join Date: Jan 2007
Location: Sri Lanka
Posts: 596
Rep Power: 0
AfTriX is on a distinguished road
Default

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 11:32 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-18-2007, 02:00 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,345
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default

My tutorials assume you are using Eclipse which allow you to compile and run the app within the IDE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-18-2007, 10:39 PM
AfTriX AfTriX is offline
Programming God
 
Join Date: Jan 2007
Location: Sri Lanka
Posts: 596
Rep Power: 0
AfTriX is on a distinguished road
Default

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 10:50 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-19-2007, 01:30 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,345
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default

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
Quote:
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
Quote:
java MyFirstApp
of course those commands assume you have the java directory defined in your system variables

Last edited by John; 01-19-2007 at 01:33 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-19-2007, 11:04 PM
AfTriX AfTriX is offline
Programming God
 
Join Date: Jan 2007
Location: Sri Lanka
Posts: 596
Rep Power: 0
AfTriX is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-17-2007, 03:34 PM
PCsmasher PCsmasher is offline
Newbie
 
Join Date: Jul 2007
Posts: 3
Rep Power: 0
PCsmasher is on a distinguished road
Default 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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-17-2007, 04:12 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,345
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default

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 04:16 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-17-2007, 04:23 PM
PCsmasher PCsmasher is offline
Newbie
 
Join Date: Jul 2007
Posts: 3
Rep Power: 0
PCsmasher is on a distinguished road
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-17-2007, 04:51 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,345
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default

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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java:Tutorial - Making multiple objects work differently John Java Tutorials 0 01-11-2007 02:10 PM
Java:Tutorial - A better looking GUI John Java Tutorials 0 01-11-2007 02:07 PM
Java:Tutorial - Make Your Button Work John Java Tutorials 0 01-11-2007 02:04 PM
Java:Tutorial - Adding Buttons to your Interface John Java Tutorials 0 01-11-2007 02:03 PM
Tutorial: PHP "Hello World" Void PHP Tutorials 4 08-27-2006 11:35 AM


All times are GMT -5. The time now is 04:09 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads