Tuesday 30 April 2013

Java float and double Variables

If you have a number with digits to the right of the decimal point, you can store it in a float or a double variable. However, if you try to assign it directly, it doesn't work:

UBUNTU > cat prog12.java
public class prog12
{
public static void main (String args[])
  {
  float f1 = 1/5;
  double d1 = 1/5;
  System.out.println ("f1 = " + f1);
  System.out.println ("d1 = " + d1);
  }
}
UBUNTU > javac prog12.java
UBUNTU > java prog12
f1 = 0.0
d1 = 0.0
UBUNTU > 


You can add an f (or F) at the end of the number before assigning it to a float variable and you can add a d (or D) at the end before assigning it to a double. Notice how the double has greater precision:

UBUNTU > cat prog13.java
public class prog13
{
public static void main (String args[])
  {
  float f2 = 1/7f;
  double d2 = 1/7d;
  System.out.println ("f2 = " + f2);
  System.out.println ("d2 = " + d2);
  f2 = 1/9F;
  d2 = 1/9D;
  System.out.println ("f2 = " + f2);
  System.out.println ("d2 = " + d2);
  }
}
UBUNTU > javac prog13.java
UBUNTU > java prog13
f2 = 0.14285715
d2 = 0.14285714285714285
f2 = 0.11111111
d2 = 0.1111111111111111
UBUNTU >

Alternatively, you can add (float) before a float variable or (double) before a double. This is called casting:

UBUNTU > cat prog14.java
public class prog14
{
public static void main (String args[])
  {
  float f3 = (float) 1/13;
  double d3 = (double) 1/13;
  System.out.println ("f3 = " + f3);
  System.out.println ("d3 = " + d3);
  }
}
UBUNTU > javac prog14.java
UBUNTU > java prog14
f3 = 0.07692308
d3 = 0.07692307692307693
UBUNTU >

As well as giving greater precision, a double can also store much larger numbers than a float:

UBUNTU > cat prog15.java
public class prog15
{
public static void main (String args[])
  {
  System.out.println
  ("Largest float = " + Float.MAX_VALUE);
  System.out.println
  ("Largest double = " + Double.MAX_VALUE);
  }
}
UBUNTU > javac prog15.java
UBUNTU > java prog15
Largest float = 3.4028235E38
Largest double = 1.7976931348623157E308
UBUNTU >

If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

 

Monday 29 April 2013

Compile Once - Run Anywhere

In the previous example, I compiled and ran prog11 on a Solaris machine. Java is designed to be platform independent or portable. To show that it could be run on a different platform, I copied the prog11.class file to a Windows 7 machine in binary mode. Then I ran it there as follows:
 
C:\Java>dir
Volume in drive C is System
Volume Serial Number is B068-444A
 
Directory of C:\Java
 
04/29/2013  05:37 PM    <DIR>          .
04/29/2013  05:37 PM    <DIR>          ..
04/29/2013  05:36 PM               717 prog11.class
               1 File(s)            717 bytes
               2 Dir(s)  204,152,512,512 bytes free
 
C:\Java>java prog11
a = 100
b = 100
 
C:\Java>

If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

How to Copy a long Java Variable Into an int

You might think that you could copy a long variable into an int variable like this:
 
Solaris > cat prog10.java
public class prog10
{
public static void main (String args[])
  {
  int a;
  long b = 100;
  a = b;
  System.out.println ("a = " + a);
  System.out.println ("b = " + b);
  }
}
Solaris >
 
… but you get a compilation error if you try:
 
Solaris > javac prog10.java
prog10.java:7: possible loss of precision
found   : long
required: int
  a = b;
      ^
1 error
Solaris >
 
You have to do it like this instead:
 
Solaris > cat prog11.java
public class prog11
{
public static void main (String args[])
  {
  int a;
  long b = 100;
  a = (int) b;
  System.out.println ("a = " + a);
  System.out.println ("b = " + b);
  }
}
Solaris > javac prog11.java
Solaris > java prog11
a = 100
b = 100
Solaris >

If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

Saturday 27 April 2013

How Big (or Small) Can a Java Long Integer Be?

Java long variables let you store even larger numbers. The program below shows how you can display the maximum and minimum values allowed:

