My journey to Java 001 — Variables, operations, methods, object and class
Every time I started learning a new programming language, I feel like Its a journey. Today, I decided to share my new journey with you. Let's go.
Java is one of the most powerful languages. If you wanna to be an Android developer, then you have to learn java. If you wanna to be an automation test engineer, then Java is one of the most important languages for you.
Java is an OOP language which means that everything is a class and an object. You can imagine that class is the blueprint of building for a city under construction and the object is a specific real building in the city.
Install Java Development Kit in ubuntu:
You need to get Java JDK (Java development kit) and you can do simply by doing sudo apt-get install default-jdk
, then you have java 10 installed in your system and u can do java --version.
IntelliJ IDEA:
We need an IDE to help us in developing our project. I suggest you to get the latest free version of IntelliJ from here.
1- Variables:
You can find the code in this GitHub repo. Why do we need a variable? Do you remember the first class in algebra, we needed X to store a specific value, then do a lot of operation over this X. Like x=5, y=6, z=x+y, as you can see, we stored 5 in x, 6 in y then we calculate z=x+y, but why we didn't simply do z=5+6?! > the simple answer is, what if we wanna calculate 10+12, here we will only change the value of x and y and the z equation will be valid too.
So the conclusion is, we need the variables to store values and to do operations on those variables.
Java variable example:
First, u need to create a project, then create a class file like this with your first main method. It only prints “Hello World!”.
As you can see here, we declare a new class
with JournyToJava001 name, then defined the main
function with void
type as it returns nothig. Don't worry about public
and static
for now.
Now we are going to make our first method with variables. here is the code,
public static void main(String[] args) {
int int_v = 10; // Integer numbers
double dou_v = 140.13; // floating numbers
char char_v = 'a'; // one character
String str_v = "JourneyToJAva By Islam Taha"; // string
System.out.println(int_v);
System.out.println(dou_v);
System.out.println(char_v);
System.out.println(str_v);
}
as you can see, we defined int, double, char, String
variables.
2- Operations:
There are a fundemntal operation can be done like mathmatic operations+, -, *, /, %
and the logic operation like ||, &, !
.
double addition(double x, double y){
return x+y;
}
double subtraction(double x, double y){
return x-y;
}
double multi(double x, double y){
return x*y;
}
double divde(double x, double y){
return x/y;
}
Here we defined three methods, but wai! Did we mentiont methods before? No. So, Let's discuss what is methods?
3.1- Methods (Instance Methods):
The method is a block of code start with the method type
followed by the method name
then it takes inputs (arguments)
then we write the method body (method logic) and at the end we return
a value. An example makes it more clear
double addition(double x, double y){
return x+y;
}\*
type: double (that is the type of the return result)
name: addition
inputs: x and y (both of them are double)
return: x+y (it returns double variable that is why we defined the method with double type)
*\
but when this code will be executed?! When You call this method, it will be executed. as an exmaple if you do z=obkj.addition(1.1, 2);
then the value of z
is 3.1.
Hint: we need an instance to call the instance methods.
3.2 Methods (Static Methods):
Static methods are the same as instance methods except it has static
word in the declaration and we can call it using the class itself without needs for the object. This method is shared between all instances too cause it is static.
double static addition(double x, double y){
return x+y;}
Inside the static methods, you can call another static method by just doing method_name();
but If you wanna to call an instance method inside a static method, then you need to create an object from the class, then use this object to call the method. I’m gonna make it more clear in the following lines.
Show me an example for static and instance methods:
To answer this, we need to understand that, every java project has a main
method and this main method is static
, so we will use the main method as an example for static methods.
once we run the project, this main method will be called automatically. So only the code inside this main method will be executed. That is meaning, If you wanna to execute the instanceaddition
method, then u need to call it inside the main method which is static
hmm … make sense, but How can I call an instance method inside static method? You only can call a methods from an object :D hmmm and what is the object? The object is an instance from the class. Stop taking and let's call the method in two steps.
//create an object
// class_name object_name = new class_name();
JourneyToJava001 Journey_obj = new JourneyToJava001;
/*
type: class_name
object_name: any_thing
new: deserve a place in the memory to store an object
*/
so to glow all pieces together, we are gonna define a method, create an object and call this method inside the main method
public class JourneyToJava001 {
double addition(double x, double y){
return x+y;
} double static subtraction(double x, double y){
return x-y;
}public static void main(String[] args) {
JourneyToJava001 journey_obj = new JourneyToJava001();
System.out.println(journey_obj.addition(10.1, 10.2)); System.out.println(subtraction(15, 10.2)); }
}
as you can see we defined the addition
method then defined journey_obj
object then called the method by doing journey_obj.addition(10.1, 10.2)
cause it is an instance method. In another hand, we defined a static method subtraction
and we can call it directly inside the main method cause its static
too.
4- Access modifiers:
Java provides us with four different access modifier public, private, protected and default
and we need to choose one every time we declare anything in java as this access modifiers declare the access scope of each member.
4.1 — Private
private variable or method can only be accessed inside the class even if you imported that class or inherited it, you can’t access the private methods.
4.2 — protected
protected variable or method can be accessed in the class and its children only.
4.3 — Default:
If we don’t specify any access modifier then it will take default
by default. The default
access modifier can be accessed anywhere in the package but not outside it even if we imported this package.
4.4 — Public:
public variable or method means we can access it from everywhere without any restrictions.
Don’t forget to check the code here and keep in touch there is more ….