Conceitos de Java
Conceitos fundamentais da linguagem de programação Java, com exemplos práticos de código para estudantes e desenvolvedores.
Cartões · 53
- Hello World / Main Method
- public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }
- Declaring Primitive Variables
- int age = 25; double price = 19.99; char grade = 'A'; boolean active = true;
- If-Else Conditional
- if (score >= 60) { System.out.println("Passed"); } else { System.out.println("Failed"); }
- Switch Statement (Traditional)
- switch (day) { case 1: System.out.println("Monday"); break; default: System.out.println("Other"); }
- Switch Expression (Modern Java)
- String type = switch (day) { case 1, 7 -> "Weekend"; default -> "Weekday"; };
- For Loop
- for (int i = 0; i < 5; i++) { System.out.println(i); }
- Enhanced For Loop (For-Each)
- int[] numbers = {1, 2, 3}; for (int n : numbers) { System.out.println(n); }
- While Loop
- int count = 0; while (count < 3) { System.out.println(count); count++; }
- Do-While Loop
- int x = 0; do { System.out.println(x); x++; } while (x < 1);
- Array Declaration and Initialization
- String[] colors = new String[]{"red", "green", "blue"}; int[] nums = new int[5];
- Class and Object Instantiation
- public class Car { String model; } Car c = new Car(); c.model = "Sedan";
- Constructor Declaration
- public class Person { String name; public Person(String name) { this.name = name; } }
- Method Definition and Return
- public int add(int a, int b) { return a + b; }
- Method Overloading
- public int sum(int a, int b) { return a + b; } public double sum(double a, double b) { return a + b; }
- Inheritance (extends)
- class Animal { void eat() {} } class Dog extends Animal { void bark() {} }
- Method Overriding (@Override)
- class Dog extends Animal { @Override void speak() { System.out.println("Woof"); } }
- The 'super' Keyword
- public Dog(String name) { super(name); } // calls parent class constructor
- Interface Implementation
- interface Drivable { void drive(); } class Car implements Drivable { public void drive() {} }
- Abstract Class and Method
- abstract class Shape { abstract double getArea(); }
- Static Variable and Method
- public class MathUtils { public static final double PI = 3.1415; public static int square(int x) { return x * x; } }
- Final Variable (Constant)
- final double TAX_RATE = 0.05; // value cannot be reassigned
- Try-Catch Exception Handling
- try { int res = 10 / 0; } catch (ArithmeticException e) { System.out.println(e.getMessage()); }
- Try-Catch-Finally
- try { openFile(); } catch (IOException e) { log(e); } finally { closeFile(); }
- Try-with-Resources
- try (BufferedReader br = new BufferedReader(new FileReader("f.txt"))) { br.readLine(); }
- Throwing an Exception
- if (age < 0) { throw new IllegalArgumentException("Age cannot be negative"); }
- Throws Clause
- public void readFile() throws IOException { FileReader f = new FileReader("file.txt"); }
- String Concatenation and Immutability
- String full = "Hello, " + "World!"; String upper = full.toUpperCase();
- StringBuilder Usage
- StringBuilder sb = new StringBuilder(); sb.append("Hello").append(" ").append("World"); String res = sb.toString();
- ArrayList (List Collection)
- List<String> list = new ArrayList<>(); list.add("Java"); list.remove(0);
- HashSet (Set Collection)
- Set<Integer> unique = new HashSet<>(); unique.add(1); unique.add(1); // size is 1
- HashMap (Map Collection)
- Map<String, Integer> map = new HashMap<>(); map.put("apple", 3); int count = map.get("apple");
- Iterating Over Map Entries
- for (Map.Entry<String, Integer> e : map.entrySet()) { System.out.println(e.getKey() + ": " + e.getValue()); }
- Generic Class Definition
- public class Box<T> { private T val; public void set(T val) { this.val = val; } public T get() { return val; } }
- Generic Method Definition
- public <T> void printItem(T item) { System.out.println(item); }
- Lambda Expression
- Runnable r = () -> System.out.println("Running in lambda"); r.run();
- Predicate Functional Interface
- Predicate<Integer> isEven = n -> n % 2 == 0; boolean result = isEven.test(4);
- Function Functional Interface
- Function<String, Integer> strLength = s -> s.length(); int len = strLength.apply("test");
- Consumer Functional Interface
- Consumer<String> printer = s -> System.out.println(s); printer.accept("Hello");
- Supplier Functional Interface
- Supplier<Double> randomGen = () -> Math.random(); double val = randomGen.get();
- Stream Filter and ForEach
- List<Integer> nums = List.of(1, 2, 3, 4); nums.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
- Stream Map and Collect
- List<String> words = List.of("a", "b"); List<String> up = words.stream().map(String::toUpperCase).toList();
- Stream Reduce
- int sum = List.of(1, 2, 3, 4).stream().reduce(0, (a, b) -> a + b);
- Method Reference
- List<String> names = List.of("Anna", "Bob"); names.forEach(System.out::println);
- Optional Value Handling
- Optional<String> opt = Optional.ofNullable(name); String val = opt.orElse("Default");
- Enum Declaration and Usage
- enum Status { PENDING, ACTIVE, DONE } Status s = Status.ACTIVE;
- Record Type (Java 14+)
- public record Point(int x, int y) {} Point p = new Point(10, 20); int x = p.x();
- Text Blocks (Java 15+)
- String json = """ { "name": "John", "age": 30 } """;
- Pattern Matching for instanceof (Java 16+)
- if (obj instanceof String s) { System.out.println(s.length()); }
- Sealed Interface / Class (Java 17+)
- sealed interface Shape permits Circle, Square {} final class Circle implements Shape {}
- Var Keyword (Local Variable Type Inference)
- var message = "Inferred as String"; var list = new ArrayList<String>();
- Creating a Thread via Runnable
- Thread t = new Thread(() -> System.out.println("Thread running")); t.start();
- Synchronized Block
- synchronized (this) { count++; } // ensures thread-safe modification
- Autoboxing and Unboxing
- Integer wrapped = 10; // autoboxing; int primitive = wrapped; // unboxing