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 Say Hello to Me and Say Hello to Java

gui cli

  • Please log in to reply
6 replies to this topic

Poll: Java Tutorial Survey (4 member(s) have cast votes)

Was This A Useful Java Tutorial?

  1. Yes (3 votes [75.00%])

    Percentage of vote: 75.00%

  2. No (1 votes [25.00%])

    Percentage of vote: 25.00%

  3. See My Comments Below (0 votes [0.00%])

    Percentage of vote: 0.00%

Vote Guests cannot vote

#1 madushan1000

madushan1000

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Location:Sri Lanka
  • Programming Language:Java, Python
  • Learning:Java, C#, PHP, Python, JavaScript, NXT-G

Posted 20 August 2012 - 10:35 AM

Hello I'm Madushan Nishantha from SriLanka and I'm going to start new java tutorial set,
Think you will like it

I'm going to cover almost all the Java basics like Basic language concepts,I/O,File handling,Statements and so on.

As a starters I'm going to tell you how to write a simple "Hello World" program using both Command line and GUI.
Remember It's required to install a version of JDK:http://www.oracle.com/technetwork/java/javase/downloads/ on your computer and the path javadir/bin added to PATH environment variable to follow this tutorial.I'll cover this part in my next tutorial.

Let's start,

Command Line

First open a text editor (If you are using Windows you have in built Notepad but I prefer Notepad++:http://notepad-plus-plus.org.Create a file called helloWorld.java and put this code in.

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

Then open up a command line and go to the folder helloWorld.java file located (I'm using c:/java).
Type

javac helloWorld.java

This will create a file called helloWorld.class in the same folder.Then type,

java helloWorld

This will print the following on command line,

Hello World

Now I'm going to explain the code,
The first line of the code is

public class helloWorld

"public" is the keyword that define the class to publicly accessible,I'll explain more about this further.
"class helloWorld" is defining of the class.HelloWorld is the class name,Note that this must be the same as file name!.
The first "{}" marks the block,area that Owns by helloWorld class.

The next line is,

public static void main(String args[])

This is definition of main() method,Every java program must have the main() method to execute!!

"public" is same as before.
"static" this keyword define the method as static.I'll explain this further as well.
"void" is the return type,which means what is the type the method returning the result.This could be int,String,etc..void means there is no return type.

Then,

System.out.println("Hello World");

This is the line which prints "Hello World" on command line.
"System.out" is the class used to stranded output in Java.
println() is the method which prints the "Hello World",It prints the part in the "()" in a new line.If you don't want a new line just use print()

That's it,
So try to compile and run.Here is my snapshot.
helloWorldcli_1.png


GUI

Create a new file called helloWorldGUI.java and put this code in.

import javax.swing.*;
class helloWorldGUI
{
     public static void main(String args[])
     {
          JOptionPane.showMessageDialog(null,"Hello World");
     }
}

Then using command line go to the folder and,

javac helloWorldGUI.java

java helloWorldGUI

This will pop up a message box saying "Hello World".Press ok to end the program.

What's new in this code?,

import javax.swing.*;

"import" keyword is used to include external packages or classes in our java program.
"javax.swing" is the JDK's inbuilt GUI toolkit.

What is the meaning of "javax.swing.*"
The "*" in the end means import all the classes inside the package "javax.swing"

JOptionPane.showMessageDialog(null,"Hello World");

"JOptionPane" is the class which includes the tools like message boxes or input boxes.
"showMessageDialog()" is the method which pops up a message box.The parameter "null" means the message box should not be in a parent window.We'll come to this later.And the next parameter says what is the message.In this case it's "Hello World".

Compile and run.Let's see how it goes!
helloWorldgui_1.png

Here are the both command line version and GUI version sources.
Attached File  helloWorld.java   199bytes   226 downloads
Attached File  helloWorldGUI.java   230bytes   243 downloads

That's it,
See you with the next tutorial.
Any questions and comments are welcome

Edited by madushan1000, 20 August 2012 - 09:34 PM.
added poll

  • 3

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 20 August 2012 - 12:55 PM

A nice start, but make sure you indent your code properly so others also understand the imoprtance of it.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 20 August 2012 - 01:07 PM

Great first tutorial, madushan1000!
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.


#4 Zer033x

Zer033x

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 50 posts
  • Programming Language:C++, C#, Lua
  • Learning:C++, C#, JavaScript, Lua, Others

Posted 20 August 2012 - 07:38 PM

Nice tutorial, but here are some suggestions. I'd suggest if you're going to do an absolute beginner tutorial to do an absolute beginner tutorial. First, the command line stuff and javac stuff will confuse the people this tutorial is aimed at to begin with so you should explain how to set all that up or suggest ways around it like an IDE. Explaining every word is nice and all, but I think it would be more helpful to a beginner to learn the high level of what things mean first like what a class is, what a package is, where the heck are these packages coming from that you're importing, and maybe even a list of some other useful standard classes that can be imported and used.
  • 1

#5 madushan1000

madushan1000

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Location:Sri Lanka
  • Programming Language:Java, Python
  • Learning:Java, C#, PHP, Python, JavaScript, NXT-G

Posted 20 August 2012 - 09:23 PM

A nice start, but make sure you indent your code properly so others also understand the imoprtance of it.

Thanks..I'll do that :D

Great first tutorial, madushan1000!

Thanks.

Nice tutorial, but here are some suggestions. I'd suggest if you're going to do an absolute beginner tutorial to do an absolute beginner tutorial. First, the command line stuff and javac stuff will confuse the people this tutorial is aimed at to begin with so you should explain how to set all that up or suggest ways around it like an IDE. Explaining every word is nice and all, but I think it would be more helpful to a beginner to learn the high level of what things mean first like what a class is, what a package is, where the heck are these packages coming from that you're importing, and maybe even a list of some other useful standard classes that can be imported and used.

Yeah yeah I'm going to Start from the beginning from the next tutorial on.This is just a worm up stat for who know how to setup java and actually have no clear idea of compiling and running a java program.Thank you for the suggestions
  • 1

#6 TALucas

TALucas

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 118 posts
  • Programming Language:Java, (Visual) Basic, Visual Basic .NET, Transact-SQL
  • Learning:C++, PHP, ActionScript

Posted 12 September 2012 - 10:09 AM

madushan1000


I liked the idea of starting with the command line. This will allow the beginning programmer to learn the nuts & bolts of how a program compiles and executes on very simple code.

Thanks.
  • 0
Your thoughts are the architects of your destiny.

#7 maher

maher

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Location:India
  • Programming Language:C, C++, (Visual) Basic
  • Learning:Java, PHP, JavaScript

Posted 05 October 2012 - 06:21 PM

good. i can use this method of displaying result in my coming practical test :D. thanks madushan1000
  • 0





Also tagged with one or more of these keywords: gui, cli

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