Advertise Here

Author Topic: Java for Beginners!  (Read 10025 times)

0 Members and 1 Guest are viewing this topic.

Offline |-|Java()|-|

  • SMF For Free Newbie
  • *
  • Posts: 12
    • View Profile

  • Total Badges: 11
    Badges: (View All)
    Topic Starter Combination Level 2 Level 1 10 Posts
Java for Beginners!
« on: July 18, 2007, 10:32:23 pm »
Hi, welcome to my beginner Java tutorial. I, like you, am learning Java. If you really are eager to learning Java, but think you are not able, think again! You would be surprised how far a strong will for learning can take you. Let us begin with a simple exercise:


--------------------------------------------------------------------------------

Exercise 1: Creating a simple 'HelloJava' program

Hopefully you have your environmental variables set up. Because I assume you do, this will be our compiler using the Command Prompt:



Code: [Select]
javac -cp . *.java
The javac will call upon the Classpath environmental variable to redirect it to the bin folder of the Java Development Kit (JDK). The '-cp' tells the compiler to be ready to change directories depending on where the Java files are, and where the outputed class files should be placed. The reason why we use the 'Change Directory' tag is because it helps the compiler better understand where the Java files should be compiled to and where they are themselves. Using the default 'javac *.java' at some points can cause issues. This tag also helps you in the future when you want to use simpler methods of cross-compiling.

Now that our compiler is set up, lets make our first Java file. Open up Notepad (unless you have an IDE [Integrated Development Environment] set up, there is a link for both Eclipse and Visual J# at the bottom of this Exercise) and add this code:



Code: [Select]
public class HelloJava
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Hello, Java!");
JLabel label = new JLabel("Hello, Java!");
frame.add(label);
frame.setSize(300, 300);
frame.setVisible(true);
}
}

Now save this code as 'HelloJava.java'. After you are done with that, lets use our compiler command prompt code to compile our new Java file:



Code: [Select]
javac -cp . HelloJava.java
If you wish to, you could use the wildcard '*' to cover all the Java files in the directory. This will be needed in future exercises. Now, you should have a file called 'HelloJava.class'. Lets use some new command prompt code to run our class file:



Code: [Select]
java -cp . HelloJava
Note that we do not use wildcards (will not work with the Java interpreter) or a '.java' extension while calling the interpreter. We do not do these things because they will cause an error whilst running the Java Runtime Environment (JRE). After you have started your application, you should see this:



Congratulations, you have just created your first Java program! Lets relook over the code that we have just used.



Code: [Select]
public class HelloJava
This will create a new public class file called 'HelloJava'. Instead of creating an 'heir' class file (used with just the 'class' without 'public', this will be explained more in future exercises) you are creating a Java file that represents its name. That is why it is essential to have the Java file named 'HelloJava' and nothing else (it does not ignore capitals.) You should also notice the brackets ('{', '}') that are both after this lin at the end of the class file. These brackets start the parsing of the code in that a class or other code that use brackets (integers, voids, booleans, etc....) The final bracket will also end the parsing of the code. If the final bracket is not implemented, you most likely will have a parsing or public errors.



   
Code: [Select]
public static void main(String[] args)
This is a public static (unchangeable unless the creator changes it) void that the interpreter auto-maticly looks for. Without one of these, it is impossible to run a class file directly. However, you can still run it be calling it from another file (hence comes the static [static will be used for every void and boolean that is called from another class file].) Also, you may notice the 'String[] args'. This will String the basic argument (args) to the main void. This will implement basic System code inside of the application (used in 'System.out.println' and other System code, depending on the arugement implemented.) You will be using many Strings while coding in Java. I will teach you more about strings in the future exercises.



      
Code: [Select]
JFrame frame = new JFrame("Hello, Java!");
This code will start a new JFrame class and input it with the text 'Hello, Java!'. This will serve as the title of our Java application. The JFrame is also out Graphical User Interface (GUI) whilst coding basic Java applications. It is very important while making a visual Java program. Also note the semicolon (';') at the end of this statement. This tells the interpreter that this is the end of the line. Without a semicolon on most statements, a Java error will occur during the compiling.



      
Code: [Select]
JLabel label = new JLabel("Hello, Java!");
This will call the Java heirarchy JLabel and set it as a new class. It will then input the quoted text 'Hello, Java!' inside of the public JLabel and will set it equal to that String. JLabel then will create a new Label called 'label' with the value of its String (in thise case, 'Hello, Java!'.)



      
Code: [Select]
frame.add(label);
This will add our JLabek 'label' to our JFrame 'frame'. The method 'add' comes from the JFrame heirarchy class.



      
Code: [Select]
frame.setSize(300, 300);
The method 'setSize' is also from the JFrame class. In this case, it is setting our JFrame 'frame''s size to three hundred by three hundred pixels, a good size for the purpose of our application.



      
Code: [Select]
frame.setVisible(true);
Of course, you could have guess that the 'setVisible' method also comes from the JFrame class. In this case, it is setting our JFrame 'frame''s visiblity to 'true'. We could always change the value of the boolean to 'false' instead, but then we would not be able to see our GUI  .

