Charas-Project

  • Home
  • Help
  • Search
  • Calendar
  • Login
  • Register
*
Please login or register.

Login with username, password and session length
 

News:

New forum theme up and running!



  • Charas-Project »
  • Off-Topic »
  • Archive »
  • Really Old Stuff »
  • General programming »
  • Basic Java Tutorial
« previous next »
  • Print
Pages: [1]

Author Topic: Basic Java Tutorial  (Read 3137 times)

Offline Tomi

  • *does mannerism*
  • Leader
  • *
  • Posts: 2,000
Basic Java Tutorial
« on: July 17, 2006, 06:06:14 PM »
Basic Java Tutorial
By Tomi

Hello.  Today I am going to teach you the basics of Java programming, which is a powerful yet easy language to learn.  First, so background about Java.

Java is an object-oriented programming language, which means that it uses objects to get things done.  What are objects, you ask?  Well, think of an object as a thing that has certain characteristics (set by its class) and can perform different tasks.  You can send commands to it, and it will do what you tell it to, and send the information back to you.

All you really need to code java is notepad, command prompt, and the JDK (java development kit) which can be found at java.sun.com.  However, it makes coding a lot easier if you find a notepad specially made for coding java, such as JCreator or Microsoft Visual Studio.  

There are some basic things that you need to keep in mind when you code java:
-   Java is cAsE SeNsItiVe.  So write the code just like I have it in here.
-   Never start the name of a variable or object with a capitol letter, symbol, or a number.
-   Save often.

First make a .java file.  Java programs pretty much always start with the following code:
Quote

public class filename {
   public static void main(String [] args) {
   
   CODE GOES IN HERE

}
}

See where it says filename?  Well, you need to insert the name of your file there (not including the .java)  What all of this means is too advanced for this tutorial, so just accept it as it is.  Now for coding the infamous Hello World program.

Quote

public class HelloWorld {
   public static void main(String [] args) {
   
   System.out.println(“Hello monkey! Erm, World!”);

   }
}


Now if you use the compiler (which creates a .class file) built into one of the Java writing programs, you can run this, and command prompt will open and print out the following line:  Hello monkey! Erm, World!

System.out is an object built into java.  Its job is to output what is in the (), called the parameters.  It has 2 main commands you can tell it.
-   println will print what is in the (“”) and will go to the next line.
-   print will print what is in the (“”) and will put the cursor at the end of that line.

So that is how you do a basic print statement.

Variables:
Variables store numbers.  Depending on what type of variable it is, it can hold different types of numbers.

int – holds whole numbers
double – holds numbers with decimals

Before you use variables to store numbers, you must instantiate them.  You do this at the beginning of the program, using the following format:
Quote

int yearsold;
double moneyHeld;

You could store the number 4 in yearsold, but you couldn’t store 3.4.  If you want to store a whole number in a double, put a .0 after it (e.g. 4.0).  Here’s how you can store values in variables.
Quote

yearsold = 4;
moneyHeld = 44.56;

Now, if you want the user to input a value for a variable, first import the following line of ode BEFORE the public class filename.
Quote

import TerminalIO.KeyboardReader;

then create an object with that KeyboardReader class inside where I said the code goes:  
Quote

KeyboardReader read = new KeyboardReader();

then use readInt if you want to input an integer, or readDouble if you want to input a decimal.
Quote

yearsold = read.readInt();
moneyHeld = read.readDouble();

So then whatever they enter in will be stored in the variable.

Then there are basic operations you can use with variables.  Most of them are quite obvious:
+ Add
- Subtract
* Multiply
/ Divide
% Modulus ( The remainder when you divide 2 numbers)
= SET TO! Sets whatever is on the left equal to whatever is on the right.

And don’t try to operate on ints and doubles at the same time. It doesn’t work.

If/Else Statements:
If/Else statements do just what they say; IF something is true, do something.  ELSE, do something else.  They work like this:
Quote

if (yearsold <= 10)
{
   System.out.println(“You are young enough to have a kids meal.”);
}
else
{
System.out.println(“What are you trying to pull?  Getting food that’s for little”);  
System.out.println(“KIDS?  Buy the over-priced food we sell for older people”);
}

That’s the basic format for an if/else statement.  If the condition inside the parenthesis is true, then do the code.  There are a few ways to compare the variables and/or numbers in the conditions.
== (two equal signs) checks whether they are equal to each other ( a single = will NOT work)
< If it’s less than
<= If it’s less than or equal to
> If it’s greater than
>= If it’s greater than or equal to
!= If it’s not equal

And those are the basics of java!  Not too hard, eh?  There’s a lot of things you can do even with this basic stuff.  I may add on later on some more advanced topics (I’m still learning too!) Thanks for reading.

EDIT: One more piece of code for your information
Quote

Java != Javascript
Logged

Offline coasterkrazy

  • June 2005 - September 2008... January 2011?
  • Exemplar
  • *
  • Posts: 1,915
  • Hello...
(No subject)
« Reply #1 on: July 17, 2006, 06:08:08 PM »
Very interesting! I'm actually taking Java this year so that was nice to read before the school year starts. :)
Logged

Offline Gary

  • Is something everyone can enjoy
  • Associate
  • *
  • Posts: 109
(No subject)
« Reply #2 on: July 17, 2006, 06:12:16 PM »
Nice tutorial. Write more before I destroy you XD
Logged

By Me ^.^

Offline Trevlac


  • The Executioner
  • Agent
  • *
  • Posts: 942
(No subject)
« Reply #3 on: July 19, 2006, 01:47:31 AM »
I love you tomie. ^^

*huggles Tomie*

Excellent job. You've inspired me to write a Ruby tutorial.
Logged

Friends: MrMister, Dragonium, Shady Ultima, Tomi, Meiscool, BlackSkullWarlock, Ace of Spades, Smokey_Locs, Robotam, Blue_Strife, Apex

Offline Tomi

  • *does mannerism*
  • Leader
  • *
  • Posts: 2,000
(No subject)
« Reply #4 on: July 19, 2006, 02:00:01 AM »
*gets huggled*

Awesome, now we can learn stuff from each other. :D
Logged

  • Print
Pages: [1]
« previous next »
  • Charas-Project »
  • Off-Topic »
  • Archive »
  • Really Old Stuff »
  • General programming »
  • Basic Java Tutorial
 

  • SMF 2.0.10 | SMF © 2015, Simple Machines
  • XHTML
  • 2O11
  • RSS
  • WAP2
  • Simple Machines Forum