0% found this document useful (0 votes)
16 views

Java notes

ce document contient un résumé sur le langage de programmation java

Uploaded by

Israe Benouda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java notes

ce document contient un résumé sur le langage de programmation java

Uploaded by

Israe Benouda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Java notes :

public class Main {

This line declares a class named Main, which is public, that means that any
other class can access it
public static void main(String[] args) {

This is the entry point of our Java program:

 public again means that anyone can access it.


 static means that you can run this method without creating an instance
of Main
 void means that this method doesn't return any value.
 main is the name of the method.

System.out.println("This will be printed");

 System is a pre-defined class that Java provides us and it holds some


useful methods and variables.

out is a static variable within System that represents the output of your
program (stdout)

 println is a method of out that can be used to print a line.

Les primitives:

 byte (number, 1 byte)


 short (number, 2 bytes)
 int (number, 4 bytes)
 long (number, 8 bytes)
 float (float number, 4 bytes)
 double (float number, 8 bytes)
 char (a character, 2 bytes)
 boolean (true or false, 1 byte)

To declare and assign a number use the following syntax:

int myNumber;

myNumber = 5;

or:

int myNumber = 5;

characters and strings:

1
there is a special syntax for chars:

char c = 'g';

#There is no operator overloading in Java but there is the exception that proves
the rule - string is the only class where operator overloading is supported. We can
concat two strings using + operator. The operator + is only defined for strings,
you will never see it with other objects, only primitives.#

You can also concat string to primitives:

int num = 5;

String s = "I have " + num + " cookies";

Every comparison operator in java will return the type boolean. Unlike
other languages, it only accepts two special values: true or false.

boolean b = false;

b = true;

conditionals:
Java uses boolean variables to evaluate conditions. The boolean
values true and false are returned when an expression is compared or
evaluated. For example:
int a = 4;

boolean b = a == 4;

if (b) {

System.out.println("It's true!");

Normally, we just use the short version:

int a = 4;

2
if (a == 4) {

System.out.println("Ohhh! So a is 4!");

Boolean operators:
int a = 4;

int b = 5;

boolean result;

result = a < b; // true

result = a > b; // false

result = a <= 4; // a smaller or equal to 4 - true

result = b >= 6; // b bigger or equal to 6 - false

result = a == b; // a equal to b - false

result = a != b; // a is not equal to b - true

result = a > b || a < b; // Logical or - true

result = 3 < a && a < 6; // Logical and - true

result = !result; // Logical not - false

if-else and between:


if (a == b) {

// We already know this part

} else {

// a and b are not equal... :/

Or it can be used in one line without {}:


if (a == b)

System.out.println("Another line Wow!");

else

System.out.println("Double rainbow!");

3
== and equals:
The operator == works a bit different on objects than on primitives. When we are
using objects and want to check if they are equal, the operator == will say if they
are the same, if you want to check if they are logically equal, you should use
the equals method on the object.
String a = new String("Wow");

String b = new String("Wow");

String sameA = a;

boolean r1 = a == b; // This is false, since a and b are not the same


object

boolean r2 = a.equals(b); // This is true, since a and b are logically


equals

boolean r3 = a == sameA; // This is true, since a and sameA are really the
same object

arrays:
Arrays in Java are also objects. They need to be declared and then created. In
order to declare a variable that will hold an array of integers, we use the
following syntax:
int[] arr;

we will create a new array with the size of 10. We can check the size by printing
the array's length:
arr = new int[10];

System.out.println(arr.length);

We can access the array and set values:


arr[0] = 4;

arr[1] = arr[0] + 5;

We can also create an array with values in the same line:


int[] arr = {1, 2, 3, 4, 5};

To print out an array, use the following code:

for (int i=0; i < arr.length; i++) {

System.out.println(arr[i]);

4
}

Loops:
For
for (int i = 0; i < 3; i++) {}

The for loop has three sections:

 First section runs once when we enter the loop.


 Second section is the gate keeper, if it returns true, we run the
statements in the loop, if it returns false, we exit the loop. It runs
right after the first section for the first time, then every time the loop
is finished and the third section is run.
 The third section is the final statement that will run every time the
loop runs.

While

while (condition) {}

The condition will run for the first time when entering and every time the loop is
done. If it returns false, the loop will not run.

If we want the loop to always run at least one, we can use do-while

do {(instructions)

} while(condition);

Foreach
int[] arr = {2, 0, 1, 3};

for (int el : arr) {

System.out.println(el);

Break and continue


break will cause the loop to stop and will go immediately to the next statement
after the loop:
continue will stop the current iteration and will move to the next one. Notice that
inside a for loop, it will still run the third section.

Functions:

5
In Java, all function definitions must be inside classes. We also call functions methods.
Let's look at an example method
public class Main {

public static void foo() {

// Do something here

 static means this method belongs to the class Main and not to a specific
instance of Main. Which means we can call the method from a different
class like that Main.foo().
 void means this method doesn't return a value. Methods can return a
single value in Java and it has to be defined in the method declaration.
However, you can use return by itself to exit the method.
 This method doesn't get any arguments, but of course Java methods can
get arguments as we'll see further on.

Arguments:
By value means that arguments are copied when the method runs. Let's
look at an example.

public void bar(int num1, int num2) {

...

Here is a another place in the code, where bar is called

int a = 3;

int b = 5;

bar(a, b);

non static methods:


Non static methods can access and alter the field of the object
public class Student {

private String name;

public String getName() {

6
return name;

public void setName(String name) {

this.name = name;

Calling the methods will require an object of type Student.


Student s = new Student();

s.setName("Danielle");

String name = s.getName();

Student.setName("Bob"); // Will not work!

Student.getName(); // Will not work!

Summary

 Every Java method has to be within a class


 Static methods belong to a class while non-static methods belong to objects
 All parameters to functions are passed by value, primitives content is copied,
while objects

Les objets:
class Point {

int x;

int y;

This class defined a point with x and y values.

In order to create an instance of this class, we need to use the


keyword new.

Point p = new Point();

7
In this case, we used a default constructor (constructor that doesn't get
arguments) to create a Point. All classes that don't explicitly define a
constructor has a default constructor that does nothing.

We can define our own constructor:

class Point {

int x;

int y;

Point(int x, int y) {

this.x = x;

this.y = y;

1. this:
 this is a reference to the current object within an instance
method or constructor.
 It is used to access instance variables and methods of the
current object.
 When you have a local variable or parameter with the same
name as an instance variable, this can be used to
disambiguate between them.
 It is also used to invoke one constructor from another
constructor in the same class, using the this() constructor
call.
 Example:
java
Copy code
public class MyClass { private int x; public MyClass(int x) { this .x = x;
// Assigning the parameter x to the instance variable x } public void
printX() { System.out.println( "x = " + this .x); // Accessing the instance
variable x } }
2. new:
 new is used to dynamically allocate memory for an object at
runtime.
 It is used to create instances of classes by invoking a class's
constructor.
 When you create an object using new, memory is allocated on
the heap for that object, and the constructor of the class is
called to initialize the object.

8
 Example:
java
Copy code
MyClass obj = new MyClass (); // Creating a new instance of MyClass

In summary, you use this to refer to the current object within a class,
mainly to access its members or differentiate between instance variables
and local variables/parameters. On the other hand, you use new to
dynamically allocate memory and create instances of classes.

Inheritance:
the term inheritance refers to the adoption of all non-private properties
and methods of one class (superclass) by another class (subclass).
Inheritance is a way to make a copy of an existing class as the starting
point for another.
Interfaces define only the structure of the class members while inherited
classes include the actual code of the superclass. Additionally, inheritance
(more accurately, the definition of a subclass) uses
the extends keyword in the subclass declaration.

You might also like