Oop
Oop
Contents
Week 1...................................................................................................................................................2
Java....................................................................................................................................................2
Why Use Java?...............................................................................................................................2
Syntax............................................................................................................................................2
The main Method..........................................................................................................................2
Java Variables.................................................................................................................................3
Declaring (Creating) Variables........................................................................................................4
Final Variables................................................................................................................................4
Identifiers.......................................................................................................................................4
Real Life Example of Variables:......................................................................................................5
Java Data Types..............................................................................................................................5
Java Operators...............................................................................................................................6
Java Comparison Operators...........................................................................................................6
Java Logical Operators...................................................................................................................6
Java Strings....................................................................................................................................7
Java Math.......................................................................................................................................8
Java If ... Else......................................................................................................................................8
Java Switch.......................................................................................................................................10
1
Object oriented programming
Week 1
Java
Java is a popular programming language, created in 1995.
It is used for:
Mobile applications (specially Android apps)
Desktop applications
Web applications
Web servers and application servers
Games
Database connection
And much, much more!
Why Use Java?
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
It is one of the most popular programming languages in the world
It has a large demand in the current job market
It is easy to learn and simple to use
It is open-source and free
It is secure, fast and powerful
It has huge community support (tens of millions of developers)
Java is an object-oriented language which gives a clear structure to programs and allows code to be
reused, lowering development costs
As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa.
Syntax
System.out.println("Hello World");
Example explained
Every line of code that runs in Java must be inside a class. And the class name should always start
with an uppercase first letter. In our example, we named the class Main.
2
Object oriented programming
Any code inside the main() method will be executed. Don't worry about the keywords before and
after it. You will get to know them bit by bit while reading this tutorial.
For now, just remember that every Java program has a class name which must match the filename,
and that every program must contain the main() method.
System.out.println()
Inside the main() method, we can use the println() method to print a line of text to the screen:
System.out.println("Hello World");
Note: The curly braces {} marks the beginning and the end of a block of code.
System is a built-in Java class that contains useful members, such as out, which is short for "output".
The println() method, short for "print line", is used to print a value to the screen (or a file).
Don't worry too much about how System, out and println() works. Just know that you need them
together to print stuff to the screen.
You should also note that each code statement must end with a semicolon (;).
Example:
System.out.println("Hello World!");
System.out.println("It is awesome!");
Java Variables
Variables are containers for storing data values.
3
Object oriented programming
String - stores text, such as "Hello". String values are surrounded by double quotes
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
Final Variables
If you don't want others (or yourself) to overwrite existing values, use the final keyword (this will
declare the variable as "final" or "constant", which means unchangeable and read-only):
Identifiers
All Java variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
4
Object oriented programming
// Good
int m = 60;
// Student data
// Print variables
5
Object oriented programming
Java Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
int x = 5;
int y = 3;
Logical operators are used to determine the logic between variables or values:
6
Object oriented programming
Java Strings
Strings are used for storing text.
String Length
A String in Java is actually an object, which contain methods that can perform certain operations on
strings. For example, the length of a string can be found with the length() method:
System.out.println(txt.indexOf("locate")); // Outputs 7
String Concatenation
7
Object oriented programming
Note that we have added an empty text (" ") to create a space between firstName and lastName on
print.
You can also use the concat() method to concatenate two strings:
System.out.println(firstName.concat(lastName));
Java Math
Math.max(x,y)
The Math.max(x,y) method can be used to find the highest value of x and y:
Math.max(5, 10);
Math.min(5, 10);
Math.sqrt(64);
if (doorCode == 1337) {
} else {
This example shows how you can use if..else to find out if a number is positive or negative:
8
Object oriented programming
if (myNum > 0) {
} else {
} else {
int myNum = 5;
if (myNum % 2 == 0) {
} else {
9
Object oriented programming
Java Switch
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
The break and default keywords are optional, and will be described later in this chapter
The example below uses the weekday number to calculate the weekday name:
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
10
Object oriented programming
System.out.println("Sunday");
break;
default:
When Java reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
A break can save a lot of execution time because it "ignores" the execution of all the rest of the
code in the switch block.
11