Java Programming Language
How to write a Java program
Open Eclipse
File> New> Java Project>
Write Project Name
Right Click on src>New>class
Write a name for the class
Write the program
Run
Variables
Named location that stores a value of one particular type.
Form:
TYPE NAME;
Example:
String foo;
Types
Kinds of values that can be stored and manipulated.
boolean: Truth value (true or false).
int: Integer (0, 1, -47).
double: Real number (3.14, 1.0, -2.1).
String: Text (hello, example).
String a = a;
String b = letter b; a = letter a;
String c = a + and + b;
\n new line, \t tab
Operators
Symbols that perform simple computations
Assignment: =
Addition: +
Subtraction: -
Multiplication: *
Division: /
Order of Operations
Follows standard math rules:
1. Parentheses
2. Multiplication and division
3. Addition and subtraction
double x = 3 / 2 + 1; // x = 2.0
double y = 3 / (2 + 1); // y = 1.0
First Program
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world.");
System.out.println("Line Number2");
}
}
Second Program
public class Hello2
{
public static void main(String[] arguments)
{
System.out.println("Hello world.");
System.out.println("Line number 2");
}
}
class Hello3
{
public static void main(String[] arguments)
{
String foo = "Al Qassim University";
System.out.println(foo);
foo = "Something else";
System.out.println(foo);
}
}
public class DoMath
{
public static void main(String[] arguments)
{
double score = 1.0 + 2.0 * 3.0;
System.out.println(score);
score = score / 2.0;
System.out.println(score);
}
}
public class DoMath2
{
public static void main(String[] args)
{
double score = 1+2*3;
System.out.println(score);
double copy=score;
System.out.println(copy);
System.out.println(score);
}
}
public class divide1
{
public static void main(String[] args)
{
double a = 5.0/2.0; // a = 2.5
System.out.println(a);
int b = 4/2; // b = 2
System.out.println(b);
int c = 5/2; // c = 2
System.out.println(c);
Double d=5/2; // d=2.0
System.out.println(d);
}