If you're new to java the first thing you need to know is that all java code is writen in classes (a program file) that you create and one of the best way to write java is in the program eclipse. If you don't have it, then we highly suggest that you get it. Click here to download. To start writing you need to create a new project to hold your class. You do this by clicking on New and then clicking on Project then you name it what ever you want. Now you need to create your class and you do this by clicking on the New Java Class and then naming it whatever you want again. Be sure to check the main method box.
It should look like this when you open the class.
Before you start writing code there are a few things you need to know:
1st: when you have an error it will tell you by putting a red x on the line with the error and red error line under the mistake on the line.
Scrolling over the x tells you whats wrong.
2nd: Every line you write needs to end in a ";" unless specified. If specied and it ends in a "{" then you need to have a "}" to end it. You can write code inbetween the curly brackets.
3rd: When naming a variable there are some things you can't have in it. For example if you want to name the variable "Sally's Car" you can't acturally name it "Sally's Car". Your can't use symbols like #,$,',?... etc. and you also can't have spaces. So if you wanted "Sally's Car" as a variable name you could right it in carmal case like "SallysCar" or with an underscore "Sallys_Car".
4th: To output something you System.out.println() or System.out.print(). Stings need to be in "quotes"
5th: Java only runs threw a program once.
In java when you're creating a variable that you want your program to use it. Now in order for your computer to understand it, there are a few things you need to fill out. You need to define its type. Now when I say type I mean how java defines it. Java has 4 basic types.
ints, which are whole numbers
doubles, which are decimals
Strings, which are characters, need to have thier value inside quotes
Booleans, which are yes or no answers, are defined with either true or false values
Then you name the variable whatever you want. After that you define its value base on its type. So ints get numbers, doubles get decimal, strings get characters, and booleans get true or false values. Heres a lay out to better understand it.
(type) (variable name) = (type value);
If statements make the decisions in computer programs. If statements can compare anything.
for comparing, if statements use > greater than, < less then, >= greater than or equal to, <= less than or equal to, and == equal to. Anything you want to be excuted is writen in between the curly brackets. If you want to put an alternate option for the if statement you write else at the end of the statement.
Loops are exactly what there name is. They are a sets of code that repeat what they are implemented to do when there condition is met. There are 2 basic types of loops. For loops and while loops.
In a for loop you create a disposapal variable that only exist between the curly brackets. Then it compares it to another variable. Then if it meets the condition it increments the temporary variable.
While loops you need to do the same thing but not in one line.