UBUNTU > cat prog9.java
public class prog9
{
public static void main (String args[])
  {
  System.out.println
  ("The largest long you can have is " + Long.MAX_VALUE );
  System.out.println
  ("The smallest long you can have is " + Long.MIN_VALUE );
  }
}
UBUNTU > javac prog9.java
UBUNTU > java prog9
The largest long you can have is 9223372036854775807
The smallest long you can have is -9223372036854775808
UBUNTU > 

If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

 

Friday 26 April 2013

Multiplication in Java

For many situations, the int data type will be adequate for storing integer variables. It can hold values between (roughly) + or - 2 billion. You can multiply two integers as follows:

UBUNTU > cat prog7.java
public class prog7
{
public static void main (String args[])
  {
  // Declare 2 integers:
  int num1 = 1234;
  int num2 = 4321;

  // Multiply them together:
  int num3 = num1 * num2;

  // Display the result:
  System.out.println("num1 = " + num1);
  System.out.println("num2 = " + num2);
  System.out.println("num1 * num2 = " + num3);
  }
}
UBUNTU > javac prog7.java
UBUNTU > java prog7
num1 = 1234
num2 = 4321
num1 * num2 = 5332114
UBUNTU >


... but what happens if the result is too big to store in an int variable?

UBUNTU > cat prog8.java
public class prog8
{
public static void main (String args[])
  {
  // Declare 2 integers:
  int num1 = 123456;
  int num2 = 654321;

  // Multiply them together:
  int num3 = num1 * num2;

  // Display the result:
  System.out.println("num1 = " + num1);
  System.out.println("num2 = " + num2);
  System.out.println("num1 * num2 = " + num3);
  }
}
UBUNTU > javac prog8.java
UBUNTU > java prog8
num1 = 123456
num2 = 654321
num1 * num2 = -824525248
UBUNTU >

According to my calculator, the result should be 80,779,853,376 so some arithmetic overflow must have taken place. As soon as I find out how to handle this, I will do a worked example to demonstrate.

If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

Thursday 25 April 2013

Java Short Integers

These are 16-bit signed integers. They can store values between -32768 and +32767 both inclusive. If you try to assign numbers outside this range, you get a compilation error: 

UBUNTU > cat prog5.java
public class prog5
{
public static void main (String args[])
  {
  // Declare a short integer:
  short num1;

  // Try to assign some invalid values:
  num1 = -32769;
  num1 = 32768;
  }
}
UBUNTU > javac prog5.java
prog5.java:9: possible loss of precision
found   : int
required: short
  num1 = -32769;
          ^
prog5.java:10: possible loss of precision
found   : int
required: short
  num1 = 32768;
         ^
2 errors
UBUNTU >

...but numbers within the range are accepted:

