mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
[java/en] Update java.html.markdown with modern Java updates (#5128)
This commit is contained in:
parent
495272cff9
commit
742574706b
@ -44,6 +44,9 @@ Multi-line comments look like this.
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
// Import all classes inside of java.security package
|
// Import all classes inside of java.security package
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
|
// Java to illustrate calling of static members and methods without calling classname
|
||||||
|
import static java.lang.Math.*;
|
||||||
|
import static java.lang.System.*;
|
||||||
|
|
||||||
public class LearnJava {
|
public class LearnJava {
|
||||||
|
|
||||||
@ -211,9 +214,21 @@ public class LearnJava {
|
|||||||
// Prefer the String constructor when you need an exact value.
|
// Prefer the String constructor when you need an exact value.
|
||||||
BigDecimal tenCents = new BigDecimal("0.1");
|
BigDecimal tenCents = new BigDecimal("0.1");
|
||||||
|
|
||||||
|
// Type inference with 'var'
|
||||||
|
var x = 100; // int
|
||||||
|
var y = 1.90; // double
|
||||||
|
var z = 'a'; // char
|
||||||
|
var p = "tanu"; // String
|
||||||
|
var q = false; // boolean
|
||||||
|
|
||||||
// Strings
|
// Strings
|
||||||
String fooString = "My String Is Here!";
|
String fooString = "My String Is Here!";
|
||||||
|
|
||||||
|
// Text blocks
|
||||||
|
vat textBlock = """
|
||||||
|
This is a <Text Block> in Java
|
||||||
|
""";
|
||||||
|
|
||||||
// \n is an escaped character that starts a new line
|
// \n is an escaped character that starts a new line
|
||||||
String barString = "Printing on a new line?\nNo Problem!";
|
String barString = "Printing on a new line?\nNo Problem!";
|
||||||
// \t is an escaped character that adds a tab character
|
// \t is an escaped character that adds a tab character
|
||||||
@ -459,6 +474,8 @@ public class LearnJava {
|
|||||||
System.out.println(br.readLine());
|
System.out.println(br.readLine());
|
||||||
// In Java 7, the resource will always be closed, even if it throws
|
// In Java 7, the resource will always be closed, even if it throws
|
||||||
// an Exception.
|
// an Exception.
|
||||||
|
} catch (IOException | SQLException ex) {
|
||||||
|
// Java 7+ Multi catch block handle both exceptions
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
//The resource will be closed before the catch statement executes.
|
//The resource will be closed before the catch statement executes.
|
||||||
System.out.println("readLine() failed.");
|
System.out.println("readLine() failed.");
|
||||||
@ -852,6 +869,12 @@ public abstract class Mammal()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Java Records are a concise way to define immutable data carrier classes, automatically
|
||||||
|
// generating boilerplate code like constructors, equals(), hashCode()and toString().
|
||||||
|
// This automatically creates an immutable class Person with fields name and age.
|
||||||
|
public record Person(String name, int age) {}
|
||||||
|
Person p = new Person("Alice", 30);
|
||||||
|
|
||||||
// Enum Type
|
// Enum Type
|
||||||
//
|
//
|
||||||
// An enum type is a special data type that enables for a variable to be a set
|
// An enum type is a special data type that enables for a variable to be a set
|
||||||
|
Loading…
Reference in New Issue
Block a user