Getting Started with Java (using Textpad) [Published]

duuudeduuude Regular
edited April 2011 in Tech & Games
In this guide I am going to show you how to install Java and compile and run programs. This guide is intended to get you STARTED with Java. Unfortunately, I can't really teach anyone Java because that would be writing a book and I still have a lot to learn anyways. I will post links at the end to help learn. I can also share some things I've written myself if anyone is interested.

This is my first guide so feel free to suggest anything you think will help my writing or the guide improve. So, first things first..

What exactly is Java?
Java is a class based, object oriented programming language developed by Sun Microsystems.
Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. It is intended to let application developers "write once, run anywhere".
For you noobs: Java and JavaScript are NOT the same thing.

Installing Java
First, before you install anything, you should check to see if you already have the JRE (Java Runtime Environment - used to run Java) installed on your computer (most people already do). Go to Control Panel and view your list of installed programs on your system (Windows 7: Click 'Start', 'Control Panel', 'Programs')
If you see 'Java(TM) SE Runtime Environment ...' then you already have the JRE installed and all you need is the JDK. Regardless if you do or don't click here..

Once you open the page look down under 'Java Platform Standard Edition' and click 'Download JRE' choose your platform, etc. Then find where you saved and install. After you install that click 'Download JDK' and do the same. JDK is for Java Development Kit (used to develop Java).

Installing a Compiler
Next you are going to want to download and install a compiler. There are many text editors and IDEs that will compile and run Java, but for the sake of this guide and simplicity we are going to use Textpad. Click here to download Textpad. After that install it and Launch.

Compiling and Running Programs
Once you have Textpad open, Click 'Tools' if you see 'External Tools' .. 'Compile Java' .. 'Run Java App..' etc. then Java has already "integrated" with Textpad If not.. Click 'Configure', 'Preferences', 'Tools', then 'Add' and select 'JDK Commands'.

Next you should click 'File', 'Save As' and save the blank document wherever you want, as 'test.java' (filetype java). I like doing this so I can already have the file in java format and I can already see the colors and spacing when I write my code. After you do that, copy and paste this..
public class test {		//the public class must always match the filename
	public static void main(String[] args)
	{
		System.out.println("ohai! I'm a totsean! =O");
	}
}

Then press Ctrl+1 and if everything has worked out correctly your 'Tool Output' will say "Tool completed successfully" which means your code has compiled. Next, you'll just want to press Ctrl+2 and there ya go, your program should be running. :D

Links
Official tutorials - http://download.oracle.com/javase/tutorial/
My favorite forum to get help at - http://www.javaprogrammingforums.com/
Random guides -http://math.hws.edu/javanotes/ http://www.boloji.com/java/j001.htm http://www.freejavaguide.com/corejava.htm

