Java
Java
0.9
Table of contents
Objectives 3
I - Java Programming Language 4
1. Java Features .............................................................................................................4
2. Java Portability ..........................................................................................................4
3. Tools to make Java Programs...................................................................................4
4. A first Java program ..................................................................................................5
5. Primitive types ...........................................................................................................5
6. Primitive types ...........................................................................................................6
7. Wrapper Classes ........................................................................................................6
8. Operators ...................................................................................................................6
9. Type Conversion ........................................................................................................7
10. Control Structures ...................................................................................................7
11. Strings ......................................................................................................................8
12. Arrays........................................................................................................................9
13. The Scanner Class..................................................................................................11
14. Test your knowledge .............................................................................................12
2
Objectives
3
Java Programming Language I
1. Introduction
There are many Object-Oriented programming languages:
- Java,
- C++,
- Python,
- C#,
- PHP,
- Ruby,
- etc.
2. Java Features
- Java is an Object-Oriented programming language
- It has a syntax close to that of C++ programming language.
- It is portable, i.e. the compiled code could be executed on different platforms and in different
environments.
3. Java Portability
4
Java Programming Language
- The instruction System.out.println(...) allows to display the text between the parentheses.
- The compilation generates the file "Test.class".
- The execution displays on the console the text:
Welcome L1
Output:
1 Welcome Section 3, I am agent 1
2 Welcome Section 3, I am agent 2
6. Primitive types
- Conventional or traditional types.
- They are not object classes.
- Used to declare variables and constants in methods and attributes in classes.
Integer types
Type Size Min Max
5
Java Programming Language
7. Primitive types
boolean, chars and reals
Type Size
boolean 1 bit
char 16 bits
float 32 bits
double 64 bits
8. Wrapper Classes
To manipulate integers, reals and others like objects.
A wrapper type includes a primitive variable and offers a set of methods applicable on the
variable.
int Integer
byte Byte
short Short
long Long
boolean Boolean
char Char
float Float
double Double
9. Operators
Operator Description
= Assignment
++ -- (in/de)crementation
...
6
Java Programming Language
Note:
Curly braces {} are optional if a block contains a single statement.
The Switch Statment
1 switch (expression){
2 case v1:
3 bloc_1; //instructions executed when expression==v1
4 break;
5 case v2:
6 bloc_2; //instructions executed when expression==v2
7 break;
8 ...
9 default:
10 bloc_n; //instructions executed when expression is other then v1,v2,...
11 break;}
"While" Loop
1 while(condition){
2 bloc;}
or
1 do{
2 bloc;} while(condition);
"For" Loop
1 for(preparation; condition; change){
2 bloc;}
7
Java Programming Language
12. Strings
The class String
Declaration
1 String s="NTIC L1 MI";
Display
1 System.out.println(s);
displays:
1 NTIC:
2 L1\ "MI"
Concatenation:+
Example:
1 String s1="L1",s2="MI";
2 s1+=" "+s2;
3 System.out.println(s1); displays L1 M1
Some Methods:
Method Description
8
Java Programming Language
Method Description
Example
1 public static void main(String[] args) {
2 String s=" Welcome L1 MI ";
3 s=s.trim();
4 System.out.println("1) The length of \""+s+"\" is "+s.length());
5 System.out.println("2) "+s.equals("Welcome L1 Mi"));
6 s=s.toUpperCase();
7 System.out.println("3) "+s.equals("Welcome L1 Mi".toUpperCase()));
8 System.out.println("4) "+s.charAt(2));
9 System.out.println("5) "+s.substring(8));
10 System.out.println("6) "+s.substring(1,3));
11 s=s.substring(0,s.indexOf(" "))+"-"+s.substring(s.indexOf(" ")+1);
12 System.out.println("7) "+s);
13 }
Output:
1 1) The length of "Welcome L1 MI" is 13
2 2) false
3 3) true
4 4) L
5 5) L1 MI
6 6) EL
7 7) WELCOME-L1 MI
13. Arrays
Declaration:
1 elemType[] arrId;
Or
1 elemType arrId[];
e.g.
1 int[] t;
2 Double[] m;
3 char[] code;
9
Java Programming Language
Output:
Multidimensional Arrays
It is an array of arrays; each element is an array.
Declaration of a 2-dimensional array:
1 elemType[][] arrId;
2 //e.g.
3 double[][]m=new double[5][3]; //5 rows et 3 columns
4 int[][]t={{1,255},{21,4,16}}; // 2 rows with different lengths!!
5
Example:
1 public class Essai {
2 public static void main(String[] args) {
3 int[][]t= {{1,255},{21,4,16}};
4 int[][]y=t;
5 int temp=t[0][0];
6 t[0][0]=t[1][0];
7 t[1][0]=temp;
8 for(int i=0;i<y.length;i++) {
9 for(int j=0;j<y[i].length;j++)
10 System.out.print(y[i][j]+"\t");
11 System.out.print("\n");}
12 }
13 }
Output:
10
Java Programming Language
We declare an object from the class Scanner and associate it with the standard input flow System.in
1 Scanner rdr=new Scanner(System.in);
Method Description
Example:
1 import java.util.Scanner;
2 public class Test {
3 public static void main(String[] args) {
4 int a,b;
5 String s;
6 Scanner sc=new Scanner(System.in);
7 System.out.print("Type two integer values:");
8 while(!sc.hasNextInt()) {
9 s=sc.next();
10 System.out.print("Invalid input, retry please:");}
11 a=sc.nextInt();
12 while(!sc.hasNextInt()) {
13 s=sc.next();
14 System.out.print("Invalid input, retry please:");}
15 b=sc.nextInt();
16 System.out.print(a+"+"+b+"="+(a+b));
17 sc.close();
11
Java Programming Language
18 }
19 }
Output:
Java code can run on any platform with the Java Virtual Machine (JVM)
Quiz 2
Which of the following are primitive data types in Java?
Char
float
String
boolean
Quiz 3
What is the wrapper class for the primitive data type int in Java?
Int
intWrap
integer
Integer
Quiz 4
What is the result of the expression 10/3 in Java?
3.33
3.0
3
Error
Quiz 5
What is the output of the following code snippet?
12
Java Programming Language
1 int x=10,y=3;
2 System.out.println("Sum="+x+y);
Sum=13
Sum=103
Sum=x+y
Error
Quiz 6
Which of the following casting are done implicitly in Java?
double to int
long to float
char to String
boolean to int
Quiz 7
What is true about wrapper classes?
They are equivalent to primitive types
Quiz 8
Which of the following correctly declares an array in Java?
int[] tab=new int[5];
int numbers[5]
Quiz 9
What is the index of the last character in the String "Hello"?
5
4
6
"Hello".length()-1
Quiz 10
Which methods are used to read input from the user in Java?
getInput()
scanInt()
13
Java Programming Language
nextDouble()
next()
Quiz 11
Closing the Scanner object
is obligatory to allow running the program.
is highly recommended.
is done implicitly.
Quiz 12
Which of the following are valid ways to initialize a string in Java?
String str="Hello";
String str="";
Quiz 13
What is the default value of an uninitialized integer variable in Java?
0
null
a random value
Error
Quiz 14
An matrix in Java
all the rows should have the same length.
the rows could be initialized at different times with elements of different types.
Quiz 15
Suppose that the value of i was 5, the instrauction "i+=(++i);" will set its value to:
10
6
11
Error
Quiz 16
According to Java conventions,
14
Java Programming Language
the first letter in each word in a compound identifier after the first is capitalized.
15