Skip to content

Commit

Permalink
Update java.html.markdown with modern Java updates
Browse files Browse the repository at this point in the history
Update Java language features introduced in recents versions.
  • Loading branch information
mayureshkumbhar authored Sep 28, 2024
1 parent 495272c commit 229dcd2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions java.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Multi-line comments look like this.
import java.util.ArrayList;
// Import all classes inside of java.security package
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 {

Expand Down Expand Up @@ -211,9 +214,22 @@ public class LearnJava {
// Prefer the String constructor when you need an exact value.
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
String fooString = "My String Is Here!";

// Java 14 onwards - Text Blocks
vat textBlock = """
This is a <Text Block> in Java
""";


// \n is an escaped character that starts a new line
String barString = "Printing on a new line?\nNo Problem!";
// \t is an escaped character that adds a tab character
Expand Down Expand Up @@ -459,6 +475,8 @@ public class LearnJava {
System.out.println(br.readLine());
// In Java 7, the resource will always be closed, even if it throws
// an Exception.
} catch (IOException | SQLException ex) {
// Java 7+ Multi catch block handle both exceptions
} catch (Exception ex) {
//The resource will be closed before the catch statement executes.
System.out.println("readLine() failed.");
Expand Down Expand Up @@ -852,6 +870,12 @@ public abstract class Mammal()
}
}

//Java Records (introduced in Java 14, finalized in Java 16) 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
//
// An enum type is a special data type that enables for a variable to be a set
Expand Down

0 comments on commit 229dcd2

Please sign in to comment.