Header Ads

Java SE Programming 2 - Variables, Methods

Last post we've started learning Java, Let's see what we have today.


Variables 

There are two stages in variable. 

1. Declaring the variable.
       int x ;  
       In here int is called "Data type"
       x is "variable name"

2. Initialize the variable. 

        x = 10; 

 For get the idea look at the below image. 




output 














By seeing this output you'll get to know how these variables are work in the code. 






Methods 


There also have two stages in methods. 

1. Declaring the method.

2. Calling the method. 




Look at the example. 





There are 4 methods (add, sub, mul, div) inside the class Test. 

Inside those methods some variables have been declared. (x and y) 

We call those variables "Method Local Variables"
because those variables are only accessible by it's method.  













The same code we can write as follows. 



Did you see the difference. In this code we got those variables outside to the methods but inside the class. These variables are now called "Global Variables"

And this global variables can be accessed by all methods inside the class Test.  















Representing the real world entity from Java code. 


Real world entity has two things. 
    1. Set of Priorities. 
    2. Set of Behaviors. 


Entity -----------------------> Class
Properties of entity --------> Variables 
Behaviors of entity --------> Methods

For example we'll represent a person entity from a Java code.

The followings are the properties of the person entity. 
  • Name
  • Age
  • Weight
The followings are the behaviors of the person entity. 

  • Run
  • Jump




The reference variable calls a person type memory address. 












There are two types of variables based on the data type. 

1. primitive type -> calls a numeric (primitive) values.
2. Reference type -> calls a memory address. (references)



Let's get know more about those variables and methods in next post. Keep in touch. 


No comments

Powered by Blogger.