Think you have a car. The car has states such as color, brand. Then you can create a class called car. Your car class define states of the car like you telling about your car to a friend. "My car is a black one".Another thing you need to know, that is called methods. So what is a method ? I'll explain that using above example.
Your car has a speed meter. That is state of your car. Speed meter change according to your speed of driving. When you driving the car's speed meter always need to change. That changing part is perform with methods.If didn't get what i explain in the above example don't worry. When we do these things with coding it will be easy for you.
Now we need to to know how we can express state of your car. When we describe about a state it can be a countable or non countable. Eg. Car's color: black (non countable) and Car's speed meter : 60Kmph (countable) . So when you express state of the car that express types called data types in java. Basically there are two data types in java.
- Primitive Data Types
- Reference/Object Data Types
Primitive Data Types
There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types.
| byte |
|
| short |
|
| int |
|
| long |
|
| float |
|
| double |
|
| boolean |
|
| char |
|
Other type (Reference/Object Data Types) I'll teach you later.
When we use data types how the values of the data type store? When we use "byte" data type we can use that byte a = 100 then what is "a" ? We call that as vvariables.
What is a variables?
Variable is a piece of memory that can contain a data value.When the programme run, Programme run on the main memory.
in here there are two variables ('a' and 'b'). When the programme runs these two variables stored in the main memory. When storing values in the RAM data type comes to the role. When we use integer (int) data type has it's own size that is 32 bits. According to the above example variable 'a' store in the RAM with 32 bit size location. How about the variable 'b' ? I know now you know it. :)
There are four type of variables.
- Non-static fields (Instance Variables)
- Static fields (Class Variables)
- Local variables
- Parameters
I'll explain these thing in later examples.
How do we define a class in java?
In java we can define class using "class" key word. See the bellow example
public class ExampleClass{
}
Oooh ! what is the mean of "public" . In java we called that modifiers.
What is a modifier ?
When we create a class or method usually we use modifiers. So when we use modifiers that means, we are setting access levels. As a example,
Think about previous car example. Do you like every ones in your school use your car ? I know you don't like .. like that you can use access levels for the class or methods. There are for modifiers in java. Those are:
- public
- protected
- default
- private
Modifier Same class Package Subclass Any one Public Protected default Private
When we create a class we need to consider about the class modifiers. These modifiers we can use in method also.
What is a method ?
Method is a collection of statements (codes). This group of code work together for do a task. Think you need to add two numbers that is a one task then you can create a method to add two numbers. When we create a method we need a class. Methods create inside a class.
When we create a programme from where the programme start to run ? :O
The programme start with the "main()" method. Can you remember our "hello world" programme? From where the output of "hello world came ? That is came from the main method. Note that when you work with two or more classes you can have only one main method.
Lets do a one example with Net beans
open the net beans and create a new project and do the following.Then Run the programme. Output will look like this.






No comments:
Post a Comment
Note: Only a member of this blog may post a comment.