Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am going to give some beginners a presentation about writing first program in Java. This program will be about printing "hello world". I'm not sure how can I explain the concepts such as main method (and public and static keywords and the args argument) and class definition. Any ideas how can I explain that so they understand exactly what they mean? I don't have problem to spend 1-2 presentations just in Hello world program but I want them to understand.

share|improve this question

closed as primarily opinion-based by MichaelT, jwenting, GlenH7, Dynamic, amon May 8 at 14:09

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

74  
Congratulations! You have discovered that Java is a crappy teaching language :-) (Not very helpful, I know.) –  Jörg W Mittag Feb 20 '12 at 12:53
32  
@JörgWMittag: I agree with your comment, except I'd remove "teaching" :-) –  6502 Feb 20 '12 at 13:22
6  
I sat down one day to learn Java. The stuff you mention is exactly what stopped me within 30 minutes. Not that I didn't know OOP, but I don't want to be forced to use "all that stuff" for simple things. For something new I later learned Python - how refreshing. –  phkahler Feb 20 '12 at 15:17
19  
class Comment { public static void main(String[] args) { System.out.println("Maybe you shouldn't be teaching Java to people who won't understand what a Hello World program is already"); } } –  Ben Brocka Feb 20 '12 at 16:12
5  
@ThorbjørnRavnAndersen: Scheme, for example. You can teach the entire syntax and semantics of the language in less time it takes you to explain public static void main(args...). When you use Java to teach programming, you lose so much time teaching Java instead of programming that you won't have time left teaching programming. With Scheme, you have almost all of the time for teaching programming. You can probably throw Smalltalk and Haskell in there, too, and still end up with more time for teaching programming, and you will have exposed the students to three very different languages. –  Jörg W Mittag Feb 20 '12 at 17:56
show 10 more comments

16 Answers

For beginners?

Say that this is the magic incantation that the "java" command looks for when starting from the command line, and leave it at that.

The "Hello World" is intended to show the smallest possible program which prints "Hello World" and then you can build on that with what you actually want to teach them.

Compare it to a car. You actually only need to know "turn the key" to make the car start, but all the intricate machinery needing to make it actually happen is not necessary up front when learning to drive.