Comments

  • SlartibartfastSlartibartfast Global Moderator -__-
    edited February 2011
    The best learning IDE for Java is BlueJ .

    It's very simple and at the same time functional. It displays your classes as UML diagram and shows you graphically how they link.
  • edited September 2014
    As an example <a href="http://www.myforskolinfree.com/forskolin-eye-drops">forskolin eye drops on myforskolinfree.com</a> same time or more to <a href="http://www.myforskolinfree.com/coleus-forskohlii-for-weight-loss">myforskolinfree.com coleus forskohlii for weight loss</a> be perfectly acceptable. <a href="http://www.myforskolinfree.com/forskolin-50-mg">best forskolin 50 mg</a> the consumers. <a href="http://www.myforskolinfree.com/forskolin-buy">forskolin buy myforskolinfree.com</a> ingredient with red-colored raspberries <a href="http://www.myforskolinfree.com/forskolin-adenylate-cyclase">myforskolinfree.com forskolin adenylate cyclase</a> Mango supplement has also been shown <a href="http://www.myforskolinfree.com/forskolin-metabolism">forskolin metabolism myforskolinfree.com</a> These are very quick <a href="http://www.myforskolinfree.com/pure-premium-forskolin">myforskolinfree.com get pure premium forskolin</a> been made use <a href="http://www.myforskolinfree.com/synthetic-supplements-forskolin">best synthetic supplements forskolin myforskolinfree.com</a> These holidays bring <a href="http://www.myforskolinfree.com/">best forskolin weight loss myforskolinfree.com</a> before I spend the money .
  • duuudeduuude Regular
    edited February 2011
    The best learning IDE for Java is BlueJ .

    It's very simple and at the same time functional. It displays your classes as UML diagram and shows you graphically how they link.

    I was just thinking about how I've never really used an IDE myself. I always hear about how great Eclipse is but I'll give BlueJ a try since you suggested :)
  • BaconPieBaconPie Regular
    edited February 2011
    I whole heartedly recommend AGAINST IDE's for a beginner programmer. They make the whole experience a mess, teach bad practices (sloppy programming) and worst of all perform "magic". What do I mean by magic? Well "To compile this program click this and that then press f5, MAGIC!".

    IDE's are professional tools for people who know what they are doing. It's like giving a MIG welder to a toddler.

    But, that's just me.

    Here is my guide:
    1) Install the Java Development Kit (JDK) so that you can run the java compiler from a terminal (cmd in windows). This is trivial on linux, just find it in your package manager. I think it's already there on OSX and for Windows, well... urgh, just google it. You will know it's installed when you can run javac (the java compiler).
    $ javac
    

    It should give you some usage guide because you haven't gave it any arguments.

    2) Write your program in a plain text editor. Gedit, TextEdit, Vim, Notepad++. Anything with syntax highlighting really, it will really help (most plain text editors have syntax highlighting unless you're using something like notepad or something. WHY THE FUCK ARE YOU USING NOTEPAD?!

    Here is a sample program to get you started (call it HelloWorld.java):
    public class HelloWorld
    {
      public static void main(String[] args)
      {
        System.out.println("Hello World!");
      }
    }
    

    3) Compile it with the java compiler
    $ javac HelloWorld.java
    

    4) That should give you a HelloWorld.class file. You can then run the java virtual machine and pass it your class name (HelloWorld). It will search for that class in the current directory (HelloWorld.class), build and object from it and then run it:
    $ java HelloWorld
    Hello World!
    

    YAY!

    Summary:
    1) Install JDK
    2) Write a program (a .java source file)
    3) Compile it (javac <source file>)
    4) Run it (java <class name>)

    This is the 'real' way. Using an IDE is for when you're writing something like an Operating System or a mass market game in a team of 30.

    Virtual Machine? WTF?!
    A C source file gets compiled into a pure binary (machine code) that runs on your CPU, if you moved this to your phone/other OS, it probably wouldn't work. Java source code gets compiled into byte code and runs on a virtual machine. You can think of byte code as machine code (binary) for a special CPU, the JVM. Everyone has the same virtual machine installed which means a java program on your computer will also run on your phone, your friends mac, your nans windows and whatever other device has the JVM installed on it.

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

    "class" tells the compiler that the thing between the next two braces is the HelloWorld class.

    "public" means that people outside this class can access the things in it. Sometimes your compiler will complain if it's not there because the JVM can't access it. On my machine it compiled fine without though... weird, just put it there anyway.

    When you invoke the JVM, it looks for a method (a fancy name for a function in Object Oriented Programming), called main. Every method must have a return type and if you like, some arguments and modifiers. For example:
    int fuckingAwesomeMethod()
    {
      System.out.println("FUCK YEAH!");
      return 5;
    }
    
    String giveMeAWordOfSize(int size)
    {
      if (size == 5)
      {
        return "penis";
      }
      else if (size == "3")
      {
        return "lol";
      }
      else
      {
        return "";
      }
    }
    
    void derp()
    {
      System.out.println("derp");
    }
    

    The method fuckingAwesomeMethod() has no arguments, prints "FUCK YEAH!" and then gives back 5.
    The method giveMeAWordOfSize() takes an integer argument and returns a string.
    The method derp() prints derp and has no return type or arguments. A "return;" command is implicitly places at the end of the function by the compiler.

    Anyway, the JVM looks for a method called main and passes it an array of strings. You can call this array of strings whatever you like but "args" is the standard (arguments). This is an array containing each of the arguments you gave your java program:
    $ java MyProgram cat dog sheep
    

    In the above; args[0] is cat, args[1] is dog and args[2] is sheep.

    The main method also have some modifiers;
    public - the function can be accessed from outside of this class (I.e. by the JVM)
    static - everything in the method exists in memory for the lifetime of this program

    The System.out.println() method is a method which is inside the 'out' class (object at run time), which is in the System library. It takes a string as an argument and prints it, followed by a new line. Fun Fact: There are actually two methods called println; one that takes an argument and one that doesn't. The one that doesn't just prints a new line. Also, the method print() prints without a new line character.

    You have to end each line with a semicolon because when the compiler looks at your code, all of the white space is removed. You can even type the whole program on one line! Don't. Why? Source code is read by humans. Once you compile your program the computer doesn't see your source at all. Comment and use lots of white space. It will make someone else's life better :). Use good variable names too.

    Some other tips
    You only need braces for more than one line. So see those if/else statements above with the braces? Well I could of just wrote:
    if (size == 5)
      return "penis";
    else if (size == "3")
      return "lol";
    else
      return "";
    

    Also, note how there is a space between the else and the if. Java has no elseif statement so what I wrote is essentially:
    if (size == 5)
      return "penis";
    else
    {
      if (size == "3")
        return "lol";
      else
        return "";
    }
    

    But remember how if it's only one line then we can ignore the braces? Well the WHOLE if statement is one line :). We write it like this for clarity:
    if (size == 5)
      return "penis";
    else if (size == "3")
      return "lol";
    else
      return "";
    

    Pretty neat, eh?

    Your arguments are Strings. If you want to pass in integers you just parse the string as an integer first:
    public static void main(String[] args)
    {
      int age = args[0];                    // WRONG!
      int age = Integer.parseInt(args[0]);  // CORRECT
    }
    

    Please ask questions in this thread. We can get some pretty sweet knowledge base going on :)

    P.S. Sorry for the thread hijack, I just think that telling people to start with IDE's is the cause for 90% of the bad programmers out there.
  • duuudeduuude Regular
    edited February 2011
    BaconPie wrote: »
    P.S. Sorry for the thread hijack, I just think that telling people to start with IDE's is the cause for 90% of the bad programmers out there.

    No dude that was good stuff. :thumbsup: I should have mentioned it wouldn't be a good idea for someone to start out with an IDE and explained the JVM. If this happens to turn into a long thread I will make sure to add your input to the OP.
  • Darth BeaverDarth Beaver Meine Ehre heißt Treue
    edited February 2011
    duuude, I played with the first part of this last night and appreciate your effort ITT. I will mess with the next parts this evening after dinner. Programming is what got me involved with IT way back 1983 (BASIC). I learned a little FORTRAN and Pascal after that. Then I moved to an island off the coast of NC and got out of it all together. When I got back into IT I entered on the hardware and networking side of things and have forgotten almost everything I ever learned about any kind of programming.

    So it is kind of cool to mess about with this.

    Thanks. :thumbsup:
  • DfgDfg Admin
    edited April 2011
    Taken from: Technophiliacs & Technophiles
    CMS Status:
Sign In or Register to comment.