
Mr. Java
Newbie

Oct 10, 2007, 6:34 AM
Post #1 of 5
(254 views)
Shortcut
|
|
The Basics of Java
|
Can't Post
|
|
Java Basics You will need to download an IDE. NetBeans Eclipse JBuilder JCreator Notepad I use NetBeans, but its up to you what you want to use. I am going to explain this tutorial as if I was using Notepad. Open up you're IDE and enter this code: NOTE: You do NOT learn from copy and pasting. Type it out yourself. public class HelloWorld { public static void main(String args[]) { System.out.println("Hello World!"); } } What it means: System.out.println("Hello World!"); - It's pretty self explanatry. It wants the System to (output) print the line "Hello World". End code with ';'. Now to move onto Strings and Integers. (Strings hold text, Integers hold numbers) public class HelloWorld { public static void main(String args[]) { String STR = "Hello World!"; int INT1 = 5; int INT2 = 3; System.out.println(STR); System.out.println(INT1); System.out.println(INT1+INT2); } } What it means: String STR = "Hello World!"; - To create a string you need to start with 'String'. 'STR' is the name of the String which can be replaced to what you want. "Hello World" is the message that will be displayed. int INT1 = 5; - To create an Integer you have to start with 'int'. Again, 'INT1' is the name of the Integer, which can be replaced to what you want. The number '5' is held in the Integer. Line 10 adds the two Integers together. (INT1+INT2); Now to learn about Else..If. NOTE: '==' means Equal to public class ElseIf { public static void main(String args[]) { String DAY = "Tuesday"; String NOT = "The day is not Tuesday"; if (DAY == "Tuesday"){ System.out.println(DAY); } else{ System.out.println(NOT); } } } What it means: if (DAY == "Tuesday"){ - This line of code is just pretty much saying: If the String 'Day' is/equals to 'Tuesday' then do the code below. else{ - This line of code pretty much is saying: If the DAY isn't 'Tuesday' then do the code below. Now you understand the basics, lets create a simple 'Odd or Even' application . I added some comments in the code. (Lines starting with '//' are comments, these are not debugged with the application. import javax.swing.JOptionPane; public class OddEven { private int input; public OddEven() { // Creates the Input Dialog input = Integer.parseInt(JOptionPane.showInputDialog("Enter a Number:")); } public void calculate() { // If the 2% of the inputted number is equal to 0 then print "Even" if (input % 2 == 0) System.out.println("Even"); else // If not, print "Odd" System.out.println("Odd"); } public static void main(String[] args) { OddEven number = new OddEven(); number.calculate(); } } If you have any questions concerning this tutorial, please reply to this thread. This tutorial was created completely from scratch by ME. Do NOT use this tutorial on other forums WITHOUT my permission. If you ask I will most likely say 'Yes' anyway. So theres no point in just stealing it ;) I will be adding stuff to this. But I am tired right now. END EDIT: Thanks for the sticky ;)
(This post was edited by Mr. Java on Oct 12, 2007, 4:32 AM)
|