Introduction to Java, Programming Basic Concepts
- 6 de março de 2009
Java was born at Sun Microsystems in 1995 and quickly caught the attention of programmers at the time. It wasn’t just for being another language, but because it came with an entire execution environment and libraries, designed to be secure, stable and simple to distribute. The slogan “write once, run anywhere” appeared because code is compiled to bytecode and runs on top of the JVM. In other words, you compile once and execute wherever there’s a virtual machine available.
Another point that always attracted me is object orientation treated as the language foundation. Except for primitive types, everything revolves around classes and objects. To start well, it’s worth understanding four ideas that come back all the time: abstraction to hide details that don’t matter, encapsulation to protect state and expose behavior, inheritance to reuse code when it makes sense and polymorphism to allow different objects to respond to the same message in their own ways.
The syntax is less intimidating than it seems. Variable declaration follows the type and name pattern, with option to list several of the same type. It goes like this:
int i;
float total, price;
byte mask;
double averageValue;
char option1, option2;
Comments can be inline or block. No mystery here.
// inline comment
/*
block
comment
*/
Basic operators cover what you already expect in daily calculations, comparisons and boolean logic. I leave short tables for quick reference.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | a + b |
| , | Subtraction | a, b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Integer division remainder | a % b |
| , | Negative (unary) | -a |
| + | Positive (unary) | +a |
| ++ | Increment | ++a or a++ |
| — | Decrement | —a or a— |
| Operator | Meaning | Example |
|---|---|---|
| == | Equal | a == b |
| != | Different | a != b |
| > | Greater than | a > b |
| >= | Greater or equal | a >= b |
| < | Less than | a < b |
| <= | Less or equal | a <= b |
| Operator | Meaning | Example |
|---|---|---|
| && | Logical AND | a && b |
| || | Logical OR | a || b |
| ! | Negation | !a |
For beginners, loops and decisions are what make the program come alive. The for is direct and works well for counts and simple iterations.
public class ForExample {
public static void main(String[] args) {
for (int j = 0; j < 10; j++) {
System.out.println(j);
}
}
}
The if solves cases where you need to decide between paths and can be chained without complication.
public class IfExample {
public static void main(String[] args) {
int j = 1;
if (j == 1) {
for (j = 0; j < 10; j++) {
System.out.println(j);
}
}
}
}
When choice depends on a specific value, switch keeps code clean and easy to read.
public class SwitchExample {
public static void main(String[] args) {
if (args.length > 0) {
switch (args[0].charAt(0)) {
case 'a':
case 'A': System.out.println("Vowel A"); break;
case 'e':
case 'E': System.out.println("Vowel E"); break;
case 'i':
case 'I': System.out.println("Vowel I"); break;
case 'o':
case 'O': System.out.println("Vowel O"); break;
case 'u':
case 'U': System.out.println("Vowel U"); break;
default: System.out.println("Not a vowel");
}
} else {
System.out.println("No argument provided");
}
}
}
Part of Java’s appeal is in managed execution. The language has automatic garbage collection to handle memory, well-defined exception handling and a virtual machine that evolved a lot in performance with JIT and runtime optimizations. Add to this an extensive and cross-platform standard library and you have a set that serves from web applications to enterprise services.
If you’re starting now, my advice is to practice a little every day. Play with examples, change values, create simple classes and make small programs that print something useful. Understanding flow, types and object model early makes everything that comes after easier.