Nov 16, 2014

Data type, class, method in java

Hello this is the second tutorial with the freethemalloc. In this tutorial I'm going to teach you basic thing about the java. As i mentioned before i need to teach you what is a class in java. Basically a class is a template. I'll explain that with simple example.
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
  • Byte data type is an 8-bit signed two's complement integer.
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (inclusive)(2^7 -1)
  • Default value is 0
  • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.
  • Example: byte a = 100 , byte b = -50
short
  • Short data type is a 16-bit signed two's complement integer.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (inclusive) (2^15 -1)
  • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int
  • Default value is 0.
  • Example: short s = 10000, short r = -20000
int
  • Int data type is a 32-bit signed two's complement integer.
  • Minimum value is - 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647(inclusive).(2^31 -1)
  • Int is generally used as the default data type for integral values unless there is a concern about memory.
  • The default value is 0.
  • Example: int a = 100000, int b = -200000
long
  • Long data type is a 64-bit signed two's complement integer.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
  • This type is used when a wider range than int is needed.
  • Default value is 0L.
  • Example: long a = 100000L, int b = -200000L
float
  • Float data type is a single-precision 32-bit IEEE 754 floating point.
  • Float is mainly used to save memory in large arrays of floating point numbers.
  • Default value is 0.0f.
  • Float data type is never used for precise values such as currency.
  • Example: float f1 = 234.5f
double
  • double data type is a double-precision 64-bit IEEE 754 floating point.
  • This data type is generally used as the default data type for decimal values, generally the default choice.
  • Double data type should never be used for precise values such as currency.
  • Default value is 0.0d.
  • Example: double d1 = 123.4
boolean
  • boolean data type represents one bit of information.
  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • Default value is false.
  • Example: boolean one = true
char
  • char data type is a single 16-bit Unicode character.
  • Minimum value is '\u0000' (or 0).
  • Maximum value is '\uffff' (or 65,535 inclusive).
  • Char data type is used to store any character.
  • Example: char letter  ='A'

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.


OK see you in next post ! :)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.