/********************************************************************
* This program demonstrates the difference between long and int.
********************************************************************/
import java.util.*;
class IntDemo
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n1 = s.nextInt();
System.out.println(n1 + "! = " + factorial(n1));
long n2 = n1;
System.out.println(n2 + "! = " + factorial(n2));
}
static int factorial(int n) { return n == 0 ? 1 : n*factorial(n-1); }
static long factorial(long n) { return n == 0 ? 1 : n*factorial(n-1); }
}
Notice what happens when, as a user, I enter something that the
scanner can't process as an int:
$ java IntDemo
bad user input! ← User Input!
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:857)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at IntDemo.main(IntDemo.java:9)
This is pretty typical. When things go wrong in a Java program you
get a whole bunch of information about what went wrong and where!
This is a good thing!
import java.util.*;
class CharDemo
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
char a = 'X';
char b = s.next(".").charAt(0); // This will be a mystery!
System.out.println("a = " + a + ", b = " + b);
// here are some funky Unicode symbols!
char c = '\u27F0';
char d = '\u0103';
System.out.println("Ex: " + c + " " + d + " " + '\u016E');
}
}
First of all, don't ask about why reading a single char is what it
is. Just take it on faith. Now, what does this output look like?
$ java CharDemo
% ← User Input!
a = X, b = %
Ex: ⟰ ă Ů
(int)x),
as well as implicit cases via assignment, function calls or
promotion in expressions. This is all pretty much like C++.
The boolean type is different. Unlike C++, int or float or char values aren't automatically cast to boolean in contexts like if's/while's/for's which require booleans. On balance, this is probably a good thing, as you can't accidently write
if (x = 5)and have it compile. The compiler will barf on this.
import java.util.*;
class StringPrecAssoc
{
public static void main(String[] args)
{
int a = 2;
int b = 3;
System.out.println(a+b+"foo"); // prints 5foo
System.out.println("foo"+a+b); // prints foo23
System.out.println("foo"+a*b); // prints foo6
}
}
In case you've forgotten: precencence is what determines
which operator is applied first when different operators are
involved. Associativity is what determins which occurence of
two of the same operators is applied first.
class ArrayEx
{
public static void main(String[] args)
{
for(int i = 0; i < args.length; ++i)
System.out.println("args[" + i + "] = " + args[i]);
System.out.println(args[args.length]);
}
}
When we run this program we get the following:
$ java ArrayEx the rain in "Spain falls" mainly on the plains args[0] = the args[1] = rain args[2] = in args[3] = Spain falls args[4] = mainly args[5] = on args[6] = the args[7] = plains Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at ArrayEx.main(ArrayEx.java:10)Once again, notice how Java complains loudly when there's an error. What's especially significant here is that Java notices when you try to access an array with an invalid index (i.e. off the end of the array), refuses to do it for you, and exits the program with a useful error message.