UBUNTU > cat prog6.java
public class prog6
{
public static void main (String args[])
  {
  // Declare a short integer:
  short num1;

  // Assign the minimum valid value:
  num1 = -32768;

  // Display the value:
  System.out.println("num1 = " + num1);

  // Assign the maximum valid value:
  num1 = 32767;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU > javac prog6.java
UBUNTU > java prog6
num1 = -32768
num1 = 32767
UBUNTU >


If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

 

Tuesday 23 April 2013

Java Byte-Size Integers

This post checks what you can and cannot store in a Java byte variable. In prog2, I tried to store -129 in a Java byte variable:

UBUNTU > cat prog2.java
public class prog2
{
public static void main (String args[])
  {
  // Declare a byte-size integer:
  byte num1;

  // Check what values it can store:
  num1 = -129;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU >


... but I was unable to compile it:

UBUNTU > javac prog2.java
prog2.java:9: possible loss of precision
found   : int
required: byte
  num1 = -129;
          ^
1 error
UBUNTU >


In prog3, I tried to use a Java byte variable to store -128 and this worked:

UBUNTU > cat prog3.java
public class prog3
{
public static void main (String args[])
  {
  // Declare a byte-size integer:
  byte num1;

  // Check what values it can store:
  num1 = -128;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU > javac prog3.java
UBUNTU > java prog3
num1 = -128
UBUNTU >
 


In prog4, I tried to use a Java byte variable to store 127 and this worked too:

UBUNTU > cat prog4.java
public class prog4
{
public static void main (String args[])
  {
  // Declare a byte-size integer:
  byte num1;

  // Check what values it can store:
  num1 = 127;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU > javac prog4.java
UBUNTU > java prog4
num1 = 127
UBUNTU >

In prog5, I tried to store 128 in a Java byte variable but this produced a compilation error:

UBUNTU > cat prog5.java
public class prog5
{
public static void main (String args[])
  {
  // Declare a byte-size integer:
  byte num1;

  // Check what values it can store:
  num1 = 128;

  // Display the value:
  System.out.println("num1 = " + num1);
  }
}
UBUNTU > javac prog5.java
prog5.java:9: possible loss of precision
found   : int
required: byte
  num1 = 128;
         ^
1 error
UBUNTU >

So it seems that Java byte variables can be used to store integers from -128 to +127, both inclusive.


If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.

 

Monday 22 April 2013

How to Define Integer Variables in Java

The program below declares 3 integer variables, num1, num2 and num3. It assigns values to num1 and num2 then adds them together and puts the result in num3

UBUNTU >cat prog1.java
public class prog1
{
public static void main (String args[])
  {
  // Declare 3 integer variables:
  int num1, num2, num3;

  // Give them values:
  num1 = 1;
  num2 = 2;
  num3 = num1 + num2;
 
  // Display the result:
  System.out.println
  (num1 + " + " + num2 + " = " + num3);
  }
}
UBUNTU >


I compiled it then ran it and it produced the output below:

UBUNTU >javac prog1.java
UBUNTU >java prog1
1 + 2 = 3
UBUNTU >


Here is another example:

UBUNTU > cat prog25.java
public class prog25
{
public static void main (String args[])
  {
  int a = 10, b = 20, c = 30;
  System.out.println ("a = " + a);
  System.out.println ("b = " + b);
  System.out.println ("c = " + c);
  int x, y, z;
  x = y = z = 40;
  System.out.println ("x = " + x);
  System.out.println ("y = " + y);
  System.out.println ("z = " + z);
  }
}
UBUNTU > javac prog25.java
UBUNTU > java prog25
a = 10
b = 20
c = 30
x = 40
y = 40
z = 40
UBUNTU >

Friday 19 April 2013

How I Created a Simple Java Applet

I used vi to create a file like this:

UBUNTU > cat Andrew_Was_Here.java
// A simple applet.
import java.awt.Graphics;
public class Andrew_Was_Here
extends java.applet.Applet
{
public void paint(Graphics g)
  {
  g.drawString("Andrew Was Here!",20,40);
  }
}
UBUNTU >

I compiled it:

UBUNTU > javac Andrew_Was_Here.java
UBUNTU

Applets run within a browser so I wrote a few lines of HTML:

UBUNTU > cat Andrew_Was_Here.html
<html>
<head>
<title>Andrew Was Here</title>
</head>
<body>
<applet name="Andrew Was Here"
 code="Andrew_Was_Here.class"
 width = 400 height = 200>
</applet>
</body>
</html>
UBUNTU >


Then I typed the following command at the UBUNTU prompt:

appletviewer Andrew_Was_Here.html

... and the applet was displayed like this. As usual, click on the image to enlarge it and bring it into focus:

I was also able to run it in a browser as shown below:

Thursday 18 April 2013

How to Add Comments in Java

There are two ways to include comments in your Java code:
  • You can add one or more single-line comments by starting each line with two forward slashes.
  • You can add a multi-line comment by starting it with /* and ending it with */.
You can see what I mean in the program source code below:

UBUNTU > cat Andrew_Was_Here.java
// This is a one-line comment.
// You can do a multi-line comment as shown below:
/*
Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
All the king's horses and all the king's men
Couldn't put Humpty together again.
*/
public class Andrew_Was_Here
  {
  public static void main(String args[])
    {
    System.out.println("Andrew was here!");
    }
  }
UBUNTU >

Monday 15 April 2013

My First Java Program

Once I had installed the Java Software Development Kit (SDK), I typed the code below into a text file:

UBUNTU > cat Andrew_Was_Here.java
/*
 * My First Program
*/
public class Andrew_Was_Here
  {
  public static void main(String args[])
    {
    System.out.println("Andrew was here!");
    }
  }
UBUNTU >


The file was in a directory by itself:

UBUNTU > ls
Andrew_Was_Here.java
UBUNTU >

I compiled it as follows:

UBUNTU > javac Andrew_Was_Here.java
UBUNTU >

A new file called Andrew_Was_Here.class appeared in the directory:

UBUNTU > ls
Andrew_Was_Here.class  Andrew_Was_Here.java
UBUNTU >

This contains the compiled Java code. I ran it like this: 

UBUNTU > java Andrew_Was_Here
Andrew was here!
UBUNTU >