Here is a Java program that does nothing.
public class Test
{
}
To compile, use the javac
command.
unix> javac Test.java
You will notice a new file, Test.class
, which
is the Java bytecode translated from your source. Here is a
hex dump of it. It largely looks like gibberish with some
recognizable elments.
unix> xxd Test.class 0000000: cafe babe 0000 0034 000d 0a00 0300 0a07 .......4........ 0000010: 000b 0700 0c01 0006 3c69 6e69 743e 0100 ........<init>.. 0000020: 0328 2956 0100 0443 6f64 6501 000f 4c69 .()V...Code...Li 0000030: 6e65 4e75 6d62 6572 5461 626c 6501 000a neNumberTable... 0000040: 536f 7572 6365 4669 6c65 0100 0954 6573 SourceFile...Tes 0000050: 742e 6a61 7661 0c00 0400 0501 0004 5465 t.java........Te 0000060: 7374 0100 106a 6176 612f 6c61 6e67 2f4f st...java/lang/O 0000070: 626a 6563 7400 2100 0200 0300 0000 0000 bject.!......... 0000080: 0100 0100 0400 0500 0100 0600 0000 1d00 ................ 0000090: 0100 0100 0000 052a b700 01b1 0000 0001 .......*........ 00000a0: 0007 0000 0006 0001 0000 0001 0001 0008 ................ 00000b0: 0000 0002 0009
We then gave our program a main
method so it
would execute.
public class Test
{
public static void main(String[] args)
{
System.out.println("Testing Java....");
}
}
Since we changed the source code we must recompile. After that, we run the program.
unix> javac Test.java
unix> java Test
Testing Java....
System.out.println
is just like Python's
print
.
Here is a cold-cocking from Java.
jshell> x = 5
| Error:
| cannot find symbol
| symbol: variable x
| x = 5
| ^
It turns out that variables have type in Java. Here we
specify that our variable x
has type int
.
jshell> int x = 5;
x ==> 5
Use /vars
to see what variables you have created
in your session. It works like dir()
in Python.
jshell> /vars
| int x = 5
Here is a floating-point type.
jshell> double z = 4.56;
z ==> 4.56
There is a second floating-point type in Java, which we
shall probably never use, float
.
jshell> float f = 4.5
| Error:
| incompatible types: possible lossy conversion from double to float
| float f = 4.5;
| ^-^
You have to put an f
at the end as follows.
jshell> float f = 4.5f;
f ==> 4.5
The double type is a 64 bit double-precison IEEE 754 number. The float type is a 32 bit single-precison IEEE 754 number.
Now let's make a string.
jshell> String s = "some string";
s ==> "some string"
Here is how to see all of the variables you have created.
jshell> /vars | int x = 5 | double z = 4.56 | float f = 4.5 | String s = "some string"
Compound assignment: Just like Python.
jshell> x += 1; $5 ==> 6 jshell> x x ==> 6
This the autoincrement operator. It does not exist in Python.
jshell> x++; //does not exist in Python $7 ==> 6 jshell> x x ==> 7
Notice that each reply in jshell is given a variable name
of the form $number
.
jshell> $5 $5 ==> 6 jshell> $7 $7 ==> 6
More compound assignment.
jshell> x += 2 $11 ==> 9 jshell> x *= 2 $12 ==> 18
Integer divison for integers.
jshell> 6/5 $13 ==> 1
Here is how to cast.
jshell> (double) 6/5 $14 ==> 1.2
Our old friend mod.
jshell> 365%7 $15 ==> 1
No **.
jshell> 5**3 | Error: | illegal start of expression | 5**3 | ^
Batman says POW!
jshell> Math.pow(5,3) $16 ==> 125.0
There are many Math functions.
jshell> Math.cos(0) $17 ==> 1.0
No factorial.
jshell> int x = Math.factorial(5) | Error: | cannot find symbol | symbol: method factorial(int) | int x = Math.factorial(5); | ^------------^
Our old pal log.
jshell> Math.log(10) $18 ==> 2.302585092994046 jshell> Math.log10(10) $19 ==> 1.0
Relational operators on numbers.
jshell> 4 < 5 $20 ==> true jshell> boolean b = 4 < 5; b ==> true
And, and or.
jshell> true && true $22 ==> true jshell> //and jshell> true || true $23 ==> true jshell> // || is or
Bitwise operators too.
jshell> 5&7 $24 ==> 5
Watch this....
jshell> int n = 0; n ==> 0 jshell> n = 1; n ==> 1 jshell> while(n > 0){n++;} jshell> n n ==> -2147483648 jshell> n--; $29 ==> -2147483648
The integers are a circle, not a line. The
int
type is a 32 bit integer type.
jshell> n n ==> 2147483647
Have a byte.
jshell> byte b = 1 b ==> 1 jshell> while(b > 0){b++;}
I'll be with your shortly.
jshell> b
b ==> -128
jshell> short sh = 1
sh ==> 1
jshell> while(sh > 0){sh++;}
jshell> sh
sh ==> -32768
A byte is a one-byte integer.
jshell> b
b ==> -128
jshell> b = 300;
| Error:
| incompatible types: possible lossy conversion from int to byte
| b = 300;
| ^-^
jshell> b
b ==> -128
Don't cast caution aside.
jshell> b = (byte) 300;
b ==> 44
Longs are long.
jshell> long big = 1;
big ==> 1
jshell> for(int k = 0; k < 30; k++){big *= 2;}
jshell> big
big ==> 1073741824
jshell> for(int k = 0; k < 30; k++){big *= 2;}
jshell> big
big ==> 1152921504606846976
jshell> big *=2
$45 ==> 2305843009213693952
jshell> big *=2
$46 ==> 4611686018427387904
jshell> big *=2
$47 ==> -9223372036854775808
long is a 64 bit integer.
So far we have this.
- Four integer types: byte, short, int, long.
- The boolean type: true, false
- floating-point types: float, double
jshell> char q = 'a'
q ==> 'a'
jshell> (int) q
$49 ==> 97
jshell> (char) 65
$50 ==> 'A'
jshell>