My journey to Java 001 — Variables, operations, methods, object and class

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.

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!”.

Create a new java project
The first method, print Hello World!
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);

}

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;
}

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 returna 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)
*\

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;
}

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

//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
*/
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)); }
}

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store