Java notes
Java notes
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) {
out is a static variable within System that represents the output of your
program (stdout)
Les primitives:
int myNumber;
myNumber = 5;
or:
int myNumber = 5;
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.#
int num = 5;
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!");
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;
} else {
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 sameA = a;
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);
arr[1] = arr[0] + 5;
System.out.println(arr[i]);
4
}
Loops:
For
for (int i = 0; i < 3; i++) {}
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};
System.out.println(el);
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 {
// 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.
...
int a = 3;
int b = 5;
bar(a, b);
6
return name;
this.name = name;
s.setName("Danielle");
Summary
Les objets:
class Point {
int x;
int y;
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.
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.