You are on page 1of 10

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 platform has two components:

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:

Development tools: from compile


Application Programming Interface (API): wide
Deployment Technologies: like java web starter and java plug and play
User Interface Toolkits: JavaFX, Swing, Java 2D
Integration Libraries:
Java IDL API
JDBC API
Java Naming and Directory Interface (JNDI) API
Java RMI
Java Remote Method Invocation over Internet Inter-ORB Protocol Technology
(Java RMI-IIOP Technology)

The Java Language


Object-Oriented Programming Concepts
What is an object?
understanding object-oriented technology require looking at objects around us and and
recognize that they all have state and behavior
Example:
Cars:
States: (model, color, year)
Behavior: (Sound, breaks)

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";
}

class Car implements CarInterface


{
String color = "";
boolean engineState = false;
public void toggleEngine() {
if (engineState == true) {
engineState = false;
System.out.println("Engine OFf");
}
else {
engineState = true;
System.out.println("Engine Started");
}
}
}

class RoadCar extends Car {


public RoadCar(){
System.out.println("fef");
}
}
class HelloWorldApp
{

public static void main(String[] args)


{
System.out.println("hellow world");

Car car1 = new Car();


car1.toggleEngine();

RoadCar car2 = new RoadCar();


car2.toggleEngine();

}
}

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();
}

class implements Lin

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 = += -= *= /= %= &= ^=

Expressions, Statements, and Blocks


an expression is a construct made up of variables, operators, method invocations according
to the syntax of the language, that evaluates to a single value
Note for later to research java eval rules and stuff

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();

declaration statements and control flow statements. A declaration statement declares a


variable.

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 :*

OuterClass.NestedClass nestedInstance = new OuterClass.NestedClass(20);

Shadowing
public class ShadowingExample {
private int x = 10;

public void someMethod() {


int x = 5; // This variable shadows the instance variable 'x' in
this method's scope.
System.out.println("Inner x: " + x); // Prints the value of the
inner 'x' (5).

// 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 static void main(String[] args) {


ShadowingExample example = new ShadowingExample();
example.someMethod();
}
}
// and
public class ShadowTest {

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);
}
}

public static void main(String... args) {


ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}

Enum Types
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

public class EnumTest {


Day day;
public EnumTest(Day day){
this.day = day;
}
for
}

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

// class code goes here


@interface ClassPreamble {
String author();
String date();
int currentRevision default 1;
String lastModified();
String By();
String[] Reviewers();
}

@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;
}
}

public class Box<T> {


private T object;
private void set(T object) {
this.object = object;
}
private T get(){
return object;
}
}

Type naming conventions


E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value

Using the generic type

Box<String> = mybox new Box<String>();

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

OrderedPair<String, **Box<Integer>**> p = new OrderedPair<>("primes", new


Box<Integer>(...));

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

You might also like