I wrote the following tutorial for my friend and decided I'd post it on here to help out some people .
Some basics that aren't covered in this tutorial:
*applets
*exceptions
*cases
and some other stuff.
public class NameGoesHere {
*pubic class indicates that the class is public.
*NameGoesHere is the name of the class (this is case s. Meaning that capital letters matter.)
*{ indicates the starting of the class block
Note: we need to close the blocks with } at the end of each block.
public static void main (String[] args) {
*This sentence is needed in order to be able to execute the code, we will be
needing this in all of our codes (args stands for arguements).
*{ indicates the starting of the code block
Numbers:
int = integer (does not have a decimal & it does not round the number, it removes the decimal value).
double = an integer with a decimal.
float = a double but with more numbers (e.g: 0.00000000001).
int count = 12;
*This gives count a value of 12, whenever count is mentioned the stored value
of 12 will be used.
*; is required at the end of each statement in order to be executed, think
of it as a period in english.
Display:
System.out.print();
System.out.println();
System.out.print("");
System.out.println("");
*System.out.print command is used to display a certain value or string on
the output.
Example:
System.out.println("Hello");
Output:
Hello
*System.out represents the object while print is the method & ln stands for
new line (prints on a new line).
*The " indicates the string, we need these quotation marks whenever we use
a string.
Packages & imports:
Packages are the set of classes, while imports are used so we can use certain
classes and objects.
Example: import.java.util.Scanner;
This import must be declared in order for the scanner to work in the code.
We can either import single imports of the utilities or all of them using the *.
Example:
Now let's use what we just learnt to make a small program.import java.util.*; import java.util.Scanner;
package package.name.goes.here; public class Example { public static void main(String[] args) { int count = 25; System.out.println(count); } }
This will print out:
25 //which is the value of count.
Comments:
Comments are totally ignored by Java, they are used to help people to understand
your code.
//this is for a single line comment
/*
this comment is usually used when explaining what the program does, or stating
the author names.
**/
Examples:
package package.name.goes.here; public class Example { public static void main(String[] args) { int count = 25; //count is 25 System.out.println(count); // this will print out count } }
Definitions (just the basic ones we're going to be using)
boolean = either true or false
final = constant / can't be changed
char = character (we use single quotes such as 'H')
String = words & sentences, we use double quotes when using strings.
A full set of Java reserved words (which you can look up):
abstract assert break byte case catch class const* //no use yet continue default do double else enum extends boolean char false final finally float for goto* //no use yet if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
The if statement:
if (condition)
statement;
so basically if the statement is true the condition will be executed, otherwise
it will be fully ignored.
Example:
Nothing will be produced in the above code because the condition is false.public class Example { public static void main(String[] args) { int count = 25; if (count == 30) System.out.println("Hello"); } }
We use else so that the program will produce something else if the condition is false.
Example:
This program will print out Hi because the first condition is false.public class Example { public static void main(String[] args) { int count = 25; if (count == 30) System.out.println("Hello"); else System.out.println("Hi"); } }
We don't need braces } { while using if statements unless we have a lot
of conditions
Example:
public class Example { public static void main(String[] args) { int count = 25; if (count == 30) System.out.println("Hello"); else { System.out.println("Hi"); System.out.println("Hi1"); System.out.println("Hi2"); } } }
While using conditions we must use operators:
== means = to
!= means not equal to
>= means greater than or equal to
=< means smaller than or equal to
&& means and
|| means or
Spaces:
White space in java is totally ignored, we use spaces to make our code readable.
This means that:
public class Example { public static void main(String[] args) { int count = 25; if (count == 30) System.out.println("Hello"); else System.out.println("Hi"); } }
is the exact same as:
public class Example { public static void main(String[] args) { int count = 25 if (count == 30) System.out.println("Hello"); else System.out.println("Hi"); }}
Types of errors:
compile-time error: this means that you have wrong syntax or you're trying
to do something that java doesn't allow (example: if you forget to use ;
at the end of a print statement then you'll get a compile-time error).
If a compile time error occurs the class file will not be produced.
logical error: this means that the program does compile and run but the output
is incorrect. Usually happens when you have the wrong formula, etc.
runtime error: forces the program to crash, usually happens when we do things
that cause it to terminate abnormally such as dividing by zero.
We debug and test programs to look for errors.
The while loop:
while (condition) { statement; }
if the condition is true then the statement will be repeated until the condition
turns false.
Example:
This will print out Hello 5 times, after the 5th time the while conditionpublic class Example { public static void main(String[] args) { int count = 0; final int max = 5; //the amount of times we want it to be executed while (count < max) { //while count is smaller than max count++; //adds 1 to count everytime the program is executed System.out.println("Hello"); } } }
will be false and the loop will be ignored.
Nested loops: you can have a while loop inside a while loop, these are called
nested loops.
Escape sequences (Strings):
\n = new line
\" = double quote
\\ = backslash
\b = backspace
\t = tab
\' = single quote
\r = carriage return
Examples of use:
This will print out:public class Example { public static void main(String[] args) { System.out.println("Hello\nThis is an example\n\tto show you\n\"escape sequences.\""); } }
Hello
This is an example
to show you
"escape sequences."
Now let's use what we just learnt to make a program that will read 10 inputs
and prints out the highest number.
Note: I will not be going into detail with the scanner class, formats, math
classes, etc. You'll need to study these on your own.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in); //command we use to be able to read an input from the user
DecimalFormat fmt = new DecimalFormat ("0.###");
int count = 1;
final int max = 10;
double highest = -999999999;
double number;
System.out.print("Enter a value: ");
number = scan.nextDouble(); //gives number a value from the user
while (count < max) {
count++;
if (number > highest)
highest = number;
System.out.print("Enter a value: "); //we add this so we don't get an infinite loop
number = scan.nextDouble();
if (number > highest)
highest = number;
}
System.out.println(); //prints a new line
System.out.println("The highest number was: "+highest);
}
}
We can make a program that has the exact same outcome but with different code, example:
This program has the exact same outcome as the previous one but does notimport java.util.*; import java.text.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner (System.in); DecimalFormat fmt = new DecimalFormat ("0.###"); double number1; double number2; double number3; double number4; double number5; double number6; double number7; double number8; double number9; double number10; double highest = -999999999; System.out.print("Enter a number: "); number1 = scan.nextDouble(); System.out.print("Enter a number: "); number2 = scan.nextDouble(); System.out.print("Enter a number: "); number3 = scan.nextDouble(); System.out.print("Enter a number: "); number4 = scan.nextDouble(); System.out.print("Enter a number: "); number5 = scan.nextDouble(); System.out.print("Enter a number: "); number6 = scan.nextDouble(); System.out.print("Enter a number: "); number7 = scan.nextDouble(); System.out.print("Enter a number: "); number8 = scan.nextDouble(); System.out.print("Enter a number: "); number9 = scan.nextDouble(); System.out.print("Enter a number: "); number10 = scan.nextDouble(); if (number1 > highest) highest = number1; if (number2 > highest) highest = number2; if (number3 > highest) highest = number3; if (number4 > highest) highest = number4; if (number5 > highest) highest = number5; if (number6 > highest) highest = number6; if (number7 > highest) highest = number7; if (number8 > highest) highest = number8; if (number9 > highest) highest = number9; if (number10 > highest) highest = number10; System.out.println("The highest number entered was: "+highest); } }
use any while loops.
Note: you can also use arrays.
A quick note:
System.out.println("25 plus 25 is: "+25+25");
This will print out 2525 because we did not add the ().
System.out.println("25 plus 25 is: "+ (25+25));
Adding, subtracting, etc.:
/ = divide
* = multiply
+ = plus
- = minus
++ = +1 //example count++; this will add 1 to count's value
-- = -1 //example count--; this will subtract one from count's value
+= means this plus this and store the value in the first one // example: count += number; this means count + number = count.
-= opposite of the +=
the for loop:
for (initialization; condition; increment) statement;
Example:
for (int x = 1; x < 5; x++) System.out.println(x);
If you have any questions post below.