Java Course
Learn JAVA here!
Requirements:
Eclipse IDE
Java Development Kit (JDK)
Java Runtime Envirentment (JRE)
Related pages:
1. Course
Table of Contents
|
1. Basic syntaxes
public static void main(String[] args){ //Content of your code }
Also, here are some few things that you need to know before starting using JAVA:
1.1 Comments
// This a comment
1.2 Multiligne comments
/* This is the start of a multiline comment A comment here. Another comment. */ This is the end of a multiline comment
1.3 Exemple:
public static void main(String[] args){ //A simple comment /* This is the start of a multiline comment A comment here. Another comment. */ This is the end of a multiline comment }
Warning! After (almost) every sentence use a ; !
2 Variables and mathematic operators
2.1 Variables
Type | Value |
---|---|
byte | -128 to +127 |
short | -32.768 to +32.767 |
int | -2x10$^9$ to +2x10$^9$ |
long | -9x10$^1$$^8$ to +9x10$^1$$^8$ |
float | floating coma |
char | Unicode charchter between '' (e.g. 'A') |
boolean | true/false |
String | Text between "" (e.g. "Hi") |
2.1.1 Variable syntaxes
Always follow this syntax: 1st. type of variable, 2nd. name of the variable, 3rd. value
Exemple:
int abc = 12;
String exemple:
String abc = "Hi";
Warning! Always write String and not string!
2.1.2 Variable casting
To cast int to float:
int i = 123; float f = (float)i;
To cast int to double:
int i = 123; double d = (double)i;
To cast int to String:
int i = 123; String s = new String(); s = s.ValueOf(i);
2.2 Math Operators
Opertator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
3. Print something (e.g. a text, a variable…)
Syntax:
int in1 = 10; System.out.println(in1 + "is the value of in1")
4. Scanners
Syntax:
Scanner sc1 = new Scanner(System.in);
Import the Scanner class:
import java.util.scanner;
Import all the classes of the util package:
import java.util.*;
4.1 Get what the user typed
Syntax:
Scanner sc1 = new Scanner(System.in); System.out.println("Please enter your age here:"); String st1 = sc.nextLine(); System.out.println("Your age is" + st1)
So the syntax for line 4 is: next<Variable type> (THIS DIDN'T WORKS WITH CHAR)
For char type:
System.out.println("Please type a character:"); Scanner sc1 = new Scanner(System.in); String st1 = sc.nextLine(); char ch1 = st1.CharAt(0); System.out.println("You typed" + ch1); System.out.println("TERMINATED");
e.g. of using a scanner:
public static void main(String[] args) { Scanner sc1 = new Scanner(System.in); System.out.println("Please enter your age here:"); int st1 = sc1.nextInt(); System.out.println("Your age is: " + st1 + " years.");} }
5. Conditions
5.1. if else
5.1.1 Operators
Operator | Description |
---|---|
== | test a tie |
!= | test an inequality |
< | strictly inferior |
<= | inferior or tie |
> | strictly superior |
&& | specify a condition(and) |
|| | specify a condition(or) |
5.1. if else (back)
Syntax:
if (//condition) { //if the condition is right do: } else { //if the condition is false do: }
e.g.:
int in1 = 10; if (in1 < 0) System.out.println("The number is superior as 0!"); else System.out.println("The number is inferior as 0");
See also:
int in2 = 0; if (in2 < 0) System.out.println("in2 is negative."); else if (in2 > 0) System.out.println("in2 is positive."); else System.out.println("in2 is 0.");
So the result is gonna be :
5.2. Multiple conditions
Syntax:
int in1 = 58; if (in1 < 100 && in1 > 50) System.out.println("in1's value is between 50 and 100."); else System.out.println("in1's value isn't between 50 and 100.");
So the result is gonna be:
Comments
Credits:
Comment here if you need help!
naschemaa ;-). Dura lex sed lex. The TRF.
Post preview:
Close preview