Creating Variables in Java

Creating Variables in Java

We've seen that Java applications are class based, and we've gotten enough Java down now to create basic programs. The next step in Java programming is to start storing your data so that you can work on that data. As with JavaScript, variables serve as locations in memory in which you can store your data. However, unlike JavaScript, Java variables are strongly typed, which means that you have to declare a type for each variable and be careful about mixing those types.

For example, one of the most common variable types is int , which stands for "integer." This type sets aside 4 bytes of memory, which means that you can store values between -2,147,483,648 and 2,147,483,647 in int variables. Quite a few different variable types are built into Java, such as integers, floating-point numbers , and individual characters .

When you want to use a variable in Java, you must declare it, specifying the variable's type:

  type   name  [=  value  ][,  name  [=  value  ]...]; 

Here's an example showing how to declare a variable of the int type. This variable is named counter :

 public class ch10_03  {     public static void main(String[] args)     {  int counter;  .         .         .     } } 

I've set aside 4 bytes of memory for the variable named counter . I can store a value of 2003 in that counter like this, using the Java assignment operator:

 public class ch10_03  {     public static void main(String[] args)     {         int counter;  counter = 2003;  .     .     .     } } 

And I can display the value in the counter variable with a println statement, like this:

Listing ch10_03.java
 public class ch10_03 {     public static void main(String[] args)     {         int counter;         counter = 2003;  System.out.println("The current counter value is " + counter);  } } 

Here's the result of this code:

 %java ch10_03  The current counter value is 2003 

As in JavaScript, there's a shortcut you can use to both declare a variable and assign a value to it at the same time like this:

 public class ch10_03  {     public static void main(String[] args)     {  int counter = 2003;  System.out.println("The current counter value is " + counter);     } } 

Plenty of variable types are built into Java besides int :

  • Boolean The boolean type holds only two types of values: true and false .

  • Characters The char type holds representations of characters, such as letters and numbers.

  • Floating-point numbers There are two types here: float and double (for double precision), which hold signed floating-point numbers.

  • Integers There are a number of integer types, such as byte (1 byte of storage), short (usually 2 bytes), int (usually 4 bytes), and long (usually 8 bytes), which hold signed, whole-value numbers.

Java is a very strongly typed language, which means that it's very particular about mixing data types. For example, look at this code, in which I'm declaring a floating-point number and an integer, and then assigning the floating-point number to the integer:

 public class ch10_03  {     public static void main(String[] args)     {         float counter = 2003;         int counter2;  counter2 = counter;  System.out.println("The current counter2 value is " + counter2);     } } 

Java regards this as a problem because the floating-point type can hold numbers with greater precision than the int type. So, it returns an error when you try to compile this code, saying that an "explicit cast" is required to convert a floating-point number to an integer:

 %javac ch10_03.java  ch10_03.java:8: Incompatible type for =. Explicit cast needed to convert float to int.         counter2 = counter;                  ^ 1 error 

To solve a problem like this, you can explicitly request that Java convert the floating-point number to an integer with the cast (int) :

 public class ch10_03  {     public static void main(String[] args)     {         float counter = 2003;         int counter2;  counter2 = (int) counter;  System.out.println("The current counter2 value is " + counter2);     } } 

You can convert between types like this if required, but bear in mind that you could lose some numerical precision this way.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net