Introductionto JAVA
Introductionto JAVA
Carlos Kavka
INFN Sezione di Trieste
Area di Ricerca, Padriciano 99
34012, Trieste, Italia
Introduction
●
Java is a very powerful language that has generated
a lot of interest in the last years.
Java
Oak
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 2
Introduction
●
The world wide web has popularized the use of Java,
because programs can be transparently downloaded with
web pages and executed in any computer with a Java
capable browser.
●
A Java application is a standalone Java program that can be
executed independently of any web browser.
●
A Java applet is a program designed to be executed
under a Java capable browser.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 4
The Java platform
●
Java programs are compiled to Java byte-codes,
a kind of machine independent representation.
The program is then executed by an interpreter
called the Java Virtual Machine (JVM).
Compiler
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 5
The Java platform
●
The compiled code is independent of the
architecture of the computer.
●
The price to pay is a slower execution.
Interpreter (JVM)
Compiler
Interpreter (JVM)
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 6
A first example
/**
* Hello World Application
* Our first example
*/
public class HelloWorld {
public static void main(String[] args)
{ System.out.println("Hello World!"); // display
output
}
}
$ javac HelloWorld.java
$ ls
HelloWorld.class
HelloWorld.java
$ java HelloWorld
Hello World
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 7
Documentation
●
The javadoc utility can be used to generate
automatically documentation for the class.
/**
* My first <b>Test</b>
* @author Carlos Kavka
* @version 1.1
*/
public class HelloWorld {
/**
* @param args the command line arguments
* @since 1.0
*/
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 8
Documentation
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 9
Fundamental types
●
Java provides ten fundamental types:
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 10
Variables
●
The variables are declared specifying its type and
name, and initialized in the point of declaration, or
later with the assignment expression:
int x;
double f = 0.33;
char c = ’a’;
String s = "abcd";
x = 55;
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 11
Literals
●
The integer values can be written in decimal,
hexadecimal, octal and long forms:
int x = 34; // decimal value
int y = 0x3ef; // hexadecimal
int z = 0772; // octal
long m = 240395922L; // long
●
The floating point values are of type double by
default:
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 12
Literals
●
The character values are specified with the standard
C notation, with extensions for Unicode values:
●
The boolean values are true and false:
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 13
Constants
●
Constants are declared with the word final in front.
The specification of the initial value is compulsory:
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 14
Expressions
●
Java provides a rich set of expressions:
– Arithmetic
– Bit level
– Relational
– Logical
– Strings
related
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 15
Arithmetic expressions
●
Java provides the usual set of arithmetic operators:
– addition (+)
– subtraction (-)
– division (/)
– multiplication (‡)
– modulus (%)
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 16
Arithmetic operators
class Arithmetic {
public static void main(String[] args) {
int x = 12;
int y = 2 * x;
System.out.println(y);
int z = (y - x) % 5;
System.out.println(z);
final float pi = 3.1415F;
float f = pi / 0.62F;
System.out.println(f);
}
}
$ java Arithmetic
24
2
5.0669355
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 17
Arithmetic operators
●
Shorthand operators are provided:
class ShortHand {
public static void main(String[] args) {
int x = 12;
x += 5; // x = x
+ 5 System.out.println(x);
x *= 2; // x = x
* 2 System.out.println(x);
}
}
$ java ShortHand
17
34
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 18
Arithmetic operators
●
Pre and post operators are also provided:
class Increment {
public static void main(String[] args) {
int x = 12,y = 12;
$ java Increment
12 13 13 13
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 19
Relational expressions
●
Java provides the following relational operators:
– equivalent (==)
– not equivalent (!=)
– less than (<)
– greater that (>)
less
– than or equal (<=)
– greater than or equal (>=)
●
Important: relational expressions always return a
boolean value.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 20
Relational Expressions
class Boolean {
public static void main(String[] args) {
int x = 12,y = 33;
$ java Boolean
true
false
true
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 21
Bit level operators
●
Java provides the following operators:
– and (&)
– or (|)
not(˜)
–
shift left
–
– (<<)
shift
– right with sign extension (>>)
● shift right with
Important: zero
char, extension
short (>>>).
and byte arguments are
promoted to int before and the result is an int.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 22
Bit level operators
class Bits {
public static void main(String[] args) {
int x = 0x16; // 00000000000000000000000000010110
int y = // 00000000000000000000000000110011
0x33;
System.out.println(x & y);// 00000000000000000000000000010010
System.out.println(x | y);// 00000000000000000000000000110111
System.out.println(˜x); // 11111111111111111111111111101001
short s = 7; // 0000000000000111
System.out.println(˜s); // 11111111111111111111111111111000
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 23
Bit level operators
class Bits2 {
public static void main(String[] args) {
int x = 0x16; //00000000000000000000000000010110
System.out.println(x << 3);//00000000000000000000000010110000
●
Important: The logical operators can only be
applied to boolean expressions and return a
boolean value.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 25
Logical operators
class Logical {
public static void main(String[] args) {
int x = 12,y = 33;
double d = 2.45,e = 4.54;
$ java Logical
true
false
true
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 26
String operators
●
Java provides many operators for Strings:
– Concatenation (+)
– many more...
●
Important: If the expression begins with a string
and uses the + operator, then the next argument is
converted to a string.
●
Important: Strings cannot be compared with ==
and !=.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 27
String operators
class Strings {
public static void main(String[] args) {
class Strings2 {
public static void main(String[] args) {
String s1 = "Hello";
String s2 =
"Hello";
System.out.println(s1.equals(s2));
System.out.println(s1.equals("Hi"));
}
}
$ java Strings2
true
false
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 29
Casting
●
Java performs a automatic type conversion in the
values when there is no risk for data to be lost.
class TestWide {
public static void main(String[] args) {
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 30
Casting
●
In order to specify conversions where data can be lost
it is necessary to use the cast operator.
class TestNarrow {
public static void main(String[] args) {
long a = 34;
int b = (int)a; // a is a long
double d =
3.45; // d is a double
} float f =
} (float)d;
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 31
Control structures
●
Java provides the same set of control structures
than C.
●
Important: the value used in the conditional
expressions must be a boolean.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 32
Control structures (if)
class If {
public static void main(String[] args) {
char c = ’x’;
if ((c >= ’a’ && c <= ’z’) || (c >= ’A’ && c <= ’Z’))
System.out.println("letter: " + c);
else
if (c >= ’0’ && c <= ’9’)
System.out.println("digit: " + c);
else {
System.out.println("the character is: " + c);
System.out.println("it is not a letter");
System.out.println("and it is not a digit");
}
}
}
$ java If
letter:
x
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 33
Control structures (while)
class While {
public static void main(String[] args) {
final float initialValue = 2.34F;
final float step = 0.11F;
final float limit = 4.69F;
float var = initialValue;
$ java While
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 34
Incremented 22 times
Control structures (for)
class For {
public static void main(String[] args) {
final float initialValue = 2.34F;
final float step = 0.11F;
final float limit = 4.69F;
int counter = 0;
$ java For
Incremented 22 times
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 35
Control structures (break/continue)
class BreakContinue {
public static void main(String[] args) {
$ java BreakContinue
0 2 4 6 done.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 36
Control structures (switch)
class Switch {
public static void main(String[] args) {
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 37
Control structures (switch)
$ java Switch
number of days: 366
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 38
Arrays
●
Arrays can be used to store a number of elements
of the same type:
int[] a; // an unitialized array of integers
float[] b; // an unitialized array of floats
String[] c; // an unitialized array of Strings
●
Important: The declaration does not specify a size.
However, it can be inferred when initialized:
int[] a = {13,56,2034,4,55}; // size: 5
float[] b = {1.23F,2.1F}; // size: 2
String[] c = {"Java","is","great"}; // size: 3
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 39
Arrays
●
Other possibility to allocate space for arrays
consists in the use of the operator new:
int i = 3,j = 5;
double[] d; // unitialized array of doubles
●
Components of the arrays are initialized with
default values:
– 0 for numeric type elements,
– '\0' for characters
null
– for references.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 40
Arrays
●
Components can be accessed with an integer index
with values from 0 to length minus 1.
a[2] = 1000; // modify the third element of a
●
Every array has a member called length that can
be used to get the length of the array:
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 41
Arrays
class Arrays {
public static void main(String[] args) {
int[] a = {2,4,3,1};
$ javaFirst
Carlos Kavka Arrays
Latin American Workshop on Distributed Laboratory Instrumentation Systems 42
d[1]=0.5 d[3]=0.25 d[5]=0.16666667
d[7]=0.125 d[9]=0.1
Command line arguments
●
We have seen that the method main has to be
defined as follows:
●
Through the array argument, the program can get
access to the command line arguments
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 43
Command line arguments
class CommandArguments {
public static void main(String[] args) {
for(int i = 0;i < args.length;i++)
System.out.println(args[i]);
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 44
Command line arguments
class Add {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Error");
System.exit(0);
}
int arg1 = Integer.parseInt(args[0]);
int arg2 = Integer.parseInt(args[1]);
System.out.println(arg1 + arg2);
}
}
$ java Add 24
Error args args
[0][1] [0]
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation Systems 45