Saturday 25 May 2013

User I/O in Java

This example does console input/output in Java. It asks the user to type his / her name then says Hello.

At the start of the program, the java.io package is imported. This contains code to do system input / output. The asterisk which follows means that the program may want to use any of the classes in java.io.

The program creates an InputStreamReader class variable called isr to read what the user types. It also creates a BufferedReader class variable called br to store it temporarily.

The try block reads the user's input and the catch block handles exceptions: 

UBUNTU > cat prog31.java
import java.io.*;
public class prog31
{
public static void main(String[] args)
  {
  System.out.print("What is your name? ");
  InputStreamReader isr = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(isr);
  try
    {
    String your_name = br.readLine();
    System.out.println("Hello " + your_name);
    }
  catch (IOException test)
    {
    System.out.println("I/O error");
    }
  }
}
UBUNTU > javac prog31.java
UBUNTU > java prog31
What is your name? Andrew
Hello Andrew
UBUNTU >

Thursday 16 May 2013

Java System.out.println and System.out.print

What is the difference between these two?
 
System.out.println outputs a newline character afterwards:
 
UNIX > cat prog28.java
public class prog28
{
public static void main (String args[])
  {
  System.out.println("International DBA");
  }
}
UNIX > javac prog28.java
UNIX > java prog28
International DBA
UNIX >
 
… but System.out.print doesn’t:
 
UNIX > cat prog29.java
public class prog29
{
public static void main (String args[])
  {
  System.out.print("International DBA");
  }
}
UNIX > javac prog29.java
UNIX > java prog29
International DBAUNIX >
 
… although you can add one yourself if you wish:
 
UNIX > cat prog30.java
public class prog30
{
public static void main (String args[])
  {
  System.out.print("International DBA\n");
  }
}
UNIX > javac prog30.java
UNIX > java prog30
International DBA
UNIX >

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 13 May 2013

Operator Precedence in Java

Operator precedence is important in any programming language. This determines which of the arithmetical operators i.e. addition, subtraction, multiplication, division etc is done first.

In ICL's System Control Language (SCL), if I remember correctly, arithmetical operators all had the same precedence and were evaluated from left to right so 1 + 2 * 3 would come to 9. This is quite unusual.

In Java, multiplication and division have a higher priority than addition and subtraction but you can use brackets to override this behaviour. This is similar to a wide variety of other programming languages. You can see what I mean in the example below:

UBUNTU > cat prog27.java
public class prog27
{
public static void main (String args[])
  {
  System.out.println
  ("1 + 2 * 3 = " + (1 + 2 * 3));
  System.out.println
  ("(1 + 2) * 3 = " + ((1 + 2) * 3));
  }
}
UBUNTU > javac prog27.java
UBUNTU > java prog27
1 + 2 * 3 = 7
(1 + 2) * 3 = 9
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.

Saturday 11 May 2013

Java Modulus Operator

Many programming languages have a modulus operator, which allows you to calculate the remainder left over after a division. If I remember correctly, COBOL actually used the word REMAINDER to allow you to do this. In Java, you use the percent symbol as shown below:

UBUNTU > cat prog26.java
public class prog26
{
public static void main (String args[])
  {
  System.out.println
  ("17/5 = " + (int) 17/5 + " remainder " + 17%3);
  System.out.println
  ("15/2 = " + (int) 15/2 + " remainder " + 15%2);
  }
}
UBUNTU > javac prog26.java
UBUNTU > java prog26
17/5 = 3 remainder 2
15/2 = 7 remainder 1
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 10 May 2013

How Will I Manage Without Javablogs?

Javablogs has closed. If anybody knows how I might get people to view my blog posts now, please add a comment below. As usual, click on the image to enlarge it and bring it into focus:


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.

Java Newline and Carriage Return Characters

A newline is represented by \n and a carriage return by \r. UNIX only seems to recognize the newline:
 
UNIX > cat prog24.java
public class prog24
{
public static void main (String args[])
  {
  System.out.println("Newline->\n");
  System.out.println("Carriage return->\r");
  }
}
UNIX > javac prog24.java
UNIX > java prog24
Newline->
 
Carriage return->
UNIX >
 
If you copy the program to a Windows environment and run it in a command prompt, both characters are recognized:
 
C:\Users\J0294094\Java>java prog24
Newline->
 
