The Java™ Tutorials
The Java™ Tutorials
#review
2023-12-08, 11:36
Source:
Tags/Keywords:
Linked Notes:
Questions/Ideas:
getStarted
Java technology is both a programming language and a platform.
The _Java Virtual Machine: It's a base for the java platform
The Java Application Programming Interface (API): It's a collection of already made
ready made software utils that provide some useful capabilities
Every full implementation of the java platform gives you the following features:
What Is a Class?
A class define the blue print of the object we say the the object x is an instance of a class x
/*
*
* A simple Hello World program*/
interface CarInterface {
String model = "ferrai";
}
}
}
What Is an Interface?
an interface define the contacts between the class and the outside world
interface LinkedLists<T> {
void add(T data);
boolean remove(T data);
int size();
}
What Is a Package?
A package is a namespace that organizes a set of related classes and interfaces.
Java provide an enormous class library (a set of packages) Its packages represent the tasks
most commonly associated with general-purpose programming
Language Basics
Variables
Instance Variables (Non-Static Fields): Objects store their value in non static field
meanring that their values are unique to each instance of a class.
Class Variables (Static Fields): static keyword meaning that the same balue will apply
to every value no matharer how many instances, the keyword final could be added to
indicate that the number of gears will never change.
Local Variables: variables that are defined inside a method
Operators
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative */%
additive +-
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR
logical AND &&
logical OR
Operators Precedence
ternary ?:
assignment = += -= *= /= %= &= ^=
Statements:
statment are the equivalent to
// assignment statement
aValue = 8933.234;
// increment statement
aValue++;
// method invocation statement
System.out.println("Hello World!");
// object creation statement
Bicycle myBike = new Bicycle();
Blocks
A block is a group of zero or more statements between balanced braces and can be used
anywhere a single statement is allowed
Classes
class laptop {
// class fields
public int ram;
public int storage;
public final hinges = 2;
public laptop(int ramSize, int storageSize){
ram = ramSize;
storage = storageSize;
}
// class methods
public void open(){
}
public void close(){
}
}
Nested classes
class EnclosingClass {
class InnerClass {
}
static class StaticInner {
}
}
Non static classes have access have access to other memebers of the the EnclosingClass
even if it's private
Static nested classes do not have access to other members of the enclosing class
*static nested class; you can you can access it without creating an instance of OuterClass :*
Shadowing
public class ShadowingExample {
private int x = 10;
// You can still access the outer 'x' using 'this' keyword:
System.out.println("Outer x: " + this.x); // Prints the value of
the outer 'x' (10).
}
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("ShadowTest.this.x = " +
ShadowTest.this.x);
}
}
Enum Types
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
Annotations Basics
Information for compilers and runtime
@Author(
name = "yassineChih",
age = 31
)
@Ebook
class myClass {...}
Annotations Type
// Author: John Doe
// Date: 3/17/2002
// Current revision: 6
// Last modified: 4/12/2004
// By: Jane Doe
// Reviewers: Alice, Bill, Cindy
@ClassPreamble(
author = "John Doe",
...
reviewers = {Alice, Bill, Cindy}
)
Generics
public class Box {
private Object object;
private void set(Object object) {
this.object = object;
}
private Object get(){
return object;
}
}
The Diamond
Box<Integer> integerBox = new Box<>();
Multiple params
public interface Pair<K, V> {
public K getKey();
public V getValue();
}
Pair<String, Integer> p1 = new OrderedPair<String, Integer>("Even", 8);
Parameterized Types
Numbers
Autoboxing and Unboxing
Autoboxing is the automatic conversion that the Java compiler makes between the primitive
types and their corresponding object wrapper classes.
AutoBoxing
int -> Integer
double -> Double
Character ch = 'a';
Unboxing
int -> Integer
double -> Double
List<Integer> li = new ArrayList<>()
for (int i = 0; i > 9; i++)
li.add(i)
Packages
to create a package you put the name package on the top of the file containing the classes
there can only be one package statement on every file
If you put multiple types in the same file you can only define one public class examples:
public class Circle, Circle.java,
public interface Draggable, Draggable.java
Essential Java Classes