LAB_2___Sup on os architacter
LAB_2___Sup on os architacter
Jan-Jun 2024
Named after the Java island in Indonesia, known for its tasty coffee.
1
Wanna learn Java, check out this link:
https://www.w3schools.com/java/default.asp
Dr. Karim Lounis AI224: Operating Systems Jan-Jun 2024 2 / 27
Lab 2
The bytecode can be executed on any computer architecture that has the
proper JVM for that architecture. The price to pay is a slower execution.
Dr. Karim Lounis AI224: Operating Systems Jan-Jun 2024 6 / 27
Lab 2
☞ To use Java (write, compile, and execute), you need to download and
install the suitable JDK for your operating system (JRE if execution only).
Go to https://www.java.com/fr/download/manual.jsp, or
On GNU/Linux, run $sudo apt install default-jdk
Characters are specified with the standard C notation, with extensions for
Unicode values:
Java Expressions
Arithmetic. +, -, /, *, and %.
int z = (((x - y) * 3) / 2) % 5;
int a +=5; int b *=3; b++; ++c;
Bit level. Used to deal with bites:
& for binary AND.
| for binary OR.
∼ for complement.
<< for left shifting.
>> for right shifting.
Relational. ==, !=, <, >, <=, and >= (Returning booleans).
Logical. There are three operators:
&& for logical AND.
|| for logical OR.
! for negation.
Strings. There are many. E.g., s1 + s2 is used to concatenate two
strings s1 and s2, and s1.equal(s2) to check their equality.
Dr. Karim Lounis AI224: Operating Systems Jan-Jun 2024 13 / 27
Lab 2
Java Arrays
An array is an object. You can get its attributes or call a method on it.
Java Arrays
args[i]
if(args.length != 3) System.exit(0);
Classes in Java
A Java class is a template for objects. It is defined using the class keyword
and a name: class House { . . . }.
A class contains attributes and methods.
Then, we can create instances of the class (i.e., objects) using new:
House H1 = new House();
House H2 = new House();
H3 = new House();
A Constructor can be used to create a customized instance of a given class.
It is a void-returning method that holds the same name as the class:
H1 = new House("90 Virginia Street", "K7K5Y4", "Lounis");
A Java class contains instance variables and methods (functions in C):
H1.address; — this is an attribute
H2.owner; — this is an attribute
H3.AddTanent();— this is a method
Dr. Karim Lounis AI224: Operating Systems Jan-Jun 2024 17 / 27
Lab 2
Classes in Java
Here is an example of creating an instance book of the class Books using
its customized constructor:
Methods in Java
Is used to implement the messages that an instance (or class) can receive:
Static:
When you declare a variable or a method as static, it belongs to the class,
rather than a specific instance. It will be shared by all objects.
Also, a static method belongs to the class rather than instances. Thus, it
can be called without creating an instance of the class. It is used for altering
the static contents of the class.
Public:
Declares a member’s access as public. Public members are visible to all
other classes (i.e., can be updated by any other class if not final).
Java Inheritance
Java Input/Output
In Java, there are two data streams: input and output. They can be byte
streams or character streams, buffered and non buffered:
Input. Is the stream used to get data from outside (e.g., keyboard,
network, files, etc). To read input from keyboard, do the following:
System.out.println("Message");
System.out.print("Message");
Java Packages
A package is a structure in which classes can be organized (i.e., a folder).
The standard classes in the system are organized in packages. We can use
them after importing them using the keyword import, e.g.,:
Java Exceptions
The usual behavior on runtime errors is to abort the execution.
s="Hello"; System.out.println(s.charAt(13));
Exception in thread "main"
java.lang.StringIndexoutOfBoundsException:
String index out of range: 13
at java.lang.String.charAt(String.java:499)
at TestExceptions1.main(TestException1.java:11)
Java Exceptions
The usual behavior on runtime errors is to abort the execution.
s="Hello"; System.out.println(s.charAt(13));
Exception in thread "main"
java.lang.StringIndexoutOfBoundsException:
String index out of range: 13
at java.lang.String.charAt(String.java:499)
at TestExceptions1.main(TestException1.java:11)
In Java, exceptions can be handled (Precisely):