Tuesday December 6, 2011 16:46

Lesson no. 4 (Variables and data types in j2me)

Posted by pradeep as J2ME (Java)

Hi all,
We all knw how to create midlet and see their outputs. Now lets learn some syntax or other useful part of programming. Today we will learn what the variable means in java? It is widely used term and a logic throughout the programming.
* What is variable?
A variable is a container that stores a meaningful value that can be used throughout a program.
Dont get confuse. For example:
There are 4 boys Ram, shyam and hari and gopal. All four holds some data. They all store some number or any contents then they can be taken as variable. They just hold some important informations that can be used throughout the program.Variables store this information in a computer’s memory and the value of a variable can change all through out a program.

How to declare variables?
Well, One variable in your program can store numeric data while another variable can store text data which depends upon coders. While declaring variables programmer has to declare every variables with java data types. You need to use some keywords as data types. They are mentioned below:
Java Data Types

* Byte :
The byte data type is an 8-bit integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). It helps in storing large memory. If it is not initialized then its default value is 0.
example, byte data = 100; , here byte is data type data is variable and 100 is value stored by it.
You can change variable and value as you need in your program.

* Short :
The short data type is a 16-bit integer.you can use a short to save memory in large arrays, in situations where the memory savings actually matters. If it is not initialized then its default value is 0. EXample: short sam = 10000; short is data type with sam variable and 1000 is vlue stored by it. Variable and value can be changed.

* Int :
The int data type is a 32-bit integer. This data types is use as default to assign numerical variables. This can store large numbers of data. If it is not initialized then its default value is 0.
example: int var = 100000; here, int is data type and var is variable with value.

*l ong :
The long data type is a 64-bit integer. Use this data type when you need a range of values wider than those provided by int. If it is not initialized then its default value is 0L. example: long sam= 1000000;

* Float :
The float data type is a single-precision 32-bit floating point. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. If it is not initialized then its default value is 0.0F. Example: float f1 = 123.4f;

* Double :
The double data type is a double-precision 64-bit floating point. Range of values in large. If it is not initialized then its default value is 0.0D.eg, double d1 = 123.4;

* Boolean :
The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its size cant be defined. Only conditions. If it is not initialized then its default value is false. example: boolean result = true;

* Char :
The char data type is a single 16-bit Unicode character. It stores only one letter. If it is not initialized then its default value is ‘\u0000′. Example: char variable = ‘C’;

*string (not shortlisted)
String stores string values. like string hello = “Hello world”; Here, String is like a data type hello is variable which stores text data “hello world” . Its default value is null

Basic rules while declaring variables:
>” /> No spaces in variable names<br />
<img title= ~x( < relax like :d
Dont get into every words and dont get confuse. just go lines to line and make concepts.
Just make concepts this type of variable stores this type of data..i will give some examples within the topic..so enjoy!

Hola guys, :d
I already mentioned variable commands and syntax lines on how to use each of them. Now i am giving you an small example to let you know how can we work with this variables.
Well, here is the code how can you read input data and do the actions to it. Watch the code carefully:

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;public class HelloWorld extends MIDlet implements CommandListener{
private Display display;
private Form form;
private Form form2;
private Command go;
private TextField input;
private String msg;
int a=3;

public HelloWorld(){
display = Display.getDisplay(this);
form = new Form(“Hello world”);
form2 = new Form(“output”);
go=new Command(“Check!”, 1,1);
input =new TextField(“enter number”, “”, 50, TextField.NUMERIC);


public void startApp(){


public void pauseApp(){}

public void destroyApp(boolean unconditional){
public void commandAction(Command cmd, Displayable disp) {

String inputdata = input.getString();
double d = Integer.parseInt(inputdata);
double c= d a;
if ( c <5)
msg=”Your input number is less then 5″;
else msg=”Your input number is more then 5″;


Well, no matter to get worried. Its just simple..I will define new added lines in this modified helloworld project. Other which i dont explain are already explained in previous classs.

private TextField input;
private String msg;
int a=3;

we initiated a textfield named input. we initiated s string named msg. We assigned a value of a as 3.

input =new TextField(“enter number”, “”, 50, TextField.NUMERIC);

This is how we define the textfield. You have done for commands before so its the way to do for textfields. “enter number” always append just above th textfield. You can input your desired text here. next after that text you can see”" .If you enter here any text or number then that will be seen inside the textfield. I left it blank because i wanted textfield as blank. Just after that you can see 50. You can assign your own. It determines themaximum length of letters that user can input. TextField.NUMERIC this statements notifies to program that only numeric datas can be inputted in the textfield.


This line appends the input textfield to the form named form.
//command action

String inputdata = input.getString();
double d = Integer.parseInt(inputdata);
if ( c <5)
msg=”Your input number is less then 5″;
else msg=”Your input number is more then 5″;

Variable inputdata captures the string from the textfield that user supplied. input.getString(); is the function to get user inputted string in textfield. where input is the name of textfield.
we use double data type with variable d to parse the data and get actual integers.
We made new function c which stores data of sum of value of a and d that we got from above line.
Now if condition applies. If vaue of c is less then 5 the string msg is passed . Else works as if not. If the information is not true then second msg is output.
We just append this msgs in the form. Finnaly we set current page to form2. After clicking check or go user will be redirected to form2 page and all the works are done inside the programs.

See the importance of variables. How they hold datas and we can use in program as we want.
This was just a example using some of the variables and data types. You can make any others or modify the code.
Still any confusions? Fell free to raise your questions :)
I want to see it more advanced by yourself. Add some commands like exit, back assign more variabl and bring them in action.
Lets see who will make best :)
Best of luck!

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Comment Form

You must be logged in to post a comment.

My Songs

My Music Video

Join Me In Facebook

Recent Activity

Most Recent Visitors

Recommended URLS

www.lovingnepal.com www.npmig33.com www.sudanbhandari.com.np www.sandipkc.com.np www.sprakash.com.np www.madabishek.blogspot.com www.educatesansar.com

Recent Searches