Advanced Programming
SUT • Spring 2019
Review
First program in java
Variables
Methods
Conditions
Loops
Java is
Simple
object oriented
Robust
And popular
Java is platform independent.
Write Once, Run Anywhere!
Create a file named First.java
Java class files have .java extension
Note to naming convention
Copy this lines to the file
Note: File name and class name should be the same.
public class First{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
behnam@behnam-UX430UNR:~$ mkdir test
behnam@behnam-UX430UNR:~$ cd test/
behnam@behnam-UX430UNR:~/test$ cat > First.java
public class First{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
behnam@behnam-UX430UNR:~/test$ ls -lah
total 12K
drwxr-xr-x 2 behnam behnam 4.0K فوریه 4 10:55 .
drwxr-xr-x 22 behnam behnam 4.0K فوریه 4 10:55 ..
-rw-r--r-- 1 behnam behnam 116 فوریه 4 10:56 First.java
behnam@behnam-UX430UNR:~/test$ javac First.java
behnam@behnam-UX430UNR:~/test$ java First
Hello World!
behnam@behnam-UX430UNR:~/test$
A simple java program is a file
The file contains one class
The class name equal to the file name
The names are case sensitive
The class contains a main method
When we run the program, the main method is executed
Primitive type | Size | Min | Max | Wrapper Type |
---|---|---|---|---|
boolean | - | - | - | Boolean |
char | 16 bits | Unicode 0 | Unicode 2^16-1 | Character |
byte | 8 bits | -128 | 127 | Byte |
short | 16 bits | -2^15 | 2^15-1 | Short |
int | 32 bits | -2^31 | 2^31-1 | Integer |
long | 64 bits | -2^63 | 2^63-1 | Long |
float | 32 bits | IEEE754 | IEEE754 | Float |
double | 64 bits | IEEE754 | IEEE754 | Double |
void | - | - | - | Void |
Operator | Java | Sample | meaning |
---|---|---|---|
= | == | x == y | x is equal to y |
≠ | != | x != y | x is not equal to y |
> | > | x > y | x is greater than y |
< | < | x < y | x is less than y |
≥ | >= | x >= y | x is greater than or equal to y |
≤ | <= | x <= y | x is less than equal y |
public class SimpleProgram{
public static void main(String[] args){
int a;
a = 12;
a += 2;
int b;
b = 4;
b++;
b = a*b;
System.out.println(b);
}
}
method
inputs
output
public class Example{
static double add(double a, double b){
double result = a + b;
return result;
}
public static void main(String args[]){
double x = 3;
double y = 4;
double add = add(x,y);
System.out.println(add);
}
}
Local variables
Java passes the parameters by value
public class ParameterPassing{}
public static void main(String[] args) {
double x = 3;
double y = 4;
double add = add(x,y);
System.out.println(add);
System.out.println(x);
}
static double add(double a, double b){
a = a + b;
return a;
}
}
public class Main{
public static void main(String[] args){
int x = 2;
int y = 3;
if(x>y){
System.out.println("X is greater than Y");
} else if(x==y){
System.out.println("X is equal to Y");
} else {
System.out.println("Y is greater than X");
}
boolean condition = x>y;
if(condition){
System.out.println("X is greater than Y");
}else{
System.out.println("Y >= X");
}
}
}
public class Loop{
public static void main(String[] args){
int counter = 0;
while(counter < 10){
counter++;
System.out.println(counter);
}
counter = 0;
do{
counter++;
System.out.println(counter);
}while(counter < 10);
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
for (X; Y; Z) {
body();
}
X;
while(Y){
body();
Z;
}
goto is a reserved word in java
But forbidden!