I hope you enjoyed this Exercise. I will, in the future, hopefully add more to teach you a little more about basic Java. Thank you for reading.


-----------------------------------------------------------------------------------------------------

Here are some useful Java resources:

Eclipse Open-Source-IDE
Visual J# Express IDE (for J# development)


I am a moparScape expert so if you need anyhelp!
Pm me here or on moparscape and I'll help!!
If you need more help add me on my msn moparsc4p3@hotmail.com

Thanks!

Offline lewey52

  • SMF For Free Newbie
  • *
  • Posts: 3
    • View Profile

  • Total Badges: 9
    Badges: (View All)
    Topic Starter Combination Level 2 Level 1 First Post
Re: Java for Beginners!
« Reply #1 on: October 10, 2007, 08:41:23 am »
Another way to make a program something like this is:

Code: [Select]
public class HelloJava         //States the class

{                                    //Starts the class
       
        public static void main(String[] args)  // main method

       {
                 System.out.println("Hello Java!");  //output statement
        }                                                       //ends main method
}                                                              // ends class

That is just a very simple way of making a java program that says "Hello Java!"  Very nice tutorial by the way  ;).  It's some good stuff!
This is just a very basic way of outputting what you want to be said. 

By the way, which compiler do you use?  I use jGRASP
« Last Edit: October 10, 2007, 08:44:14 am by lewey52 »

Offline mr_css

  • SMF For Free Sr. Member
  • *
  • Posts: 252
    • View Profile

  • Total Badges: 15
    Badges: (View All)
    Topic Starter Combination Level 3 Level 2 Level 1
Re: Java for Beginners!
« Reply #2 on: October 17, 2007, 08:34:31 am »
I don't get you about "assuming" enviromental variables, where and what would we enter to begin? i'm sorry but this is not a good way to start, I'm new to java coding. So you'll have to be specific.

            WOLFSCAPE FORUMS

Offline nondier

  • SMF For Free Member
  • *
  • Posts: 38
    • View Profile
    • Synysterscape

  • Total Badges: 12
    Badges: (View All)
    Windows User Topic Starter Combination Level 2 Level 1
Re: Java for Beginners!
« Reply #3 on: February 04, 2009, 07:44:58 pm »
this is like the most used beginners java tutorial
<embed src="http://www.xatech.com/web_gear/chat/chat.swf" quality="high" bgcolor="#000000" width="540" height="405" name="chat" FlashVars="id=40672730&gn=Synysterscape" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://xat.com/update_flash.s

 

Related Topics

  Subject / Started by Replies Last post
4 Replies
6583 Views
Last post July 11, 2007, 12:40:04 pm
by doggiedoo86
3 Replies
1240 Views
Last post October 16, 2007, 11:56:33 pm
by guest1608
1 Replies
5180 Views
Last post July 02, 2008, 01:16:07 pm
by Jessie
10 Replies
12881 Views
Last post January 09, 2010, 01:57:39 pm
by hell hound
13 Replies
5567 Views
Last post January 27, 2009, 06:12:08 pm
by simply sibyl