SMF For Free Support Forum
Signup For Free Forum
July 04, 2008, 04:14:15 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to SMF For Free. The best free SMF Host
 
   Home   Help Search Arcade Gallery Login Register  

Pages: [1]
  Print  
Author Topic: Java for Beginners!  (Read 917 times)
0 Members and 1 Guest are viewing this topic.
|-|Java()|-|
SMF For Free Newbie
*
Offline Offline

Posts: 13


View Profile
« 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:
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:
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:
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:
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:
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:
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:
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:
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:
frame.add(label);

This will add our JLabek 'label' to our JFrame 'frame'. The method 'add' comes from the JFrame heirarchy class.



      
Code:
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:
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)


Logged

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!
lewey52
SMF For Free Newbie
*
Offline Offline

Posts: 3


View Profile
« Reply #1 on: October 10, 2007, 08:41:23 am »

Another way to make a program something like this is:

Code:
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  Wink.  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 » Logged
mr_css
SMF For Free Sr. Member
*
Offline Offline

Posts: 264



View Profile
« 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.
Logged


            WOLFSCAPE FORUMS


Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC
ServerBeach Coupon
Page created in 0.198 seconds with 18 queries.