Workshop 2007 JRE Java Support
Installation
Home
CalcTool
Java Applets
Friends/Family
Gardening
Woodworking
Forums

Declaring Variables and Converting Data Types

 

Each time you use a variable in Java, you must declare what type it is.  These types could include primitive types, which are default types in Java, or they could include user defined types of your own construction. There are the 6 primitive types of variable.

 

Primitive Data Types

 

Data Type

Content

Default Value

Min Value

Max Value

byte

Integer

0

-128

127

short

Integer

0

-32768

32727

int

Integer

0

-2147483648

2147483647

long

Integer

0

-9223372032654775808

9223372032654775808

float

Real

0.0

-3.40282347E+38

3.40282347E+38

double

Real

0.0

-1.79769343486231570E+308

1.79769343486231570E+308

char

Character

N/A

N/A

N/A

Boolean

Logical

N/A

false

true

char variables only represent single characters.  These characters include most of the keyboard strokes on your keyboard, plus some with the escape prefix:
      \b         backspace
      \t          tab
      \n         new line
      \f          new page (form feed)
      \r          return to the start of the line

 

Strings.

We would like to string together several different characters to represent words and sentences.  So at least two classes have been defined to help us do this:
            String
      StringBuffer

Please note the use of the capital letter “S” when declaring these variables. 

For example we can call and initialize a String with the name firstname via:
            String firstname=new String(“Jim”);//firstname is defined as Jim

In general, we instantiate a class (that is call it into existence) via the new command.  In the case of the String class, we can also use one of the following shortcuts

                        String firstname; //Declaration and then initialization
            firstname=”Jim”;

            or
                        String firstname=”Jim”; //Both declaration and initialization
                                      
Please note the use of double quotation marks, “, instead of single quotation marks, ‘,  when initializing strings. Single quotes are for character typed variables.  Double quotes are for string typed variables.

 

String Buffers.

StringBuffer is an object used primarily for buffered input and output. It is particularly useful for outputting multiple lines of information.

Example Let’s suppose we want to ouput list of numbers from 1 to 10 in a column.  In order to output multiple lines of information, you will not be able to use a Label object.  One helpful object is a TextArea object.

  1. Drag and drop a TextArea object onto your designer pad. It will probably be named textArea1.
  2. Instantiate a StringBuffer object.  Inside of a button_ActionPerformed() method, instantiate a StringBuffer object with the new command. For example, if you name it myStringBuffer, your code will be as follows.

StringBuffer myStringBuffer=new StringBuffer();

  1. Append the numbers 1 thru 10 to myStringBuffer. Set up a for loop and use the append() method.  You should notice that when you type the period after myStringBuffer that a menu will pop up with all available methods.  Choose the append method with a String argument (or just type the word append). Append the counter used in the for method and a carriage return, “\n”.  Here’s an example.

for (int i=1; i<=10; i++){
               myStringBuffer.append(""+i+"\n");
         }

  1. Output the information to the textArea object.  You will want to use the setText() method.  Since this method expects a string, you need to convert myStringBuffer from a StringBuffer object to a String object.  This is done using the toString() method. Here’s an example.

 

textArea1.setText(myStringBuffer.toString());

 

Converting Data Types.

The Convert.java class is useful for converting some data types to other data types.  When writing applets, I’ve primarily used the toDouble() and toInt() methods.  Syntax for use is
Convert.toType(argmument);
For example, when converting a String named inputString to a double, we would call the method as follows.
Convert.toDouble(inputString);

Currently available methods are summarized in the following table.

static public long  

toLong(String snum) Converts String to long

static public double  

toDouble(String snum) Converts String to double

static public double  

toDouble(int inum) Converts int to double

static public int  

toInt(String snum) Converts String to int

static public int  

toInt(double dnum) Truncates double to int

static public int  

toRoundedInt(double dnum) Rounds double to int

static public String  

toString(double dnum) Converts double to a String

static public String  

toString(int inum) Converts int to a String

static public String

toString(float fnum) Converts float to a String

           

Wrapper Classes. 

I’ve used wrapper classes to construct the Convert.java class.  Read on if you are interested in the details…

Other types of variables that we may encounter are ones defined by Double, and Integer classes.  Note the capital letter at each of these classes.  The class Double is known as a wrapper class for the primitive type double.  Thus Double objects and double variables are two different things. The reason Java needs these classes is that it is an object oriented language.  With each of these classes is defined a myriad of different methods (i.e. functions/subroutines) that operate on the Double object.  But as you might expect there is a close relationship between an object defined by the Double class and a variable defined by the double primitive type.  Here’s an example of how to define a Double object.

            Double dval=new Double(70.5);

This statement constructs a Double object named dval and gives it the value of 70.5.

Now suppose you want to extract the value of dval and assign it to the double variable height.  Here’s how to do this:

        double height;
   height=dval.doubleValue();

 

Or, you can combine these three statements into one:
  double height=(new Double(70.5)).doubleValue();

You should observe that doubleValue() is one of many methods that will operate on a Double object.  Note: There are parallel concepts  for Integer objects, Float objects, etc. 

To make life simpler for us, I have written a class to take care of the dirty work.  If you want to use these shortcuts, you should get Convert.java and import it into your project.

Here's an example of converting a double to a String:
double x=3.645;
String=stringx;
stringx=Convert.toString(x);

We can now output this using the setText() method.  For example:
label1.setText(stringx);


Here's an example of converting an int to a String:
int year;
String=stringyear;
stringyear=Convert.toString(year);


Here's an example of converting a float to a String:
float x=3.645;
String=stringx;
stringx=Convert.toString(x);