share|improve this answer
3  
As long as you explain it eventually. As a student, it's frustrating to be told that this is just a thing-that-makes-it-work. Promise your students that you will explain it later. Not that it has to be all at once. –  AlbeyAmakiir Feb 20 '12 at 21:41
1  
When I was teaching Java to high schoolers, I did pretty much exactly this. It wasn't really satisfying--they wanted to know why they were typing all that other stuff--but it did teach them enough to bootstrap learning the rest of the language. Admittedly, now I that I know more languages, I would teach something else (Scheme is a good choice here). –  Tikhon Jelvis Feb 21 '12 at 3:45
1  
I remember being taught something similar when learning C.... (I'm probably going to get this wrong but) the whole void main(int argc, char **argv) thing... was taught at the time that it was magic and to just use it for now... several months later we learnt what it actually did. –  sevenseacat Feb 21 '12 at 5:02
1  
@Karpie: If they taught void main, they probably taught other things badly as well. –  David Thornley Feb 21 '12 at 18:04
show 4 more comments

I'm not sure how can I explain the concepts such as main method (and public and static keywords and the args argument) and class definition

I think you're missing the point of the Hello World program. It's supposed to be the bare minimum coding before you can see something. You teach the students how to output something to the screen, then build from there.

If you start explaining about static methods and class definitions you will just confuse them. Simply explaining it with "The main method is what's run by the program" is enough.

share|improve this answer
show 1 more comment

Just break it down into sub areas like you would a project and explain the details of each "module".

  1. Main-Method - Explain how the program needs to have this so the computer knows what to look for when running. It can't just randomly choose where to start and produce expected results.
  2. Arguments - Well that one doesn't seem too complex. You may want to pass a message on to the program for it to run, and this is how it is done. In Java's case with the main methods, it is a String array so you can tell them it is broken into pieces.
  3. Class - There are many ways to explain this out there. Something like representing the parts of a car is pretty much standard. Explain how a class is like a blueprint for a car and that you can use it to create actual cars (or whatever your example) from the Class and this actual object will have all the data that the blueprint lays out. In a Hello-World application this isn't truly used however, so you may not need to explain this at all.
  4. Static - This one is a little abstract to explain. Make sure this comes after the Class explanation as it is dependent on it. Working off of the previous example of a blueprint, you could explain that all of the cars that come out of the blueprint would have the same data and instead of having that data stored in the car, it is stored in a central location for any of them to access (this can merge into the concept of final as well).
share|improve this answer
show 1 more comment

I'm not sure how can I explain the concepts such as main method (and public and static keywords and the args argument) and class definition

I recommend to avoid explaining those points in the first lesson. Tell your audience that this is some technical stuff you will explain later. Concentrate on the commands which actually do something, like System.out.print("Hello World"); , or whatever you are going to introduce else in the first lesson.

Arguments can be explained very soon after this. But before you start explaining concepts like classes, staticor private/public, you should teach functions, variables, flow of control and all that basic stuff.

share|improve this answer
show 1 more comment

Magic.

On a more serious note, java is a bad language to start beginners off in. It is very heavily object oriented, but that concept is impossible to understand without a good grounding in procedural programming. If you can't understand function calls and follow logic through a main method to sub methods and back, then you aren't going to understand an additional abstraction layer.

For actually explaining hello world, there are two simple things to understand.

  1. sequence
  2. print output

Everything else can be put off for later, and explained in isolation. Treat it like legos, where you add a new piece to the student's toolkit each time.

What the main method is and how methods work isn't important yet. Variables aren't important, since they won't be used. Your students have to understand that programs are logical. If this, then that. They need to see and reason about the code, not guess.

Therefore, demonstrate flow through the code(a debugger is an excellent tool for this). That shows the program is just a machine, moving through motions in a predictable manner.

Print is a magic incantation now, but easily leads to a demonstration of functions and function calls, and how abstracting away details is useful.

share|improve this answer
8  
+1 for bad language for beginners. –  phkahler Feb 20 '12 at 15:22
2  
Java also tends to be very verbose, i would not start a new programmer In Java. Maybe Python or Scheme –  Zachary K Feb 20 '12 at 15:47
2  
Pascal is also not a bad language to teach with, it tends to push decent practice. Personally I would want a language with a REPL to learn in, something where I can try out a line of code here or there just too see what it does. –  Zachary K Feb 20 '12 at 17:14
4  
I'm not sure a REPL is guessing your way to the solution so much as trying out different things to understand how they work. I do a fair bit of teaching and coaching and I have come to find that using the REPL is a great way to learn how things work –  Zachary K Feb 20 '12 at 19:05
1  
@SpencerRathbun, a REPL isn't so much about guessing your way to a solution as experimenting to see what works. Instead of wondering "Hmm, what does this function do?" I can type map(lambda x: x + 2, [1,2,3] and immediately discover. I don't have to fire up Eclipse, create a new project, type in a bunch of code, and then execute it and hope I got the syntax correct. Even the best case of echo map(3,4) > test.py && python test.py provides less interaction and (I'd argue) less ability to learn. –  Wayne Werner Feb 21 '12 at 17:44
show 7 more comments

Usually I find that its easiest to just tell the newbies: "This is a main method. Code you put there will be what is run when you run the program". Just let them memorize the code behind the main method for now.

Then I tell them "System.out... is how you display stuff to the console.

If you still want to explain it:

  • Public: Explain the "public" by saying: "In Java, you can write code that others can use. You may have reason to not let others access the inner workings of your code, be it tidyness, or security. So, we declare stuff 'public', 'private' etc. Since an outside agent (the JVM) wants to run the main method, it must be public.". Note that this is a sort of half-truth (access modifiers aren't exactly for security; and the JVM can access private methods as well); but it should suffice.

  • Void: For 'void', explain what a method is in brief, and say that the main method doesn't need to return anything, as the returned value would go to the OS/JVM who wouldn't know what to do with it

  • String args: Write a short program that uses this. Have it take two values from args and display them as first name, last name or something. Then show the students how it gets inputted on the command line. Note that the new program may confuse them more.

  • Static: No real way to explain this; just say that it enables the method to be called directly (and that more will be explained later)

Try to first show them the program, and then explain it. Maybe if you explained what classes, methods, objects, and data are first, then you could explain this pretty easily. It's always fun to see theory unfold in practice.

share|improve this answer
show 1 more comment

The trick is to explain the focus on explaining the most important points so that your beginner can start to get their head around what is going on - you can always explain the complicated details later.

I would do it something like:

  • Your actual program is really just the one command System.out.println("Hello World!");. Depending on your audience, you can get philosophical at this point and explain that all programming is really just about issuing the right commands in the right order.

  • The program needs to know where to start - and that in Java you have a method called public static void main(...) which is the first method in your program that Java calls. You don't need to explain public static and void at this point - you can say that they are there for a good reason which they will learn later.

  • Java is an object oriented language and everything has to be inside a class (you can leave the specific details about calsses vs. instances for later). I think it's necessary to mention this even if you don't want to go into too many OOP specifics because it helps explain the "structure" of the program.

If your beginner can understand all that (and do give plenty of opportunity to ask questions!) then you are probably off to a good start.

You can then extend the lesson in various different directions:

  • What if we wanted to write "Hello "+name where the name is given on the command line (introduce the concept of concatenating strings, and getting something from the args array)
  • What if we wanted to write hello world 10 times? (introduce looping)
  • What if we wanted to say "Good morning world" before noon (introduce a conditional)
  • What if we wanted to display hello world in a window (introduce a simple GUI window)
share|improve this answer
1  
+1 for speaking about a window. Non-programmers always just want a dialog to pop up. Introducing them to the terminal is always a pretty disappointing experience. –  ZJR Feb 21 '12 at 1:43
1  
@ZJR - very much agree - I have in the past written a JFrame app behind the scenes to hide the "black magic", then given them just the embedded JPanel class to play with. This works very well - since adding buttons and suchlike is easy at that point –  mikera Feb 21 '12 at 1:49
add comment

I'm still a student and my first Java class was not so far.

I remember that all notions were not been explain during the first class with HelloWord programm.

So that how I learn :

Programm :

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

Explanation line by line

public class HelloWord

In Java all the code is implemented in a class. They are stored in a file named as the class : HelloWord.java.

Note : For the first class, don't explain what mean this public word and the fact that there can be ony one public and other private classes. Explain the part of Java encapsulation mechanism after some explanation of objects.

public static void main(String[] args)

Same thing for static, you can't really explain this before talking about objects.

Then you can explain the basics of how to write a method :

returnType functionName(TypeArg1 nameArg1,TypeArg2 nameArg2,..)

Explain that, in order to be runnable, a programm must contains one (and only one) main method with this exact prototype. They will understand later what every words mean.

System.out.println("Hello world!");

Basic statement to print a sequence of character.

In resume don't go to far for the first time, or it might be off-putting for begginers. Don't lose too much time on HelloWord and go straight to OOP basics it's the core of java. HelloWord example can't be really relevant. Btw you can take this example through the differents steps to add informations step by step until, for example you can really make them undertand what is the difference and the use of the three words of System.out.println.

When I started Java I didn't even know what means half the keywords I was using or why I was exactly using them, but I was anyway able to make simple programms. After some trainings, students will quickly learn the details of each keywords if you ask them to do a bunch of progs by themselves.

share|improve this answer
add comment

Start out with Groovy instead of Java.

It has an interpreter, has very Java-like syntax and all the concepts from java port to Groovy. After a while you can introduce other Java features such as main (once they understand public, static and void perhaps). I believe your hello world becomes:

print("hello")

Then you can start adding concepts after that.

Also there is an environment called BlueJ that is INCREDIBLE. For exactly this reason they remove the need for a "Main" by allowing you to execute methods directly. In this way you can create a class like this:

class Test {
    void helloWorld() {
        System.out.println("Hello World");
    }
}

To run this you right-click on the Test class and instantiate it (It creates an instance in a bar below--awesome visualization) then right-click the instance, select the "helloWorld" method and select execute. It prints "Hello World" and you are done. This is pure, actual java--just launched in a different way.

In this way you can avoid many of the concepts at first. It also has a great UML-like interface for defining classes that makes it very easy to keep track of how your classes relate.

Either Groovy, BlueJ or both would be a MUCH better introduction than jumping into raw Java and both ARE java.

share|improve this answer
show 1 more comment

public static void main(String[] args)

  1. public: Scope and name visibility
  2. static: class vs instance
  3. void : types and the special void type
  4. main : functions, entry points, class loading
  5. String : Classes. Special rules for Strings
  6. []: Arrays, more on type declarations

Oh, and the whole thing is wrapped in a class and a package and requires import

There is no way and you ought not to try to explain this first. But the very fact that you have to say "And you'll have to accept that as magic..." is something that will throw lots of learners.

The problem of the teachability of programming languages and their paradigms is something that is shamefully neglected by most people putting together curricula. Either you get "teaching languages" that bear little resemblance to anything in the real world or you get languages chosen for ideological reasons ("industry uses this language" or "this language will lead them towards formal CS").

If you look at "what programming languages are teachable?" you see that BASIC and spreadsheets (data-flow) are, by far, the languages that are most successfully taught to a broad audience. Unfortunately, that doesn't advance anyone's agenda...

Today, it seems to me that the language that is most appealing for teaching is Python. You can avoid it's syntactical and semantic complexities for awhile and it is easy to demonstrate meaningful programs (Web interactions, robotic interfaces, etc.).

Obviously, this won't help you with your talk, but perhaps you can consider it for the future.

share|improve this answer
show 1 more comment

Well, that is very interesting. Why don't you first start with this. Depending on the language, I will just say Java for now.

class hello {

  public static void main (String[] args) {
    System.out.println ("Hello World"); 
    // You can maybe say that that "Hello World" is independent 
    // and is not changed.
  }
}

You can also say that hello worlds are very boring if such is your opinion.

Although, you can use others examples, I doubt mine are even slightly better than anyone else. I am not a teacher so I am not the right person.

share|improve this answer
add comment

This may seem trite, but... There are several ways to get to "Hello, world!" You should choose how to get there based on what features you want to highlight. Want to highlight simplicity? Use a simple text view. From there it's all about the concepts you want to show? Hard go avoid libraries, but inheritance, how you pass variables, exception handling...

share|improve this answer
add comment

Easy--

  • The "main()" method -- tell Java to "begin here" when a program is started from the command line.
  • args[] -- an array of whatever else was entered on the command line.
  • Public -- any other Java program can call this method.
  • static data -- You can only have a single instance of the data in this class.
  • static method -- This method can only access static data of the class.
  • "Class" -- trickier one. Anything short of a book is going to be considered wrong by someone. But hear goes "a class is the group of data definitions and associated methods"
  • System.out -- is an IO stream representing whatever is assigned to "stdout" by default this is the console.
  • println -- a method of System.out that prints a string on followed by a newline.

Java is probably a bad language to teach absolute beginners. Things like "public"/"private" data and methods, and "static" just seem like unnecessary complications unless you know the problems they are trying to solve, but, you cannot have any idea of what these problems are as a beginner.

You might be better of starting with a "super simple" language like groovy where you can ease your way into OO concepts gradually.

share|improve this answer
add comment

Beside such a Hello World program, you have to teach them to compile and run the program too. It is a lot of new stuff, which doesn't make much sense, which can't be combined with other inforamations, and is in the way at the beginning.

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

I once teached a class of newbies, with exactly that problem to pupils, which had, in their majority, never seen an int or an if before.

But my paradigm was, to practice early.

So I explained to them, that there is a dilemma. That there are many things to tell about every little piece of code, and that we need to use that boilerplate code, this kind of entry point in the beginning without understanding, but that they will lose their obscureness over time.

I taught them the most basic concepts first: keywords, datatypes, assignment, declaration, initialisation, conditions, control structures, ... objects, classes, inheritance, ... visibility, API, pretty close to a book, and I could tell them, where in the book they will encounter which part which looks unclear.

So the curious could look forward, to answer their questions right away, and I could produce some trust, that these things will be explained later, and that it isn't just an excuse; that maybe I don't know it myself. :)

I then tried to explain in simple words what most parts are, except the static thing, and told them, that they shouldn't be frustrated, if they don't understand it, if it is too few information. Which parts of the program can be modified right away (class name, param name, text to output) without damaging the program and leading to which result and consequences (name of the class to run, changing the filename).

I guess it worked pretty well and would do it in a similar manner again.

share|improve this answer
add comment
class
    HELLO_WORLD
create
    make
feature
    make
        do
            print ("Hello, world!%N")
        end
end

This is Eiffel as you can see there is no word that is not needed or easy to explain. We define a class called HELLO_WORLD with entry point make, we define the feature make as do some printing (to the screen), the %N is a way to say add the special character [new line].

share|improve this answer
add comment

I think Hello World doesn't tell you anything about java class concept, public,static keywords since there's no way for the students to compare the functionality if you don't use these keywords in your code. Hello World has become a conventional program for any language and actually it tells you nothing about the language but supposed to run the fears away from the beginner. (this is what I feel) So if you can use a little more advanced Hello World code, that would do.For an example, consider the following piece of code.

    public class HelloWorld{
         Public static void main(String[] args){
              HelloWorld wisher= new HelloWorld();
              wisher.sayHelloWorld();

         } 

         public void sayHelloWorld(){
              System.out.println("Hello World");
         }
    }

This may be a much better Hello World program which gives a sense of object orientation as well. or if you are talking to complete novices, probably this might be your second code example and try to explain how the same output appears but the internal structure of the code is a bit more complicated.

Happy teaching... Good luck !!!

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.