Java basics, step by step

Java

Robust · popular · in demand

Banking platforms

High availability

Enterprise backend

Massive systems

Android

Mobile applications

Microservices

Modern architectures

C / C++

programa.c

One source code

.exe

Windows x86

ELF

Linux ARM

Mach-O

macOS Apple Silicon

Three compilations, three different binaries.

Java · WORA

Programa.java

One source code

javac
Programa.class

One single bytecode

JVM

Windows

JVM

Linux

JVM

macOS

Write Once, Run Anywhere

JDK · Java Development Kit

Developers
javac jar javadoc jdb

JRE · Java Runtime Environment

Run
java.lang java.util java.io java.net

JVM · Java Virtual Machine

bytecode

Class Loader

Loads .class files into memory

Bytecode Verifier

Checks safety and integrity

JIT

Hot bytecode → native code

Garbage Collector

Frees unused objects

HolaMundo.java
public class HolaMundo { public static void main(String[] args) { System.out.println("Hello, World from Java!"); } }
keyword name text string
HolaMundo.java

Step 1 · create

javac
HolaMundo.class

Step 2 · compile

java

Console

Hello, World from Java!

Step 3 · run

Command line

$ java SaludoPersonalizado

Command line

$ java SaludoPersonalizado Facundo
String[] args
empty —
"Facundo" [0]
args.length == 0 args.length == 1

if (args.length > 0)

"Hello, " + args[0] + "! Welcome to Java engineering."

else

"Hello, Anonymous Developer! Pass your name as an argument."

PascalCase

Classes and interfaces

HolaMundo CuentaBancaria UsuarioService

camelCase

Methods and variables

calculateTotal() userName initialBalance

UPPER_SNAKE_CASE

Constants

MAX_RETRY_COUNT PI

;

Ends every statement

// ...

Single line

/* ... */

Multi-line

/** ... */

Javadoc · API documentation

Local IDE

IntelliJ IDEA · Eclipse · VS Code

Offline Local JDK Installation

Online IDE

In the browser

Quick start CPU and version limits Code at a third party

Terminal

javac + java

Real flow visible No completion

Safe default: local LTS JDK + terminal as the reference

  • 1java --version and javac --version compatible
  • 2The project uses that JDK, not the one the IDE picked
  • 3Compile and run once from the terminal
  • 4Keep it locally and under version control
  • 5Never paste passwords or personal data online

Javadoc

Project JDK version
Module java.base Package java.lang Class String

Summary: purpose, inheritance, interfaces and general notes of the class.

public String substring(int beginIndex, int endIndex)

Parameters

beginIndex inclusive · endIndex exclusive

Returns

A new String; does not modify the original

Throws

IndexOutOfBoundsException if the indices fall out of range or are reversed

Since Deprecated

Availability and migration signals

Linked types → follow the link and repeat the process: they are part of the contract too.

Mistake 1

class HolaMundo is public, should be declared in a file named HolaMundo.java

The file is not named exactly like the public class (capitalization included).

Mistake 2

Error: Could not find or load main class HolaMundo

You ran java HolaMundo.class instead of java HolaMundo, or you are not in the right directory.

Mistake 3

java.lang.ArrayIndexOutOfBoundsException

You accessed args[0] without checking args.length > 0 and passed no arguments.

PerfilDesarrollador.java

args.length >= 2 ?

yes · name and language
========================================== Developer: Facundo Specialty: Java ==========================================
no · missing arguments
Usage: java PerfilDesarrollador <YourName> <YourLanguage>
1 · WORA · JDK / JRE / JVM 2 · Compilation and execution 3 · Anatomy of HolaMundo 4 · Terminal: javac and java 5 · String[] args 6 · Conventions 7 · Development environment 8 · API and Javadoc 9 · Common mistakes 10 · Exercise

CursoDeJava · l01_conceptos_basicos

Ej01HolaMundo Ej02SalidaEstandar Ej03ConstantesYConvenciones