Carriage return->
 
C:\Users\J0294094\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.

Java Tab Character

You can use \t to tab across in Java. Tab positions appear to be every 8 characters on the UNIX system below:

UNIX > cat prog25.java
public class prog25
{
public static void main (String args[])
  {
  System.out.println("         1         2");
  System.out.println("12345678901234567890");
  System.out.println("Andrew\tReid");
  System.out.println("Andrew\t\tReid");
  }
}
UNIX > javac prog25.java
UNIX > java prog25
         1         2
12345678901234567890
Andrew  Reid
Andrew          Reid
UNIX >

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 9 May 2013

How to Use Hexadecimal Values in Java

You can tell Java that a number is in hexadecimal (base 16) by prefixing it with 0x.
In the example below, the variable hexadecimal_1F is assigned the value 0x1F i.e. hexadecimal 1F. The program then displays the value in decimal (base 10).
 
UNIX > cat prog23.java
public class prog23
{
public static void main (String args[])
  {
  int hexadecimal_1F = 0x1F;
  System.out.println
  ("hexadecimal 1F = " + hexadecimal_1F);
  }
}
UNIX > javac prog23.java
UNIX > java prog23
hexadecimal 1F = 31
UNIX >
 
You can check the result yourself by converting hexadecimal 1F to decimal (base 10) like this:
 
Hexadecimal 1F = (in decimal or base 10) (1 x 16) + 15 = 31
 
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 7 May 2013

How to Use Octal Values in Java

You can tell Java that a number is in octal (base 8) by prefixing it with a zero.

In the example below, the variable octal_77 is assigned the value 077 i.e. octal 77. The program then displays the value in decimal (base 10).

UNIX > cat prog21.java
public class prog21
{
public static void main (String args[])
  {
  int octal_77 = 077;
  System.out.println
  ("octal 77 = " + octal_77);
  }
}
UNIX > javac prog21.java
UNIX > java prog21
octal 77 = 63
UNIX > 

You can check the result yourself by converting octal 77 to decimal (base 10) like this:

octal 77 = (in decimal or base 10) (7 x 8) + 7 = 63

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.

Sunday 5 May 2013

Java boolean Variables

These store the values true or false. There are two in the example below.

The first is called one_greater_than_two and stores the value obtained by evaluating the condition 1 > 2, which is clearly false.

The second is called two_greater_than_one and stores the value obtained by evaluating the condition 2 > 1, which is clearly true.

The tests at the end of the program check that the two booleans have been assigned the correct values:

UBUNTU > cat prog20.java
public class prog20
{
public static void main (String args[])
  {
  boolean one_greater_than_two = (1 > 2);
  boolean two_greater_than_one = (2 > 1);

  if (one_greater_than_two)
    {System.out.println ("1 > 2");}

  if (two_greater_than_one)
    {System.out.println ("2 > 1");}
  }
}
UBUNTU > javac prog20.java
UBUNTU > java prog20
2 > 1
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.

Java char Variables

A Java char variable can store one Unicode value. You can see this in the example below:

UBUNTU > cat prog19.java
public class prog19
{
public static void main (String args[])
  {
  char A = 'A';
  System.out.println ("A = " + A);
  }
}
UBUNTU > javac prog19.java
UBUNTU > java prog19
A = A
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.

Wednesday 1 May 2013

Java Escape Character

If you wanted a Java program to display My name is “Andrew”, or whatever your name happens to be, you might try to do it like this:
 
UNIX > cat prog16.java
public class prog16
{
public static void main (String args[])
  {
  System.out.println
  ("My name is "Andrew"");
  }
}
UNIX > javac prog16.java
prog16.java:6: ')' expected
  ("My name is "Andrew"");
                ^
1 error
UNIX >
 
… but it does not work as the compiler treats the double quote before Andrew as the end of the literal then sees Andrew as an error. You can get round this by placing an escape character before the double quotes at the start and end of Andrew. Java then treats these double quotes just like any other character rather than literal delimiters. The escape character to use is a backwards slash as shown below:
 
UNIX > cat prog17.java
public class prog17
{
public static void main (String args[])
  {
  System.out.println
  ("My name is \"Andrew\"");
  }
}
UNIX > javac prog17.java
UNIX > java prog17
My name is "Andrew"
UNIX >

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.