Sunday, October 16, 2011

Java Variables

Hi all,
In this post I am going to share my knowledge in java variables. According to my previous post, I mentioned that a java class is same as a factory. In a factory there should be raw materials to produce something. In a java class, variables do the job of raw materials.

In java we have several kinds of variable types.

eg: Integer
Double
Char
Float
Long
Byte
Short
Boolean

Integer
Integers are whole numbers, for example, -35, 0, 2048, ....

this is the way of using integers.


class MyTest{
public static void main(String args[]){

int x = 10;
int y = 20;
int z = x + y;

System.out.println(z);
}
}

copy this code and paste to a notepad. Then compile and run to see the output. You can see the output as 30


In this code int x = 10; means,
x is an integer type of variable. Value of x is equal to 10.

Minimum value of an integer is -2,147,483,648 and maximum value of an integer is +2,147,483,647


byte
class MyTest{
public static void main(String args[]){

byte x = 10;
byte y = 20;
int z = x + y;

System.out.println(z);
}
}
Minimum value of a byte variable is -128 and maximum value of a byte variable is +127


short

class MyTest{
public static void main(String args[]){

short x = 10;
short y = 20;
int z = x + y;

System.out.println(z);
}
}

Minimum value of a short variable is -32,768 and maximum value of a short variable is +32,767



long

class MyTest{
public static void main(String args[]){

long x = 10;
long y = 20;
long z = x + y;

System.out.println(z);
}
}


Minimum value of a long variable is -9,223,372,036,854,775,808

and maximum value of long variable is +9,223,372,036,854,775,807

Double

double and float variables are used to represent decimal values

class MyTest{
public static void main(String args[]){

double x = 10.1d;
double y = 20.3d;
double z = x + y;

System.out.println(z);
}
}


float

class MyTest{
public static void main(String args[]){

float x = 10.1f;
float y = 20.3f;
float z = x + y;

System.out.println(z);
}
}

Boolean
A Boolean variable has two values(true or false).

This is the way of assigning a value to a boolean variable

boolean b = false;



String

class MyTest{
public static void main(String args[]){

String name = "mahesh";
System.out.println(name);
}
}


Data Type Description Size Default Value
boolean true or false 1-bit false
char Unicode Character 16-bit \u0000
byte Signed Integer 8-bit (byte) 0
short Signed Integer 16-bit (short) 0
int Signed Integer 32-bit 0
long Signed Integer 64-bit 0L
float Real number 32-bit 0.0f
double Real number 64-bit 0.0d


Thursday, October 13, 2011

Start Programming with java

I am going to explain how to start programming in java. Lets write a simple program to print something.
for that you need to get a text editor. If you are a windows user, you can use the Notepad.

this is notepad


now you can start to type the program.

first of all we need to create a java class. All the java coding should implement under this class.
This is the way of creating a class,

class MyFirstClass{

}


"class" is a keyword. Keywords should be have correct spelling and correct upper and lowercase. (java is a case sensitive language).

"MyFirstClass" is the name of the class that we are going to create. It can have any name. In good coding practices, we start class name with a capital letter and we should always try to use meaningful names as class names.

A java class is same as a factory.From now I am going to explain java coding while comparing it with a tea factory. It will be a good help for you to understand java easily.

In a tea factory there is a building. All the raw materials and all the machines are inside the building. In a java class, all the attributes and all the methods should be implemented inside the class.

In a factory there is a main machine. And also in a java class, there should be a main method.

What is a method??
A method is like a machine of a factory. A machine is used to produce something. A method is also used to produce something.

For a machine there should be raw materials to produce something (Some machines do not need raw materials). In a method there should be variables to produce something.(some methods do not use variables).

For a machine there should be two doors. One for to give inputs(raw materials) and the other one to get the output. In a method, there should have two doors to give inputs and get outputs.

example for a method,


About return types and additional details of method will be discussed in later. Main method is a different method. It is used to handle all other methods like main machine controls all the other machines in a factory. Instead of that processing also can be done inside the main method.

Main method

Main method should be like this,

public static void main(String args[]){

}



Now lets move to the our topic(Start Programming with java) again.
To get successful output, there should be a main method inside of a class.(for this beginner level). I am going to print some text using a java code. I am going to use main method to do this process rather than using additional methods. Because this is the our first program.

class MyFirstClass{

public static void main(String args[]){
System.out.println("hello.. world");
}

}


This is the java code for our first program. I used main method to print the "hello.. world" sentence.

Write this code on a notepad and save it as a .java file.



I saved the file in D:\java_examples directory.

Now I want to compile the java class. In the compiling process compile errors of the code, are captured.(Eg spelling mistakes).

To compile the class,
first I should open the command prompt.
To open the command prompt go to start and type cmd one the search bar and press enter(I am a windows 7 user)


then you can see the command prompt like this


To compile the java code, we should go to the folder in which the java class is saved. So I should go to "java_examples" folder inside the D partition.
For that type this commands on the command prompt.

If your folder is in the D partition you can type d: and press enter.


After press enter you can see the command prompt like this,

Now we are in the D partition of the computer.
Then we have to go to the folder which is having my java class. For that, type cd folder name and press enter


After press enter, command prompt can be seen like this,


Now we are inside the "java_examples" folder.

Then we should give a command to compile our class.
Type javac file name.java and press enter


After press enter, if your code doesn't have any compile errors it will give this kind of display on the command prompt.


Now we have finished the compilation process. Then we have to run our program. For that we should type java class name and press enter

After pressing enter, you can see the output of our program like this,


Thats all.. :) :) :)